Click here to Skip to main content
14,636,795 members
Articles » Development Lifecycle » Design and Architecture » Patterns and Practices
Posted 21 Feb 2013


30 bookmarked

Provider Pattern for Beginners

Rate this:
4.75 (26 votes)
Please Sign up or sign in to vote.
4.75 (26 votes)
21 Feb 2013CPOL
This article gives the basic understanding of Provider Pattern.
Image 1


Provider pattern allows the developers to create pluggable components. It was first introduced in framework 2.0 and it has lot of features like “Membership Provider”, “Roles Provider” etc. and instantiates using configuration file.

This article provides guidelines to create logging component using provider pattern. The sample project contains two providers “TextLogProvider” and “XmlLogProvider” You can set one of them default in configuration file.

Base Data Provider Class

First of all you need to define abstract representation of all your methods. Create your data provider base class and your class must inherit from System.Configuration.Provider.ProviderBase base class.

public abstract class LogProviderBase : ProviderBase
   public abstract void WriteLog(LogType logType, string message);

As you see LogProviderBase is an abstract class and it has abstract method WriteLog(..). In this example we have only one abstract method. But we can have more than one as per requirement.

Data Provider Class

After creating your base provider class, now you can create your concrete provider classes. In this sample I will create two providers for logging one for text and second for xml. Derive concrete provider class from LogProviderBase class and implement the abstract methods.

public class TextLogProvider : LogProviderBase

   #region Data Members

   private string _filePath = "";


   #region Overrided Methods

   public override void SetParameters(System.Collections.Specialized.NameValueCollection config)
       _filePath = config["fileLocation"];

   public override void WriteLog(LogType logType, string message)
       var dir = Path.GetDirectoryName(_filePath);
       if (!Directory.Exists(dir))

       using (var sw = new StreamWriter(_filePath, true))
           string s = string.Format("{0}, {1}, {2}", DateTime.Now, logType.ToString(), message);



Write the logging logic according to your provider type by implementing WriteLog(…). In TextLogProvider class i am saving CVS line in text file.

Provider Collection & Configuration Section

For taking care of the provider configuration, you must write your own provider collection class derived from System.Configuration.ProviderCollection class. ProviderCollection class exposes properties and methods to work with the list of various data providers declared in your configuration file.

public class LogProviderCollection : ProviderCollection
   new public LogProviderBase this[string name]
       get { return (LogProviderBase)base[name]; }

Provider pattern reads the concrete providers from the configuration file, for this purpose you need one more class which will read all the provider collections from the configuration file. Create ProviderConfiguration class derived from the System.Configuration.ConfigurationSection file.

public class LogProviderConfiguration : ConfigurationSection

   public ProviderSettingsCollection Providers
           return (ProviderSettingsCollection)base["providers"];

   [ConfigurationProperty("default", DefaultValue = "XmlProvider")]
   public string DefaultProviderName
           return base["default"] as string;


In this class you can add as many properties based on the different parameters that you need to extract from the configuration sections. All the properties must be decorated with the ConfigurationProperty attribute.


For configuring the provider model, we need to define our provider configuration section in <configsections>. Here we can add a <section> element with the name of the provider model configuration section element and the type of our data provider configuration class.

After configuring section (we configured LogProviders as section name). Now we need to add our all available providers in it and set one default provider from them.

  <section name="LogProviders"
    type="ProviderPatternLogTest.LogProvider.LogProviderConfiguration, ProviderPatternLogTest"/>

<LogProviders default="XmlProvider">

    <add name="XmlProvider"
      type="ProviderPatternLogTest.LogProvider.Providers.XmlLogProvider, ProviderPatternLogTest"

    <add name="TextProvider"
      type="ProviderPatternLogTest.LogProvider.Providers.TextLogProvider, ProviderPatternLogTest"


Use Provider Model in Code

It is very easy to use provider model in your code, simply get the default property of Provider Manager Class and then call your concrete methods

LogProviderManager.Default.WriteLog(logType, txtMessage.Text);

Get Other Information

You can also easily get other useful information of your concrete provider. Simply get ProviderSetting using LogProviderManager.

var setting = LogProviderManager.ProviderSettings[defaultName];

var setStr = GetSetting(setting);


GetSetting(...) method only parse all the parameters and returns concatenated string message.

private string GetSetting(ProviderSettings setting)
   StringBuilder str = new StringBuilder();
   str.AppendLine(string.Format("Default Provider name: {0}", setting.Name));
   str.AppendLine(string.Format("Default Provider type: {0}", setting.Type));
   foreach (String s in setting.Parameters)

       str.AppendLine(string.Format("Parameter: {0} -> {1}", s, setting.Parameters.Get(s)));

   return str.ToString();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions core (Provider Model) Pin
Member 1417044319-Jun-19 21:23
MemberMember 1417044319-Jun-19 21:23 
QuestionSuggestion Pin
jack.erdey3-Nov-15 10:31
Memberjack.erdey3-Nov-15 10:31 
AnswerRe: Suggestion Pin
tamtbui13-Jun-17 9:41
Membertamtbui13-Jun-17 9:41 
GeneralNice Article Pin
Santhakumar Munuswamy @ Chennai1-May-15 4:51
professionalSanthakumar Munuswamy @ Chennai1-May-15 4:51 
GeneralMy vote of 5 Pin
dev_ranto25-Jul-14 4:28
Memberdev_ranto25-Jul-14 4:28 
QuestionGood post, one question Pin
pompair16-Oct-13 1:20
Memberpompair16-Oct-13 1:20 
GeneralMy vote of 5 Pin
Аslam Iqbal23-Feb-13 22:00
professionalАslam Iqbal23-Feb-13 22:00 
NewsClippingpathservice Pin
peter trego21-Feb-13 21:16
Memberpeter trego21-Feb-13 21:16 
GeneralMy vote of 5 Pin
Member 1051082221-Feb-13 18:53
professionalMember 1051082221-Feb-13 18:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.