Published: 25 Oct 2006
By: Brian Mains

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.

Introduction

I've talked about custom configuration sections for a while now; now we are going to focus on using them with custom providers. When developing a custom provider, you often need a provision for a <providers> element, where you can specify the provider you want to use for a particular operation. For instance, the <membership> element has a providers collection, and a provision to specify the default provider that does the work, which is used by the Membership class. But how do you then do this in your configuration section? We will look at this next.

Provider Collections

The .NET framework providers (such as MembershipProvider) all define custom configuration sections to specify the properties to use with the provider. For instance, the membership element specifies the connectionStringName to use and all of the rules and regulations that go with membership (how long the passwords are, how strong they need to be, how many times a user can login, etc). When developing custom providers, you need to have these configuration settings for this purpose. How do you set this up with a custom provider?

We will first look at the collections used to expose providers. I created a generic base class that can be used; you provide the type of provider to it, as in the code below:

This makes it simpler to create the actual strongly-typed provider collection. For this article, I changed the way rendering article works; I have an article provider that will be used to retrieve the articles. The custom collection I used to expose this is this class:

Because I used the generic class, I don't need to define anything; it makes defining a provider collection a lot easier, because it will all be defined for us. This collection is exposed through the configuration section, which I defined two extra properties. Below is a portion of the new configuration section:

The providers collection exposes a different provider; it returns a ProviderSettingsCollection collection, which I will show you later how that gets converted to our custom provider collection. This is important though, because this handles the representation in the configuration file for us. We also specify a default provider that is required, which allows us to provide the name of a provider in the collection that will be the one used by default.

Provider Classes

We next need to look at how providers work. There are two classes; one is a base ArticleProvider class, which exposes all of the methods needed for the provider (all detailed provider classes inherit from this). There is also a static class, which exposes all of these methods, plus additional overloaded methods that may be useful. I'm only going to touch the necessities to understand how it works. The abstract base class looks like this:

The GetArticles method is currently the only method defined. Any derived classes inherit from this to provide specific functionality (I have a SqlArticleProvider class defined, but all it does is return a dummy record; in actuality, it would connect to a data store to get the articles). The static class does several things. First, it converts the provider sections over to the actual implementations in our ArticleProviderCollection. Second, it exposes our provider methods statically, so they are accessible throughout. Let's take a look at it; I have a StaticArticles class that is a static class that exposes the ArticleProvider methods. The following is the GetArticles method declaration:

It uses the default provider property to return the GetArticles array. But where is that defined? Statically, in this class as shown below:

The conversion from ProviderSettingsCollection to ArticleProviderCollection is shown below. It uses a helper method to perform the conversion, which makes it easy to do. InitializeProvider is called from the Initialize method shown above, which both are my methods defined in the static class:

Summary

This is how you defined custom configuration sections for custom providers, from the provider section of it. There are more details when implementing a custom provider; however, the focus of this was to look at the provider end. It's actually pretty easy to implement this in your application.

References

See this MSDN Custom Provider article for more information.

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

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 Configuration Collections
Custom configuration sections support the use of collections. These collections are similar to the ...
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


New content providers for the RadEditor and RadFileExplorer controls 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
Client Configuration in WCF 4.0 read more
Why VBA Still Makes Sense read more
Customizing the SharePoint ECB with Javascript, Part 1 read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Article Feedback Kunal Rai 8/25/2009 4:33 AM

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.