Published: 13 Oct 2006
By: Brian Mains

Custom configuration sections support the use of collections. These collections are similar to the add, remove, and clear sections that you see defined for connectionStrings, appSettings, or other sections.

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.

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.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

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...

This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Custom Provider Configuration Sections
Each provider you see in the framework has a custom configuration section associated with it, where ...
2.0 Custom Configuration Sections
.NET 2.0 produced a new way to create custom configuration sections, one that is even easier than be...
Configuration Section Validators
.NET 2.0 has a new means to create custom configuration sections. It also provides a means to valida...

You might also be interested in the following related blog posts


Introducing RadScrollablePanel for Windows Forms read more
Introducing SharePoint 2010 Training at U2U read more
Designer Support for One-Way navigations in Entity Framework 4 read more
How To: Silverlight grid hierarchy load on demand using MVVM and RIA services read more
Migrated from Community Server to DasBlog read more
Free software for you! WebsiteSpark let the mountain go to Microsoft instead. read more
Scenarios for WS-Passive and OpenID read more
Customizing the SharePoint ECB with Javascript, Part 3 read more
Querying a Multi-Tenant Data Architecture read more
Client Configuration in WCF 4.0 read more
Top
 
 
 

Please login to rate or to leave a comment.