How creating a configuration class speedup your c# web application ?
I was creating an application with n-tier design in mind. Actually this was just a conceptual n-tier, it would be more appropriate if I call it n-layered application as the physical deployment was at a single server.
I followed the traditional approach and started with the Web.config file.
<span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><?<span style="color: #a31515;">xml <span style="color: blue;"><span style="color: red;">version<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">1.0<span style="color: black;">"<span style="color: blue;">?> </span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">configuration<span style="color: blue;">> </span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"> <<span style="color: #a31515;">system.web<span style="color: blue;">> </span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"> <<span style="color: #a31515;">compilation<span style="color: blue;"> <span style="color: red;">debug<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">true<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">targetFramework<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">4.0<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"> </<span style="color: #a31515;">system.web<span style="color: blue;">></span></span></span> <span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">connectionStrings<span style="color: blue;">> </span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">name<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">BSConnection<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">connectionString<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">Data Source=BINOYWILSON;Initial Catalog=BS;Persist Security Info=True;User ID=BS;Password=ecomm<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">providerName<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">System.Data.SqlClient<span style="color: black;">"<span style="color: blue;"> /> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"> </<span style="color: #a31515;">connectionStrings<span style="color: blue;">></span></span></span> <span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"> <<span style="color: #a31515;">appSettings<span style="color: blue;">></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">MailServer<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">smtp.gmail.com<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">MailUsername<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">[email protected]<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">MailPassword<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">226546553<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">MailFrom<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">[email protected]<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">ErrorLogEmail<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">[email protected]<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"><<span style="color: #a31515;">add<span style="color: blue;"> <span style="color: red;">key<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">EnableErroLogEmail<span style="color: black;">"<span style="color: blue;"> <span style="color: red;">value<span style="color: blue;">=<span style="color: black;">"<span style="color: blue;">true<span style="color: black;">"<span style="color: blue;">/> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;"></<span style="color: #a31515;">appSettings<span style="color: blue;">> </span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt;"><span style="background-color: white;"></<span style="color: #a31515;">configuration<span style="color: blue;">></span></span></span> </span>
While creating this project there were two things in my mind
1. Database agnostic DAL (Data Access layer)
2. Speed up the access from the webconfig file as it is an XML
Someone suggested me that I should go ahead and create a configuration class that would support my second goal. I just thought of doing some RnD regarding this. I finally ended up creating a class named BSconfig.cs .
The role of this class that I created would be to act as the collection of static properties that I had stored in the Web.config file. Following are the reasons that I found would support the suggestion that I got:
- Now instead of reading the Web.config all the time I just could use the properties of the BSconfig class and avoid reading the Web.config each and everytime. In the long run this can be very useful.
- On every request made the Web.config file will not be read. Instead the values that are cached in the BSconfig class are actually used. This is one main reason how performance can be increased.
Following is the code that I used in the BSconfig class:
<span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">using<span style="color: black;"> System; </span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">using<span style="color: black;"> System.Collections.Generic; </span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">using<span style="color: black;"> System.Linq; </span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">using<span style="color: black;"> System.Web; </span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">using<span style="color: black;"> System.Configuration;</span></span> <span style="color: darkgray; font-family: Consolas; font-size: 9pt; background-color: white;">///<span style="color: darkgreen;"> <span style="color: darkgray;"><summary> </span></span></span><span style="color: darkgray; font-family: Consolas; font-size: 9pt; background-color: white;">///<span style="color: darkgreen;"> Repository for balloonShop Configuration settings</span></span><span style="color: darkgray; font-family: Consolas; font-size: 9pt; background-color: white;">///<span style="color: darkgreen;"> <span style="color: darkgray;"></summary> </span></span></span><span style="color: blue; font-family: Consolas; font-size: 9pt; background-color: white;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">class<span style="color: black;"> BalloonShopConfiguration </span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;"> //Caches the connection string </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">private<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> dbConnectionString; </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//Caches the data provider name </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">private<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> dbProviderName; </span></span></span></span></span></span></span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">static<span style="color: black;"> BalloonShopConfiguration() </span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> { </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">dbConnectionString = ConfigurationManager.ConnectionStrings[<span style="color: #a31515;">"BSConnection"<span style="color: black;">].ConnectionString; </span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">dbProviderName = ConfigurationManager.ConnectionStrings[<span style="color: #a31515;">"BSConnection"<span style="color: black;">].ProviderName; </span></span></span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> } </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//returns the connection string for the bs database </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static <span style="color: black;"><span style="color: blue;">string<span style="color: black;"> DbConnectionString </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> dbConnectionString;</span></span></span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> } </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//returns the data provider name </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> DbProviderName </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> dbProviderName; </span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//returns the address of the mail server </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> MailServer </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> ConfigurationManager.AppSettings[<span style="color: #a31515;">"MailServer"<span style="color: black;">]; } </span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> <span style="color: green;">//returns the email username </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> MailUsername </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> ConfigurationManager.AppSettings[<span style="color: #a31515;">"MailUserName"<span style="color: black;">]; </span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//returns the email user password </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> MailPassword </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> ConfigurationManager.AppSettings[<span style="color: #a31515;">"MailPassword"<span style="color: black;">]; </span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">//returns the mail form name </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> MailFrom </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> ConfigurationManager.AppSettings[<span style="color: #a31515;">"MailFrom"<span style="color: black;">]; </span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> <span style="color: green;">// Send error log emails? </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">bool<span style="color: black;"> EnableErrorLogEmail </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> <span style="color: blue;">bool<span style="color: black;">.Parse(ConfigurationManager.AppSettings[<span style="color: #a31515;">"EnableErrorLogEmail"<span style="color: black;">]); </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: green;">// Returns the email address where to send error reports </span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">public<span style="color: black;"> <span style="color: blue;">static<span style="color: black;"> <span style="color: blue;">string<span style="color: black;"> ErrorLogEmail </span></span></span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">{ </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">get</span></span> <span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"> { </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;"><span style="color: blue;">return<span style="color: black;"> ConfigurationManager.AppSettings[<span style="color: #a31515;">"ErrorLogEmail"<span style="color: black;">]; </span></span></span></span></span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span><span style="color: black; font-family: Consolas; font-size: 9pt; background-color: white;">} </span>
If this article has been of any use to you please do comment.