Custom configuration settings are a great way to setup features that drive the web site. You can create custom collections in your configuration site, similar to how connectionStrings and appSettings work. You will find a need to add multiple items to a section in your custom configuration section, and using this approach will help. This approach utilizes two new classes: the ConfigurationElementCollection and ConfigurationCollectionAttribute class.
Working with the Collection
The ConfigurationElementCollection is the collection that works with modifying items in the configuration. It works with a root ConfigurationElement item, by exposing means to modify the list. The base class has Base methods that actually interact with the list; for instance, the BaseAdd method handles adding the item, the BaseRemove method removes the item from the collection, etc. Anyone that works with collections will see how it varies from the normal methods exposed through a collection. In the class below, the methods exposed interact with the collection.
public class ArticleCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
public ArticleElement this[int index]
{
get { return (ArticleElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
public void Add(ArticleElement element)
{
BaseAdd(element);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new ArticleElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ArticleElement)element).Title;
}
public void Remove(ArticleElement element)
{
BaseRemove(element.Title);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
}
The CollectionType property exposes an enumeration that determines the type of collection structure defined in the configuration file. In this example, I use the add/remove/clear structure everyone is familiar with.
The Custom Element
The actual element used must be a ConfigurationElement derivative. So, the ArticleElement was born. This element uses three properties: Title, Description, and Url, all of which are self-explanatory. SO the article element represents an article that the individual has written, and will be used to render a link on the screen. The construction of this element is the same as the configuration section properties and is illustrated below.
public class ArticleElement : ConfigurationElement
{
public ArticleElement() { }
public ArticleElement(string title, string description)
{
this.Title = title;
this.Description = description;
}
[ConfigurationProperty("Description", IsRequired = true, DefaultValue="No description.")]
public string Description
{
get { return (string)this["Description"]; }
set { this["Description"] = value; }
}
[ConfigurationProperty("Title", IsRequired = true, DefaultValue="Untitled")]
public string Title
{
get { return (string)this["Title"]; }
set { this["Title"] = value; }
}
[ConfigurationProperty("Url", IsRequired = false, DefaultValue = " http://www.dotnetslackers.com")]
public string Url
{
get { return (string)this["Url"]; }
set { this["Url"] = value; }
}
}
Note that it is the exact same as my previous article, using the same attribute constructs. In addition, I expose some properties in a constructor. Note you need a default constructor; the collection class dynamically instantiates this class in the CreateNewElement() method that is overridden.
The Exposure to the Custom Configuration Section
So we have a collection; it has to be exposed somehow. We add a configuration property to the AuthorSection class, and add the ConfigurationCollection attribute. This attribute denotes the type of the collection, as well as allowing you to create the add, clear, and remove key names for the items in the configuration file. For illustrative purposes, I set them to addArticle, clearArticles, and removeArticle.
[
ConfigurationProperty("Articles", IsDefaultCollection=false),
ConfigurationCollection(typeof(ArticleCollection), AddItemName="addArticle", ClearItemsName="clearArticles", RemoveItemName="removeArticle")
]
public ArticleCollection Articles
{
get { return this["Articles"] as ArticleCollection; }
}
In the test page, I render the articles as links, by looping through the collection. This looping works like any other collection:
foreach (ArticleElement article in section.Articles)
{
Response.Write("<a href='" + article.Url + "' target='_blank'>" + article.Title + "</a> - " + article.Description + "<br>");
}
And, the updated configuration file now looks like this:
<Author Name="Brian Mains" Email=" bgmst5@yahoo.com">
<Articles>
<clearArticles />
<addArticle Url=" http://www.dotnetslackers.com/articles/CustomConfiguration/Custom_Configuration_Sections.aspx" Title="Custom Configuration Sections" Description="This article discusses how to create a custom configuration section" />
<addArticle Title="Custom Configuration Validators" Description="This article discusses how to create a custom configuration validatior" />
<addArticle Title="Custom Configuration Collections" Description="This article discusses how to create a custom configuration collection" />
</Articles>
</Author>
That is all that was needed to create a custom configuration collection.
Summary
This article illustrates how you create a custom configuration section collection, so you can add multiple elements at a time.
References
I derived my example from this example on the MSDN web site.
About Brian Mains
 |
Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare.
In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...
View complete profile
|
Top Articles in this category
2.0 Custom Configuration Sections
.NET 2.0 produced a new way to create custom configuration sections, one that is even easier than before. It involves a custom class that inherits from the ASP.NET ConfigurationSection class, defining usable properties that map to the properties for the configuration element.
Custom Provider Configuration Sections
Each provider you see in the framework has a custom configuration section associated with it, where you can specify the providers that are utilized for it. These providers specify the type of database to connect to, the object that will do the work, etc. We will take a look at one, and how it is instantiated and use in a provider.
Configuration Section Validators
.NET 2.0 has a new means to create custom configuration sections. It also provides a means to validate the content within it, using a custom validator inheriting from ConfigurationValidatorBase. In addition, the validator links to the property in the configuration section through an attribute, which you will see in this article.
|
|
Please login to rate or to leave a comment.