Provider Pattern提供者模式和策略模式



Provider pattern is one of the most interesting features that Microsoft introduced in .NET 2. 

A lot of features including membership providers, roles providers, profile providers, health monitor event providers, site map providers, and more had the same design concept. These features are not changeable but extendable, and that is the beauty of the provider pattern.

许多功能有相同的设计概念,比如membership providers, roles providers, profile providers, health monitor event providers, site map providers等等。这些功能虽然是不变的,但是是可以扩展的,这就是提供者模式的优雅之处。

In this article, I will demonstrate the concept of provider pattern itself, and how easy it is to create your own services designed by provider pattern, and make your services extendable and reusable.



Provider Pattern in More Detail

It is not a brand new idea; it came basically from the popular strategy pattern. It is a good idea to have a quick look at the strategy pattern in here.



I see the strategy pattern as a changeable algorithm depending on different situations or different context. This algorithm is encapsulated in object, this object implements an interface or inherits from an abstracted class.


For example, the Paintbrush saves the same picture in different formats, GIF, BMP,...


Or Microsoft Word saves the document in different formats, doc, rtf, txt,... In my work, we used strategy pattern very often.



Maybe, we will come to the top of our design pattern list, one of the implementations that we used, we used in collection builder to build the same collection in different ways.

也许,我们来到了设计模式列表的顶端,我们使用的一种实现,在collection builder中我们使用不同的方式来创建同一个collection。


For example, Customers collection has the same methods and behavior in a certain context, once the context changes, all the behavior changes.



We have AllCustomers context and InvoiceCustomer context,ProductCustomer context, and each of them has a different database, sometimes it is a simple table in database or 1-many or many-many relation tables in database.



So we create one collection but with different behaviors. I developed an authentication module.


This module is very complex, but after I applied a strategy pattern with composite pattern, it became very simple to maintain, or to extend.


The user object has a property called Role, and depending on the context the Role changes.



The strategy pattern is beyond the scope of this article, but it is worth studying and implementing this pattern.


However, I still can see a few differences between strategy pattern and provider pattern.



Strategy pattern is a Generic concept, not sticking to a specific technology or global scenarios, but provider pattern in most cases is a configurable service, provided by external source, that you can plug-in to your application, or you may extend this service to create your own custom service, then plug-in to your application.


The service provider should have the same features or contract, that is defined in the base class as contract, but with different behaviors, different formats or different connectivity like using web services, database(s) or XML files or streams.




Creating Your Own Provider

There are 3 major steps to create your own provider.  创建你自己的提供者,有3个主要步骤:

Step 1: Create Your ServiceBase

To create your own provider pattern, you must inherit your service fromSystem.Configuration.Provider.ProviderBase class:


Step2: Create Your ServiceCollection

To define your own collection class, you should inherit fromSystem.Configuration.Provider.ProviderCollection.


Step 3: Implement First Concrete Provider

You should to implement at least one of the providers and test it carefully before releasing it in your own company library.


In the previous code, one of the most important codes is Initialize(…) method. You should retrieve the minimum information that should be in the configuration file.


Step 3: Creating ProviderManger

Moreover, to simplify the pattern, create a static class ProviderManager that has two static properties;Provider (for default provider), and Providers (for other providers that are registered in application configuration), to simplify the access in runtime.

此外,为了简化模式,创建一个名为ProviderManager 的静态类,有2个静态属性:Provider 和Providers,确保在运行时可以方便的访问。

Provider 默认的provider


posted @ 2015-05-11 20:36  ChuckLu  阅读(1269)  评论(0编辑  收藏  举报