An application settings manager that allows you to quickly categorize and manage all those miscellaneous application settings that are clogging up your web. config file. as a bonus, you can let your end users manage their own settings with the supplied editor.
- Download source files-15.8 KB
Introduction
Most applications of any size require application settings. WhileWeb. configFile provides a handy way to store and retrieve application settings usingConfigurationsettings. deleettings ["..."]
Syntax, it can become cumbersome if you have more than a few settings. As well, application settings accessedConfigurationsettings. deleettings
Are all grouped together. It wocould be handy to be able to group them into categories. While this can be done with subsections inWeb. config, It requires some coding. Perhaps most importantly though, it's often a requirement to allow the end user or administrator edit application settings.
This article presents an application settings manager that allows you to quickly and easily manage a large number of application settings. The application settings manager has the following features:
- Settings are grouped by category.
- Easily manages large numbers of settings.
- Settings editor makes it easy for end users to edit the settings.
- Editor runs in configure settings mode. In this mode, new settings are easily created and grouped under categories.
- Settings have descriptions associated with them. The descriptions appear next to the setting at Edit Time to guide the person editing the setting.
- Referring to a setting at runtime follows a similar syntax to referring to
Appsetting
InWeb. config.
- Helper syntax allows you to refer to an integer or bool setting without having to convert it from a string.
Storing and retrieving the settings
The settings are stored in two small SQL tables, settings and settingcategories. The settings table is linear, in that each setting is represented by a record. settings of all types (String
,Int
,Bool
) Are stored as strings inValue
Field in much the same way that settings are all stored as strings inWeb. config. Storing and retrieval of settings is through a set of simple stored procedures. scripts for creating the tables and stored procs is stored ded with the source.
Editing the settings
The settings are edited using a Web user control supplied in the source Appsettings. ascx . The control has two modes, Edit mode And Configure Mode . You can switch between the modes by clicking a button on the form. in edit mode, you can browse through the settings by category and edit the values of the settings. in configure mode, you can add, edit and delete categories; and under each category, you can add, edit and delete settings. when you configure a setting, you can specify the setting type, the number of lines the editor shoshould present if the setting is a text type, and Optionally a list of choices. at Edit Time, the description of the setting will appear next to the edit box to help the person editing the value fill it in correctly. the editor takes a different form, depending on the type of the setting. text settings use a text box. bool settings use a true/False radio button selector. integer settings use a small text box. if you specified a list of choices, the editor will use a combo box from which the person editing can select.
The three screen shots below show the editor idle, in configure mode, and in edit mode:
Application settings
Processing application settings
Editing application settings
Code for the editor
The code for the editor is not very interesting. it's just a lot of fiddling with showing and hiding UI elements to try to create a nice user experience. I won't bore you with going into it here.
Notice at the bottom of the editor there'sLoad settings into ApplicationButton. because the application settings are normally loaded only at application startup, it's necessary to provide this button to re-load them after they 've been edited and saved to the database. the next section covers application settings loading in detail.
Loading application settings
Loading of the application settings is managed by a Class supplied in the code calledAppsettings
. Here is the listing ofAppsettings
Code:
<SPAN class = "CS-comment"> // & lt; summary & gt; </span> <SPAN class = "CS-comment"> // a list of the application settings </span> <SPAN class = "CS-comment"> /// & lt; /Summary & gt; </span> <SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> class </span> etettings: hashtable {<SPAN class = "CS-keyword"> Public </span> etettings () {sqldatareader DR = exclasslib. SQL. sphelper. executereader (configura Tionsettings. appsettings [<SPAN class = "CPP-string"> "connectionstring" </span>], commandtype. storedprocedure, <SPAN class = "CPP-string"> "fx_appsettings_getall" </span>); <SPAN class = "CS-keyword"> while </span> (dr. read () {<SPAN class = "CS-keyword"> string </span> key = (<SPAN class = "CS-keyword"> string </span>) dr [<SPAN class = "CPP-string"> "category" </span>]). toupper () + <SPAN class = "CPP-string"> ". "</SPA N> + (<SPAN class = "CS-keyword"> string </span>) dr [<SPAN class = "CPP-string"> "name" </span>]). toupper (); <SPAN class = "CS-keyword"> string </span> value = (<SPAN class = "CS-keyword"> string </span>) dr [<SPAN class = "CPP-string"> "value" </span>]; add (Key, value);} Dr. close ();} <SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> string </span> <SPAN class = "CS-keyword"> this </span> [<SPAN class = "CS -Keyword "> string </span> name] {<SPAN class =" CS-keyword "> Get </span> {<SPAN class =" CS-keyword "> string </span> key = Name. trim (). toupper (); <SPAN class = "CS-keyword"> If </span> (<SPAN class = "CS-keyword"> base </span> [Key] = <SPAN class = "CS-keyword"> null </span>) {httpcontext. current. response. redirect (<SPAN class = "CPP-string"> "~ /Appsettings. aspx? Badappsetting = "</span> + name ); <SPAN class = "CS-keyword"> return </span> <SPAN class = "CS-keyword"> null </span> ;} <SPAN class = "CS-keyword"> else </span> <SPAN class = "CS-keyword"> return </span> (<SPAN class = "CS-keyword"> string </span>) <SPAN class = "CS-keyword"> base </span> [Key];} <SPAN class = "CS-keyword"> set </span> {<SPAN class = "CS-keyword"> base </span> [name] = value ;}}} |
As you can see, the constructor simply gets the list of settings from the table using the stored procedure for that purpose, and stuffs them intoHashtable
. The key forHashtable
Is the setting category and setting name, separated by a period. We'll use this in a minute when retrieving the app settings.
The other method is"This
"Indexed property, which provides us with a nice syntax for referring to the properties. notice also in the code for this property, there is a check to see if the property exists. if the property doesn't exist, the application is redirectedAppsettings. aspx, And the missing setting is sent in the URL. we'll see in a minute how this helps ensure that all the required settings are available for the application.
Using the application settings
To use the application settings, you need to create an instanceAppsettings
And store it somewhere where it can be globally referred. at exia Corp ., we store it in our framework class. you might put it in global, or some other global class you have. theAppsettings
Class only needs to be instantiated once for the application, so make sure you create it inApplication_start
Event and not on every request.
Next, you simply refer to application settings using the following syntax:
<SPAN class = "CS-keyword"> string </span> XYZ = myglobalclass. appsettings [<SPAN class = "CPP-string"> "category. name "</span>]; |
helper methods : you may want to write three helper methods in your global class so that you can refer to application settings without having to do type conversion all the time. here's the code for the etettingbool
method that you cocould put in your global class that contains the appsettings
:
<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> static </span> <SPAN class = "CS-keyword"> bool </span> appsettingbool (<SPAN class = "CS-keyword"> string </span> name) {<SPAN class = "CS-keyword"> If </span> (appsettings [name] = <SPAN class = "CS-keyword"> null </span>) <SPAN class = "CS-keyword"> return </span> <SPAN class = "CS-keyword"> false </span>; <SPAN class = "CS-keyword"> else </span> <SPAN class = "CS-keyword"> return </span> deleteworkflow [name]. toupper () = <SPAN class = "CPP-string"> "true" </span> ;} |
Now you can write code like this...
<SPAN class = "CS-keyword"> bool </span> ABC = myglobalclass. deleettingbool (<SPAN class = "CPP-string"> "category. name "</span> ); |
... Which is a nice time saver.
Similar methodsAppsetting
AndAppsettingint
Are encoded in the source.
Handling missing app settings
missing or bad application settings are a problem. What happens when a required application setting is missing and the application won't run? Now you can't run the application, so you can't get into the application editor to fix the setting. well, you can always open up the tables and manually adjust the settings, but this is cumbersome. what you need is a standalone form that you know you can always open to adjust the application settings. this form is supplied in the source as your ettings. aspx . the form's purpose is to house the appsettings. ascx control that edits the settings.
Now, look back atAppsettings
Class code listing. remember, we mentioned that the code for retrieving an app setting checks to see if the setting exists. if the setting doesn' t exist, the class is hard-wired to open up this form. this way, if you mess up on a setting, forgot one, or misspelled one, the form will open right away to allow you to adjust the settings.
[Is hard-wiring a middle-tier class to call a form a good idea? Probably not, but in the exia framework, we're so confident thatAppsettings
Form will always be there that we took this as a mulligan. A better method is to reworkAppsettings
Class to raise an event when it encounters a bad setting, and trap the event in your UI Layer.]
If you open the form manually, it will present the editor. however if it's opened by the system, a parameter is passed with the name of the bad setting, and the form presents you with a nice explanatory error message, as shown below:
Application settings editor in Error Mode
Tweaking you'll have to do
In our shop, we use a modified version of the Microsoft Data Access block to call stored procedures. you'll have to do some minor code tweaks to fit this code to your own method of calling stored procs.
Complete Sample Application
The supplied source code contains everything you need to set up the application settings manager. however, it requires a bit of tweaking. for those who want to use it without tweaking, a complete version is available for free download here where it's embedded in the exia web solution framework.
Securing application settings
The simplest way to secure the settings is don't give your end users access to the editor form, or give only administrators access.
If you need more granular control over security of the settings, for instance, if you want to let some groups manage some settings, while other groups manage others, then you might want to consider a guid-based security scheme, such as the one in our exia FX framework. this type of scheme enables you want to secure anything to which a guid can be attached, so you could secure the settings at either the group level or at the individual setting level.
Conclusion
This article has presented a simple, easy to use application settings manager, and a user interface that allows you or your MERs to edit settings easily. as well, the interface makes it easy for you to configure the settings and organize them into groups, and attach descriptions that are useful at Edit Time. with this system, you can clean up a lot of the junk in your web config file, and have easier control over your application settings at the same time.
About Nigel Shaw
|
I grew up in a small town outside Montreal, Canada, where I grew to appreciate the francophone culture, the hospitality of Rural Quebec and the awesome skiing of the Eastern Townships. I studied computer science at the University of Waterloo, classical guitar at the University of Toronto, electrical engineering at the University of Ottawa, and earned an MBA at Queen's University. I own exia Corp, a company based on Ottawa (Canada), that specializes in software application frameworks. our flagship product is the exia web solution framework. click here to view Nigel Shaw's online profile. |