1

I need to have a few setting values which will be used throughout the application, and I want to modify these setting values sometime.

Is it possible to load data from a database into static class members? And also modify these values and store them back to the database.

public static class CurrencySettingsBO
{

    public static int WindowPeriod { get; set; }

    public static float MarkupRate { get; set; }
}

Like this static class, how can I store the modification to the database when I modify the value, and when I start the web application next time, the application will load the value from the database?

Thanks a lot if anyone can help me.

Gavin
  • 589
  • 1
  • 6
  • 20
  • I found a possible solution here: [http://forums.asp.net/t/1665373.aspx?best+way+to+store+static+values+from+a+database+to+use+globally+] But this solution is just load data from the database, I'm still try to explore how to modify the static class value, and store the modification to database. – Gavin Mar 18 '15 at 08:49
  • 1
    You don't store a static class in a database. You store data in a database. Yes, you can create a static class which loads data when the static constructor runs, or the first time the data is accessed, and also saves the new values once data is changed. – Maarten Mar 18 '15 at 08:55
  • @Maarten Sorry, I'm still new to programming. Now I'm using ASP.NET MVC, in the Razor View, I cannot use a static class as a model. So how can I do that? – Gavin Mar 18 '15 at 09:11
  • @Maarten How can I create the data to database for the static class when I start the application first time? – Gavin Mar 18 '15 at 09:22

2 Answers2

4

I think there is could be used a simplest solution with config file, more details you are able to read by the following link: Adding and reading from a Config file

The advantage of this approach is flexibility and possibility to modify values faster then in the database.

Community
  • 1
  • 1
Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • But the user of the web application need to change these setting when they want, coz these are business settings, not application settings. – Gavin Mar 18 '15 at 09:15
  • Okay, please, describe me more detail why do you use a static class ? Is this is global settings for all users ? – Vladyslav Furdak Mar 18 '15 at 09:24
  • Oh, I found that I don't have to use static class. But using the example in the question, I need a table "CurrencySettings" in the database. But how can I have the record without user input? Is there some way can let me add the record when the application open at the first time? and then the application will load the data from the database? – Gavin Mar 20 '15 at 02:16
  • Static construcor of the static class provides you with that possibility. It calls only once when you first time acess to the static class. If you mean data insertion in the table - so you able to check it that static constructor - is the table exists and it there are keeped records that you need. – Vladyslav Furdak Mar 20 '15 at 13:18
1

You can just create Table in your database that will store those settings and fetch them every time you need to reference those settings. If you still want to use static class you can change code in your set accessors of your properties, so every time value is assigned this will be also saved in DB

user3455363
  • 408
  • 1
  • 5
  • 13