Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

Provider Pattern

Rate me:
Please Sign up or sign in to vote.
3.84/5 (14 votes)
31 Mar 2007CPOL3 min read 107.4K   1.2K   67  
Create your own custom providers
using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;

namespace ConfigActivities {
    public class MyProviderManager {
        //Initialization related variables and logic
        private static bool isInitialized = false;
        private static Exception initializationException;

        private static object initializationLock = new object();

        static MyProviderManager() {
            Initialize();
            }

        private static void Initialize() {

            try {
                //Get the feature's configuration info
                MyProviderConfiguration qc =
                    (MyProviderConfiguration)ConfigurationManager.GetSection("MyProvider");

                if (qc.DefaultProvider == null || qc.Providers == null || qc.Providers.Count < 1)
                    throw new ProviderException("You must specify a valid default provider.");

                //Instantiate the providers
                providerCollection = new MyProviderProviderCollection();
                ProvidersHelper.InstantiateProviders(qc.Providers, providerCollection, typeof(MyProviderProvider));
                providerCollection.SetReadOnly();
                defaultProvider = providerCollection[qc.DefaultProvider];
                if (defaultProvider == null) {
                    throw new ConfigurationErrorsException(
                        "You must specify a default provider for the feature.",
                        qc.ElementInformation.Properties["defaultProvider"].Source,
                        qc.ElementInformation.Properties["defaultProvider"].LineNumber);
                    }
                }
            catch (Exception ex) {
                initializationException = ex;
                isInitialized = true;
                throw ex;
                }

            isInitialized = true; //error-free initialization
            }

        //Public feature API
        private static MyProviderProvider defaultProvider;
        private static MyProviderProviderCollection providerCollection;

        public static MyProviderProvider Provider {
            get {
                return defaultProvider;
                }
            }

        public static MyProviderProviderCollection Providers {
            get {
                return providerCollection;
                }
            }

        public static string DoWork() {
            return Provider.DoWork();
            }
        }
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) NSW Curriculum & Learning Innovation Centre
Australia Australia
I am a senior developer self taught,
the main study is electronics and communication engineering

I am working as senior programmer in center for learning and innovation
www.cli.nsw.edu.au

I develop Software since 1995 using Delphi with Microsoft SQL Server

before 2000, I didn’t like Microsoft tools and did many projects using all database tools and other programming tools specially Borland C++ Builder, Delphi, Power Builder
And I loved Oracle database for its stability until I was certified as Master in Database Administration from Oracle University.

I tried to work in web programming but I felt that Java is hard and slow in programming, specially I love productivity.

I began worked with .Net since 2001 , and at the same time Microsoft SQL Server 7 was very stable so I switched all my way to Microsoft Tech.
I really respect .Net Platform especially in web applications

I love database Applications too much
And built library with PowerBuilder it was very useful for me and other developers

I have a wide experience due to my work in different companies
But the best experience I like in wireless applications, and web applications.
The best Application I did in my life is Novartis Marketing System 1999 it takes 8 months developing with PowerBuilder and Borland C++, SQL Server
Performance was the key challenge in this Application.
The other 2 applications that I loved Multilingual Project in Scada company in Italy 2000 and SDP Mobile media content platform server for ChinaUnicom 2004
I hope that you enjoy any Article I post.
God bless you.

Comments and Discussions