Veritas To continue with the scheme of the last entry, we’re going to again depart from our originally planned series.  Today we’re going to talk about our Blog Configuration.  As we discussed in the initial database design entry, our blog config database table primarily consists of an XML column.  Instead of creating a column for each setting we might want, we’ll store it all dynamically as XML.  Furthermore, there is no way I’m going to think of all the config settings we’ll want while I’m writing this.  Rather then trying to update this entry every time I think of something new, we’ll just outline what we’re going to do in our BlogConfig object so when we decide to add something else down the road, you’ll understand.  First we’ll add a BlogConfig class to the Models folder in our DataLayer.  This will be a partial class and will expand on our already created BlogConfig object (it was created as part of our Entity Framework EDMX):

   1:  namespace Veritas.DataLayer.Models
   2:  {
   3:      public partial class BlogConfig
   4:      {
   6:      }
   7:  }

We’re probably going to end up with dozens of settings (at least) but we’re only going to handle three of them for now.  We’ll add a boolean that will control if comments are allowed on the site, a string to contain the “about” information of the blog, and an int to store the number of posts:

   1:  namespace Veritas.DataLayer.Models
   2:  {
   3:      public partial class BlogConfig
   4:      {
   5:          public bool AllowComments { get; set; }
   6:          public string BlogAbout { get; set; }        
   7:          public int PostCount { get; set; }
   8:      }
   9:  }

Now, since we’re not making columns for any of these, we’re going to need to load them from the xml column whenever we load our BlogConfig object and pull them into the xml when we save it.  Lastly we’ll add methods to load our xml from the properties and to load the properties from the xml:

   1:  public void LoadConfigFromXml()
   2:  {
   3:      XmlDocument doc = new XmlDocument();
   4:      doc.LoadXml(this.ConfigXml);            
   5:      if (doc.DocumentElement["AllowComments"] != null)
   6:          this.AllowComments = Convert.ToBoolean(doc.DocumentElement["AllowComments"].InnerText);
   7:      if (doc.DocumentElement["BlogAbout"] != null)
   8:          this.BlogAbout = doc.DocumentElement["BlogAbout"].InnerText;
   9:      if (doc.DocumentElement["PostCount"] != null)
  10:          this.PostCount = Convert.ToInt32(doc.DocumentElement["PostCount"].InnerText);
  11:  }
  13:  public void BuildXmlFromConfig()
  14:  {
  15:      XElement blogConfigXml = 
  16:          new XElement("BlogConfig",
  17:              new XElement("AllowComments", this.AllowComments),
  18:              new XElement("BlogAbout", this.BlogAbout),
  19:              new XElement("PostCount", this.PostCount)
  20:              );            
  21:      this.ConfigXml = blogConfigXml.ToString().Replace("\r\n  ", "").Replace("\r\n", "");            
  22:  }

As we add (many) more config settings, we’ll have to alter these methods but you get the basic idea of how we’re storing and reading our configuration xml.  I added in unit tests for both of these methods as well.  You can get the latest here.


Chris Risner


Leave a Comment