Implementing function flags in C #

Function flags are something that I often use, but never thought about, until this new project that I am working on started.

I usually implement it with a lot of keys in the web.config file, but this approach has two main disadvantages:

  • When the value inside web.config changes, the application pool restarts - this can be a problem in a hard-access environment.
  • Too many keys in the web.config file are confusing and can become quite dirty.

What is the best way to overcome these problems?

+6
source share
2 answers

I would suggest using IoC to abstract the implementation of function flags — all your code needs is something like IFeatures.IsEnabled("FeatureA") lines. When you do this, you can choose the most reasonable implementation - some suggestions below:

  • web.config implementation (compatible with what you have)
  • Database implementation (with cached values, possibly using SqlDependency if you want to work in a web farm)
  • Separate implementation of the configuration file (cached, but using FileSystemWatcher to check for changes in the configuration file and load them without restarting the application pool). This allows you to use the case when you need functions defined before you need your database.
+7
source

You do not need to store function flags in web.config .

The option is to store them in a database - this has the added benefit of working efficiently in a web farm.

Note that with function flags, when you are in a position where the function will be either on or off (say, when switching from widgetA to widgetB, and you no longer need any widgetA code), you must remove the function and the associated flag. This will help in managing the feature set.

+4
source

All Articles