I am trying to eliminate if not ALL as many hardcodes as possible in our coding practices. I have eliminated about 70% just by moving to constants so at least they are centrally located. I then moved to populating the constants from the Web Config in the
appSettings section. I then came up with this design which I now present for critical review.
I created a static class called SessionConstants and in the constructor I loop through appSettings for all the keys and values. I then try to assign the property the value from the NameValueCollection using the Get method....but the Get method requires a
string...so I'm back to having a hard code. What I was thinking would be cool is if I could use the properties name as the string value to be matched in the NameValue collection. So if my property had the exact same name as the web config appSetting key then
a property could be populated by it's matching entry in the web config.
If this is possible I would appreciate knowing how to get the propertyName to be used as the string value for the get method of the NameValueCollection. If this is a bad idea let fly on why. Unlike 90% of society I won't be offended....I will be grateful.
What I was thinking would be cool is if I could use the properties name as the string value to be matched in the NameValue collection. So if my property had the exact same name as the web config appSetting key then a property could be populated by it's matching
entry in the web config
Why not use a configSection instead? There you create the class you need to hold your configuration settings and have that class load its setting from its configSection in you config file.
ramjet69
Member
110 Points
85 Posts
Futile Attempt to Remove All Hardcodes?
Nov 03, 2011 06:23 PM|LINK
Hello,
I am trying to eliminate if not ALL as many hardcodes as possible in our coding practices. I have eliminated about 70% just by moving to constants so at least they are centrally located. I then moved to populating the constants from the Web Config in the appSettings section. I then came up with this design which I now present for critical review.
I created a static class called SessionConstants and in the constructor I loop through appSettings for all the keys and values. I then try to assign the property the value from the NameValueCollection using the Get method....but the Get method requires a string...so I'm back to having a hard code. What I was thinking would be cool is if I could use the properties name as the string value to be matched in the NameValue collection. So if my property had the exact same name as the web config appSetting key then a property could be populated by it's matching entry in the web config.
If this is possible I would appreciate knowing how to get the propertyName to be used as the string value for the get method of the NameValueCollection. If this is a bad idea let fly on why. Unlike 90% of society I won't be offended....I will be grateful.
JB
SGWellens
All-Star
126029 Points
10309 Posts
Moderator
Re: Futile Attempt to Remove All Hardcodes?
Nov 03, 2011 08:42 PM|LINK
You really don't need a static class. Web.Config is cached.
http://weblogs.asp.net/stevewellens/archive/2011/01/15/web-config-is-cached.aspx
My blog
toas1
Participant
1559 Points
345 Posts
Re: Futile Attempt to Remove All Hardcodes?
Nov 04, 2011 06:54 AM|LINK
Why not use a configSection instead? There you create the class you need to hold your configuration settings and have that class load its setting from its configSection in you config file.
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
ramjet69
Member
110 Points
85 Posts
Re: Futile Attempt to Remove All Hardcodes?
Nov 04, 2011 10:02 AM|LINK
That's cool to know. I thought it was a disk I/O to read the web config.
ramjet69
Member
110 Points
85 Posts
Re: Futile Attempt to Remove All Hardcodes?
Nov 04, 2011 01:31 PM|LINK
I just read this.
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
PERFECT!