2

I have a system I've built in MVC 3 that currently provides a yearly submission cycle where the system proceeds through a serious of seven steps tied to dates stored in the web.config as AppSettings. However, each year, I always have to roll the system back and forth between previous steps in order to accommodate the end users. I would like to give the administrator the ability to control the system status without having to contact a developer. What is the best way to do this?

I plan to build a page with proper validation that lets the administrator set the dates. I've considered a couple options of how I should store those date, but none of them seem correct. Our entire permission system uses these dates, and various bits of text on the pages turns on and off based on what period we're currently in.

So far I've come up with two options:

Option 1: Create a database table – This was my first thought. I’ve set up properties on the MvcApplication class in the global.asax and pulled them from the database. Using a lazy loader, I can set the properties the first time they're needed. However, when they change in the database, I don't have a way to force the system to “reset” and read the date changes. If I do this action on Begin_Request(), I'm constantly opening the connection and resetting the properties for each file that the web browser opens on the server, regardless if it's static content or not.

I could directly fetch the dates from the database every time I need one of the dates, but then I'm having to redo a lot of functionality to reduce repeated database calls. I'd like to cache the dates for each request, and only pull them when I need them,

Option 2: Allow editing a config file through the application – I've looked up how to split the web.config file so I can have a separate file that just contains the appSettings. Then I could just update the new config file from a controller action. I think this would work nicely, and not require me to rewrite any of the existing functionality, but it feels like I would be introducing a bad design pattern into the code.

2
  • you not really said what the end result actually is? are you just trying to change content based on a date range? if so drop year part from your dates, have a start date, end date and content in database and load content based on time being between x and y. give them a page to change day and month ??? but just guessing here at what the actual result is Commented May 25, 2012 at 21:40
  • The dates in the web.config already control a complex workflow process that turns on and off features as the workflow progresses. This part is already implemented, but the developers control the process. We need to change the system so that the administrator, can control it without us being involved. Commented May 25, 2012 at 23:01

4 Answers 4

3

I'd vote for the database. For the sake of performance you can cache those parameter values in a static class inside your app and provide a method to reread them from DB in the same class. So:

  1. When a user makes request, check if those properties are already cached. If they are - use cached values, if no - read them from DB
  2. When administrator makes changes to those parameters - store them to database and enforce your static caching class to reread them from DB.
3
  • How will your design let another user currently logged in know the dates have changed? It's trivial to update the date cache for the administrator who makes the change, but what about other users currently logged in? Commented May 25, 2012 at 23:04
  • 2
    Static classes are Application Domain-scoped, so if the data changes in a static class then it will be changed for all the user sessions at once. Exception: If you're using Web Farm then static class will be scoped to a Web Farm process so my design won't work. Commented May 26, 2012 at 9:02
  • 1
    We are using a small Web Farm actually, so thanks for the tip. Commented May 29, 2012 at 18:39
2
+100

I would suggest an approach that doesn't care whether the settings are stored in database or key/value pairs in config file.

Since you want the settings to be accessed globally by all users you can cache the settings and the cache implementation should be generic and distributed. There are plenty of online resources available how to create such an interface.

Since you want the cache to be sync with the underlying data you have to set cache dependencies (AppFabric won't supports sql cache dependency see this thread, while NCache supports both sql and file).

2
  • What are the benefits to this approach? Commented May 31, 2012 at 18:03
  • 1
    @ICodeForCoffee: The most important benefit is that cache dependencies can automatically trigger an update of current date values in cache when you have changed them in persistent storage (either database or config file). I like this solution more than mine. Commented Jun 2, 2012 at 7:45
2

I would store the values in a database and use a distributed cache to persist the data across the web farm. MS AppFabric Caching has worked well for me. You will need to implement a standard caching pattern (check the cache, if null load from db and insert into cache).I would probably just create a static Load() method that abstracts this logic away. When the admins update the db you could update the cache or just delete the cachekey.

1
  • How would you implement this, and what are the benefits for using this technology? This looks like it will take reconfiguring the servers to achieve. Commented Jun 1, 2012 at 16:43
1

Therr are other considerations to be added to performance. Namely if you modify the config file thr application pool is re iniyializrd, while the database solution doesnt cause application reinitialization ...so do you need to re initialize the app after the changes or not?...If there i no way to avoid the inizialization whitout drastic changmes to the application ptobably the config filr solution is better

2
  • We usually need to reinitialize the application actually in this case. Features turn on and off based on these dates. While I think only forcing a reinitialization if it changes the system into another state would be ideal, that would further complicate already complex logic. Commented May 31, 2012 at 18:01
  • 1
    If the application was not designed to be "easily reinitilized", by simply changing some global application-dictionary property or similar, then it is not easy to modify it so that it can be re-initilized WITHOUT reinitializing the application pool, then the only reliable way to reinitilize it without risking to introduce bugs is just to restart the application pool. Application reinitilization can be forced either by interacting with IIS api, or by changing code or web config...so changing web config is the only easy solution Commented May 31, 2012 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.