Working with Custom Providers
page 5 of 8
by Brian Mains
Feedback
Average Rating: 
Views (Total / Last 10 Days): 52545/ 65

Part 4: Provider Static Class

The static class is the last part to creating a provider.  It is a key component in exposing your provider to the consumers.  This class pulls together all the parts, by using the configuration section to determine which provider is the default provider, creating the list of providers, and executing the methods that are called in the default provider.  The static class is declared with the static keyword, as all methods should be static in this way.  The static method exposes the provider as such:

Listing 8

public static bool NewsletterExists(string newsletterName)
{
      return Newsletter.DefaultProvider.NewsletterExists(newsletterName);
}

In addition, if you desire to have overloaded methods, to provide only some of the parameters needed, you can do so in the static class, such as the AddNewsletter method:

Listing 9

public static void AddNewsletter(string newsletterName)
{
      Newsletter.AddNewsletter(newsletterName, null);
}
public static void AddNewsletter(string newsletterName, string description)
{
      Newsletter.DefaultProvider.AddNewsletter(newsletterName, description);
}

How does the provider get initialized, as well as determining the default provider?  This occurs in the Initialize and InitializeProviders methods.  But before we look at them, we need to understand their importance.  In essence, the providers and default provider are lazy loaded, meaning they are loaded whenever they are accessed.  The following is the definition for the DefaultProvider and Providers properties.  Note that the Initialize method is called.

Listing 10

public static NewsletterProvider DefaultProvider
{
      get
      {
            Newsletter.Initialize();
            return Newsletter._defaultProvider;
      }
}
public static NewsletterProviderCollection Providers
{
      get
      {
            Newsletter.Initialize();
            return Newsletter._providers;
      }
}

In the initialize method, a boolean value tracks whether this method has been called previously.  If not, then a lock is performed in this method, to ensure that no other instance has done anything with these providers.  This is the reason for the second check; to ensure that the default provider is really null, even after the lock is placed.

Listing 11

private static void Initialize()
{
      if (!_initialized)
      {
            if (_defaultProvider == null)
            {
                  lock(_lock)
                  {
                        //Do this again to make sure provider is still null,
                         //according to MSDN
                        if (_defaultProvider == null)
                              Newsletter.InitializeProviders();
                  }
            }
 
            //Set the initialized to true, so this doesn't occur again
            _initialized = true;
      }
}

In the Initialize method, the call was made to initialize the providers in the providers collection.  The ProviderSettingsCollection needs converted into a collection of the actual provider class type (NewsletterProvider derived classes).  In the code below, I have a helper method of my own to assist with this conversion.  The actual conversion takes place in a single line, using ProvidersHelper.InstantiateProviders(section.Providers, providers, typeof(NewsletterProvider)) method call.  ProvidersHelper is already defined in the framework, whereas ProviderHelper is defined in my application.

Listing 12

private static void InitializeProviders()
{
      NewsletterSection section = NewsletterSection.Instance;
      _providers = ProviderHelper.InitializeProviders<NewsletterProviderCollection,
 NewsletterProvider>(section);
 
      _defaultProvider = _providers[section.DefaultProvider];
      if (_defaultProvider == null)
            throw new ProviderException("The provider couldn't be instantiated");
}

Now we can use our static class to do whatever we need to with this framework.  It can be utilized in a web or windows application, as long as all the appropriate references are met.


View Entire Article

User Comments

Title: Source Code   
Name: Satish Nandigam
Date: 2010-10-27 2:43:31 AM
Comment:
Hi This is a nice article . Can please provide the source code for custom Provider for the Newsletter.
thanks,
N.Satish
Title: Thanks   
Name: Anitha T S
Date: 2010-07-26 7:31:51 AM
Comment:
Thank you very much for your article on providers. Your article is easy to read and understand! I am a better because of it. ;)
Title: Question Reply   
Name: Brian Mains
Date: 2009-10-16 2:53:52 PM
Comment:
Hello,

Yes, DefaultProvider doesn't exist within ConfigurationSection; it exists in my custom base class, which I should have posted, but I didn't. My apologies.

In your custom section class, just add:

[ConfigurationProperty("defaultProvider")]
public string DefaultProvider
{
get { return (string)this["defaultProvider"]; }
}

[
ConfigurationProperty("providers", IsDefaultCollection=false),
ConfigurationCollection(ProviderSettingsCollection)
]
public ProviderSettingsCollection Providers
{
get { return (ProviderSettingsCollection)this["providers"]; }
}

That's what exists in my base class, as a helper. You can also download the project at: http://www.codeplex.com/nucleo, which has these files in Nucleo.dll, in the Nucleo.Providers namespace. I will be posting an update to this project soon with updated AJAX controls, but this code hasn't been touched so it will remain the same, if you are interested.
Title: Question   
Name: Mark Toth
Date: 2009-10-16 2:08:41 PM
Comment:
When I derive a class from ConfigurationSection I get the following error for DefaultProvider "no suitable method found to override". Am I missing something?
Title: Thanks   
Name: Mahr G. Mohyuddin
Date: 2009-04-22 9:21:56 AM
Comment:
Well explained, Brian!. Thanks.
Title: Many Thanks   
Name: Linda
Date: 2009-02-11 9:58:01 AM
Comment:
Thank you very much for your article on providers. Your article is easy to read and understand! I am a better because of it. ;)
Title: still confused reply   
Name: Brian
Date: 2008-08-28 8:50:50 AM
Comment:
The static class is a class separate from the rest of the code, which exposes the provider base class to the public. It's responsible for instantiating it.

SO this is something that should be in the same project as the provider, but is a separate class.
Title: still confused :(   
Name: .
Date: 2008-08-28 3:19:45 AM
Comment:
Would have been nice to be able to download code. I'm at a loss as to where to put the static class - whether I put it in the application which is trying to use the providers, or in the provider code itself as a separate class.
Title: good articles   
Name: I LIKE LT
Date: 2007-09-05 9:16:47 PM
Comment:
very good articles
Title: Good   
Name: Bilal Wani
Date: 2007-03-20 7:17:27 AM
Comment:
Nice Article!!!
Title: Patil   
Name: Sandip
Date: 2007-03-15 5:16:26 PM
Comment:
Nice Article!!!

-Sandip Patil
Title: Good   
Name: Ramamuni Reddy
Date: 2007-02-18 11:25:54 PM
Comment:
Hello Brian Mains,
Very Good Article.

With Regrads
Ramamuni reddy Mulapaku
Title: Provider Utility   
Name: Bilal Hadiar [MVP]
Date: 2007-02-06 6:07:46 AM
Comment:
Hello Brian,
It is a well written article, congratulations!

I would like to refer you and all the readers to a utility I created a while ago that helps you generate the skeleton of a provider files in a single button click,
Check it here:
http://bhaidar.net/cs/archive/2006/07/07/376.aspx

Regards
Title: Mr.   
Name: KotiReddy.
Date: 2007-02-06 12:30:15 AM
Comment:
Very Good Article.


Regards,
Koti Reddy. S






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 12:29:22 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search