http://www.codeproject.com/Articles/18222/Provider-Pattern

Introduction

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

提供者模式,是微软在.net2.0中介绍的最有趣的功能。
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,...

例如,画刷可以将同一张图片保存为多个不同的形式,Gif,Bmp格式等。

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

再如微软的word文档,可以保存为多种形式,doc,rtf,txt……在的工作中,我们经常使用策略模式。

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.

例如,Customers集合在一个确定的上下文中,有相同的方法和行为,一旦上下文变化了,所有的行为就都改变了。

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.

我们有AllCustomers上下文以及InvoiceCustomer,ProductCustomer上下文,每一个都有一个不同的数据库。有时它只是数据库中的一张简单表或者1对多或多对多的关联表。

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.

用户对象有一个叫做Role的属性,根据不同的上下文,属性会变化。

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.

服务提供者应该有相同的功能或者契约,这需要在基类中作为契约来定义,但是有不同的行为,形式,或连通度像web服务、数据库、或者xml文件或流。

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:

为了创建你自己的提供者模式,你必须从System.Configuration.Provider.ProviderBase继承你自己的服务:

Step2: Create Your ServiceCollection

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

为了定义你自己的集合类,你应该从System.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.

在之前的代码中,最重要的代码是Initialize方法。你应该从配置文件中检索出所需的最小信息。

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

Providers给其他在应用程序中注册的providers

Provider Pattern提供者模式和策略模式的更多相关文章

  1. 打造属于你的提供者(Provider = Strategy + Factory Method) 设计模式 - Provider Pattern(提供者模式)

    打造属于你的提供者(Provider = Strategy + Factory Method)   1.1.1 摘要 在日常系统设计中,我们也许听说过提供者模式,甚至几乎每天都在使用它,在.NET F ...

  2. 大型Java进阶专题(七) 设计模式之委派模式与策略模式

    前言 ​ 今天开始我们专题的第七课了.本章节将介绍:你写的代码中是否觉得很臃肿,程序中有大量的if...else,想优化代码,精简程序逻辑,提升代码的可读性,这章节将介绍如何通过委派模式.策略模式让你 ...

  3. Android设计模式之命令模式、策略模式、模板方法模式

    命令模式是其它很多行为型模式的基础模式.策略模式是命令模式的一个特例,而策略模式又和模板方法模式都是算法替换的实现,只不过替换的方式不同.下面来谈谈这三个模式. 命令模式 将一个请求封装为一个对象,从 ...

  4. 我学的是设计模式的视频教程——命令模式vs策略模式,唠嗑

    课程视频 命令模式vs策略模式 唠嗑 课程笔记 课程笔记 课程代码 课程代码 新课程火热报名中 课程介绍 版权声明:本文博主原创文章,博客,未经同意不得转载.

  5. 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  6. ES6对抽象工厂模式与策略模式结合的实践

    这段代码是我在学习了java版的抽象工厂模式后,实现的ES6版抽象工厂,后期大幅修改,加入了策略模式,看起来很多逻辑看似繁琐,不必要写这么多,但是为了练习设计模式,所以才这样做.当所需的工厂种类增多后 ...

  7. C++模式学习------策略模式

    当遇到同一个对象有不同的行为,方法,为管理这些方法可使用策略模式. 策略模式就是对算法进行包装,是把使用算法的责任和算法本身分割开来.通常把一个系列的算法包装到一系列的策略类里面,这些类继承一个抽象的 ...

  8. 【设计模式】 模式PK:命令模式VS策略模式

    1.概述 命令模式和策略模式的类图确实很相似,只是命令模式多了一个接收者(Receiver)角色.它们虽然同为行为类模式,但是两者的区别还是很明显的.策略模式的意图是封装算法,它认为“算法”已经是一个 ...

  9. 策略模式、策略模式与Spring的碰撞

    策略模式是GoF23种设计模式中比较简单的了,也是常用的设计模式之一,今天我们就来看看策略模式. 实际案例 我工作第三年的时候,重构旅游路线的机票查询模块,旅游路线分为四种情况: 如果A地-B地往返都 ...

随机推荐

  1. sql server2000中使用convert来取得datetime数据类型样式(全)

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  2. C++中的lambda表达式

    1.基本形式: [捕获列表](参数列表){函数体};     其中捕获列表和函数体不能省略但是捕获列表可以为空,也就是说最简单的lambda表达式是:  []{}; 2.lambda表达式又叫匿名函数 ...

  3. WPF 多线程处理(6)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...

  4. 3240: [Noi2013]矩阵游戏

    Description 婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的n行m列的矩阵(你不用担心她如何存储).她生成的这个矩阵满足一个神奇的性质:若用F[i][j]来表示矩阵中第i行第j列的 ...

  5. ios Camera学习笔记

    检测设备的摄像头是否可用: - (BOOL) isCameraAvailable{ return [UIImagePickerController isSourceTypeAvailable: UII ...

  6. SqlServer 系统存储过程

    exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns Categories;--查看列exec sp_helpIndex Cat ...

  7. C# 数据结构--排序[上]

    概述 看了几天的排序内容,现在和大家分享一些常见的排序方法. 啥是排序? 个人理解的排序:通过对数组中的值进行对比,交换位置最终得到一个有序的数组.排序分为内存排序和外部排序.本次分享排序方法都为内存 ...

  8. 剑指offer--面试题15--相关

    感受:清晰的思路是最重要的!!! 题目:求链表中间节点 ListNode* MidNodeInList(ListNode* pHead) { if(pHead == NULL) return NULL ...

  9. uniqueidentifier 数据类型(转)

     想要产生这种唯一标识的格式的数据: 6F9619FF-8B86-D011-B42D-00C04FC964FF 应该怎么做呢?答: uniqueidentifier 数据类型可存储 16 字节的二进制 ...

  10. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...