Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Design & Architecture » The .NET 2.0 Framework Provider Pattern

The .NET 2.0 Framework Provider Pattern

The Provider Model Design Pattern was first introduced with the .NET 1.1 framework, specifically in the ASP.NET starter kits and was formalized in ASP.NET Whidbey as a membership management provider API (Application Program Interface). It's primary purpose is to separate the definition for an API from the implementation. This keeps the API flexible by enabling the core functionality to be flexible and easily changed.

Author Rank:
Technologies: .NET 1.0/1.1, ADO.NET, ASP.NET 1.0,Visual C# .NET
Total downloads : 792
Total page views :  34165
Rating :
 4.33/5
This article has been rated :  6 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ProviderSample.zip
 
Become a Sponsor


Related EbooksTop Videos

Part I. Provider Design Pattern Overview

The Provider Model Design Pattern  was first introduced with the .NET 1.1 framework, specifically in the ASP.NET starter kits and was formalized in ASP.NET Whidbey as a membership management provider API (Application Program Interface). It's primary purpose is to separate the definition for an API from the implementation.  This keeps the API flexible by enabling the core functionality to be flexible and easily changed.

More details on the origins of the pattern in the .NET 1.1 Framework can be found on MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp02182004.asp

and:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp02182004.asp

The Provider Model Design Pattern is basically a fusion of the two GOF patterns: strategy and abstract factory.  The API is defined and the functionality is "pluggable" through a variation of the strategy design pattern.  This functionality is loaded into memory through a rough implementation of the abstract factory design pattern.

Here's a basic outline of the parts and how they interact.

  1. API Class:  This is a class that defines and exposes all the desired functionality through static methods.  There is no implementation in the API Class.  The API Class keeps a reference to a Application Provider Base which is basically a wrapper for the API functionality.

    For this article our API Class will be a store from which we can purchase products, specifically Coke, Snickers, and the diet pills that are needed from eating all the Snickers and drinking all the Coke. 


     
    Here are the parts of our class:
     
    1. We'll have API functionality to do basic store stuff like AddProductToStock, GetProductPrice, GetProductStockCount, and RemoveProductFromStock (the core methods)
    2. We have an Initialize() method which is used to load any existing concrete stores from the application configuration.
    3. We have references to all available providers.
    4. We have a reference to the default provider whose functionality is being wrapped.

      Once implemented, all calls will be made to this API class, which will in turn, pass the request to the default provider.

  2. Provider Base Class: This is an internal abstract .NET class in the System.Configuration.Provider namespace that is used to define a provider.  The Initialize() method takes information from the configuration file to construct the concrete provider.  Eventually, we will be overriding the Initialize() method when building our own custom providers by implementing this abstract class.



  3. Application Provider Base: This is an abstract class used to mirror the structure of the API Class, and inherit from the ProviderBase class.  The Application Provider Base inherits from ProviderBase and defines the structure for parent classes by exposing the API methods as abstract members to be implemented by a parent class.

    In our application we'll call the Application Provider Base "StoreProvider".  Notice how the Store Provider class has abstract definitions for the methods defined in the Store class and inherits from the abstract class ProviderBase.



  4. Concrete Provider:  This class is where the methods of the Application Provider Base are implemented.  The Concrete Provider overrides the Initialize() method in order to load information specific to the Concrete Provider from the configuration file.

    So these are the four core classes needed to implement the provider pattern.  The rest of the classes are either used to define what the provider is providing (which, in this case, is a Product class) or utilities for supporting building the objects on the fly or managing the application configuration file.

It takes a bit to wrap one's mind around the basic structure, but once you see how requests are executed against the static Store class and then passed to a reference of a StoreProvider which is implemented by a CornerStoreProvider, you get the basic idea of how the cogs of this machine all work together.

Part II.  Details, Details, Details

Now that we have covered the basics, let's dig a little further into the structure of our program.  Basically we are a building an API for interacting with a store and products within that store.

Let's look at the Product class.


 
This class is not part of the provider pattern, however, we are using it to move data back and forth.  We are defining the name and the wholesale cost for each product.  Each store will have it's own markup on each product so we can calculate the retail cost.   I have also added static methods to get all available products: GetProducts() and Initialize().

The Store class defines our core API for interactions between us (the client), the store and products.


 
The Store Provider exposes these same methods in an abstract class as we discussed earlier.


 
The CornerStoreProvider is our concrete implementation of the StoreProvider.


 
The VendingMachineStoreProvider is another concrete implementation of the StoreProvider. 


 
One interesting method implemented by both the concrete providers is Initialize(). This is the method called from the ProviderBase which gets called from the ProvidersHelper.  This method overrides the ProviderBase implementation and then we are using it to load markup information specific to the store which is in the <store> section of the configuration file.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="store" type="ProviderSample.StoreProviderConfigurationSection,

      ProviderSample, Version=1.0.0.0,

      Culture=neutral,PublicKeyToken=null" />

  </configSections>

  <store defaultProvider="CornerStoreProvider">

    <providers>

      <add name="VendingMachineStoreProvider"

           type="VendingMachineStoreProvider"

           Coke="0.75"

           Snickers="0.5"

           Diet_Pills="1.25"

           ></add>

      <add name="CornerStoreProvider"

           type="CornerStoreProvider"

           Coke="0.70"

           Snickers="0.65"

           Diet_Pills=".80"

           ></add>

    </providers>

  </store>

</configuration>

 

public override void Initialize(string name, NameValueCollection config)

{

    if (string.IsNullOrEmpty(name))

        name = "CornerStoreProvider";

 

    if (null == config)

        throw new ArgumentException("config parameter can not be null");

 

    if (string.IsNullOrEmpty(config["description"]))

    {

        config.Remove("description");

        config.Add("description", "A Corner store from which to get a product");

    }

 

    base.Initialize(name, config);

 

    m_inventory.Clear();

 

    foreach (Product p in Product.GetProducts())

    {

        if (string.IsNullOrEmpty(config[p.Name]))

        {

            m_markup.Add(p, 0);

        }

        else

        {

            m_markup.Add(p, Convert.ToDouble(config[p.Name]));

        }

    }
}

 

There is a  ProvidersHelper  in the System.Web.Configuration namespace, but because this is a windows app, I re-wrote this utility class specifically for windows applications using reflection so we wouldn't need to pull in the entire web namespace just for the one class. ProvidersHelper  is used to instantiate the concrete providers and set values defined in the configuration file.  If you want to implement the Provider pattern in a web application, use System.Web.Configuration.ProvidersHelper instead (it has the same interface so it is easy to swap out).


 
There is also a  StoreProviderConfigurationSection which inherits from System.Configuration.ConfigurationSection  This is used to retrieve data about the Providers and default provider from the configuration file.  


 
Part III. Execution.


To use our API, we'll be calling methods through the static Store object .  In this example, if our default provider has stock of some products we can interface with the default provider through the API definition object (the Store).

 

// Get some coke

Product p1 = Product.GetProduct("Coke");

 

// find out the cost

double cost = Store.GetProductPrice(p1);

 

// get it from the store

Store.RemoveProductFromStock(p1);  // remove from inventory         

If we want a different default provider, we just change the configuration file and basically can have another implementation of the API loaded into memory.  Try running the sample project and change the provider to see how the prices change due to the change in markup values defined in the config file.

<store defaultProvider="CornerStoreProvider">

This could be taken a lot further by not only having different markup prices, but by also having completely different functionality in each provider. 

Part IV. Wrap Up

An example of having completely different functionality exposed through one interface is the difference between the ASP.NET 2.0 System.Web.Security.ActiveDirectoryMembershipProvider and the System.Web.Security.SqlMembershipProvider.  The underlying functionality is completely different for each of these objects, but they are both exposed through the same System.Web.Security.Membership object so there is one easy API to code against. 

Now that we have covered how the Provider Pattern works, you should have enough knowledge to be able to write your own MembershipProvider.  If we wanted to build a MembershipProvider that gets information from an xml file instead of active directory or sql, we could declare the following class definition and implement the abstract methods of the base class.

class XmlMembershipProvider: System.Web.Security.MembershipProvider
{
}

We covered a lot of ground and hopefully you have a bit better understanding of how the Provider pattern works.  In most cases, we'll probably be implementing a custom provider to an existing .NET Framework API instead of rolling our own, so if you are interested in seeing the provider classes that can be customized in ASP.NET 2.0 Framework check out the MSDN article:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ASPNETProvMod_Intro.asp

Until next time,

Happy coding


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Matthew Cochran
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
ProviderSample.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments
Example download by peter On March 10, 2007
The example did not compile - was there a compiler setting required (but not documented) that was required? Error 1 'ProviderSample.ProviderBase.ProviderBase()' must declare a body because it is not marked abstract or extern \_CornerStoreProvider.cs 93 19 ProviderSample I fixed the first set of errors, but finally abandoned the example after more errors surfaced during the compile. Any ideas? Thanks
Reply | Email | Delete | Modify | 
Re: Example download by Mani On April 9, 2007

Just comment the ProviderBase abstract class in _CornerStoreProvider.cs and build the project. This should work fine.

The problem is 'ProviderBase' class is already in System.Configuration.Provider namespace and we should use this.

Thanks,

Manivannan

 

 

Reply | Email | Delete | Modify | 
Re: Re: Example download by jige On June 11, 2007

There still are compilation error as followings:

 

Error 3 Argument '1': cannot convert from 'ProviderSample.ProviderBase' to 'System.Configuration.Provider.ProviderBase' d:\data\859826208\My Documents\Visual Studio Projects\ProviderPattern.TestCase.SalePlatform\ProviderSample\ProviderSample\core\ProvidersHelper.cs 110
Error 1 Cannot convert type 'System.Configuration.Provider.ProviderBase' to 'ProviderSample.StoreProvider' via a built-in conversion d:\data\859826208\My Documents\Visual Studio Projects\ProviderPattern.TestCase.SalePlatform\ProviderSample\ProviderSample\core\Store.cs 83

Reply | Email | Delete | Modify | 
Re: Re: Re: Example download by Adzim On July 9, 2007

To fix the problem you're having, get rid of the ProviderBase that is in _CornerStoreProvider.cs or replace the use of ProviderBase with System.Configuration.Provider.ProviderBase. The error is telling you that you are using the ProviderBase class defined in ProviderSample. Hope this helps.

Adzim

Reply | Email | Delete | Modify | 
Nice article by VB On January 30, 2009
Thanks for great article with explaining simple scenario.
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved