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.
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.
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.
In the test page, I render the articles as links, by looping through the collection. This looping works like any other collection:
And, the updated configuration file now looks like this:
That is all that was needed to create a custom configuration collection.
This article illustrates how you create a custom configuration section collection, so you can add multiple elements at a time.
I derived my example from this example on the MSDN web site.
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...
This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.
You might also be interested in the following related blog posts
Introducing RadScrollablePanel for Windows Forms
Introducing SharePoint 2010 Training at U2U
Designer Support for One-Way navigations in Entity Framework 4
How To: Silverlight grid hierarchy load on demand using MVVM and RIA services
Migrated from Community Server to DasBlog
Free software for you! WebsiteSpark let the mountain go to Microsoft instead.
Scenarios for WS-Passive and OpenID
Querying a Multi-Tenant Data Architecture
Client Configuration in WCF 4.0
Please login to rate or to leave a comment.