Posted January 5, 2009 and filed under Technology    tags:  ,

I have been working on an ASP.NET project the last couple of weeks which would explain the decreased number of posts.  One problem I can’t seem to figure out yet is the best way to store and edit the application configuration data.  I know XML is the medium but how I load the data an interact with the information is the part I can’t resolve.  Also, should I take advantage of the ASP.NET ConfigurationManager mechanisms (OpenMappedWebConfiguration) that allow accessing a separate application config file?  And how should I actually store the data in the xml file?  Key/Value pairs (<name=”” value=””>?  Should I use element attributes (<name value=”” />)?  You get the idea…

My requirements are pretty straight forward but there are a couple of catch points.  Here is the list:

  • Dynamic updates to the file whenever modified in/out of application process
  • Able to store CDATA for values that may contain regular expressions or url addresses
  • Easy to interpret when looking at it outside the configuration screen

That’s it, nothing too terribly difficult.  But no matter how many searches I did on the internet I just didn’t find many options.  Can’t let that stop me, I forged ahead…

XML Format

It quickly becomes obvious that in order to meet my second and third requirement that I need to create an xml file with an element and an innertext value ( ex. <name>value</name> ).  This method allows anyone, if necessary, to easily edit the configuration in notepad.  It also allows for using the WriteCDataElement method to encapsulate certain values.  It also removed one of the options, that where I use the OpenMappedWebConfiguration methods.

XmlDocument Settings = Utils.GetXmlDocument()

How about creating a global XmlDocument object in my Utils class that could be loaded with the configuration during runtime.  This would allow me to cache the load process and prevent trips to the disk if nothing had changed.  Any changes to the xml file would dynamically empty the cache.  This works good with reading data but writing back to the document seemed to be a bit of a hassle.  For example, on an update do I update the entire xml document or just try and update the modified fields?  Do I write the updates into the global variable and then save to disk or do I just write the entire global variable data back into the xml document?  Not completely sure and at this point I was very frustrated with this method.

BlogEngine

What about looking at how BlogEngine handles storing and editing configuration data.  Boy, what effort trying to follow through all the turns and what-not as it dynamically handles different storage providers (not complaining about the code, just my lack of understanding).  A class object includes a Load method in the default constructor method that loads the configuration data during run time.  The load method loads the data into a singleton object, named instance.  The configuration data is preloaded and available by the time the web has initialized.  The configuration data is fully accessible by interacting via ‘value = instance.keyname’ and ‘instance.keyname = value’.

I was able to get this working on my project but something about this procedure bothered me.  The first requirement was not met, as there isn’t any way (unless I missed something… anyone?) to cache the data load and add a dependency to the physical file.

What To Do

It seems to come down to loading the data within a constructor but not being able to monitor for any out-of-band changes to the configuration file, or using a Global object that contains the data but is kind of a pain to write back into when updates are needed.  Can anyone share some knowledge and experience on how best to work with application configuration data?  Perhaps a link or some sample code that could help a fellow out?  In the mean time I will keep looking and testing for the ‘best’ approach…

Comments

Add comment


(Will show your Gravatar icon)

biuquote
Loading