上接乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

简介:
MessageProviderCollection
InBlock.gif using System.Configuration.Provider; 
InBlock.gif using System; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Message的Provider集合类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class MessageProviderCollection : ProviderCollection 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 向集合中添加提供程序。 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="provider">要添加的提供程序。</param> 
InBlock.gif                 public  override  void Add(ProviderBase provider) 
InBlock.gif                { 
InBlock.gif                         if (provider ==  null
InBlock.gif                                 throw  new ArgumentNullException( "provider参数不能为null"); 
InBlock.gif 
InBlock.gif                         if (!(provider  is MessageProvider)) 
InBlock.gif                                 throw  new ArgumentException( "provider参数类型必须是MessageProvider."); 
InBlock.gif 
InBlock.gif                         base.Add(provider); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
MessageProviderConfigurationSection
InBlock.gif using System.Configuration; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Message的Provider的配置 
InBlock.gif         /// </summary> 
InBlock.gif         public  class MessageProviderConfigurationSection : ConfigurationSection 
InBlock.gif        { 
InBlock.gif                 private  readonly ConfigurationProperty _defaultProvider; 
InBlock.gif                 private  readonly ConfigurationProperty _providers; 
InBlock.gif                 private ConfigurationPropertyCollection _properties; 
InBlock.gif                 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 构造函数 
InBlock.gif                 /// </summary> 
InBlock.gif                 public MessageProviderConfigurationSection() 
InBlock.gif                { 
InBlock.gif                        _defaultProvider =  new ConfigurationProperty( "defaultProvider"typeof( string),  null); 
InBlock.gif                        _providers =  new ConfigurationProperty( "providers"typeof(ProviderSettingsCollection),  null); 
InBlock.gif                        _properties =  new ConfigurationPropertyCollection(); 
InBlock.gif 
InBlock.gif                        _properties.Add(_providers); 
InBlock.gif                        _properties.Add(_defaultProvider); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Message的默认的Provider 
InBlock.gif                 /// </summary> 
InBlock.gif                [ConfigurationProperty( "defaultProvider")] 
InBlock.gif                 public  string DefaultProvider 
InBlock.gif                { 
InBlock.gif                        get {  return ( string) base[_defaultProvider]; } 
InBlock.gif                        set {  base[_defaultProvider] = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Message的所有的Provider集合 
InBlock.gif                 /// </summary> 
InBlock.gif                [ConfigurationProperty( "providers", DefaultValue =  "SqlMessageProvider")] 
InBlock.gif                [StringValidator(MinLength = 1)] 
InBlock.gif                 public ProviderSettingsCollection Providers 
InBlock.gif                { 
InBlock.gif                        get {  return (ProviderSettingsCollection) base[_providers]; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// Message的Provider的属性集合 
InBlock.gif                 /// </summary> 
InBlock.gif                 protected  override ConfigurationPropertyCollection Properties 
InBlock.gif                { 
InBlock.gif                        get {  return _properties; } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Message
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Web.Configuration; 
InBlock.gif 
InBlock.gif namespace Pattern.Provider 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// 暴露给客户端用的Message的类(Context) 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Message 
InBlock.gif        { 
InBlock.gif                 private  static  bool m_isInitialized =  false
InBlock.gif                 private  static MessageProviderCollection _providers =  null
InBlock.gif                 private  static MessageProvider _provider =  null
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 静态构造函数,初始化 
InBlock.gif                 /// </summary> 
InBlock.gif                 static Message() 
InBlock.gif                { 
InBlock.gif                        Initialize(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 插入信息 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="mm">Message实体对象</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  static  bool Insert(MessageModel mm) 
InBlock.gif                { 
InBlock.gif                         return _provider.Insert(mm); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 获取信息 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  static List<MessageModel> Get() 
InBlock.gif                { 
InBlock.gif                         return _provider.Get(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  static  void Initialize() 
InBlock.gif                { 
InBlock.gif                         try 
InBlock.gif                        { 
InBlock.gif                                MessageProviderConfigurationSection messageConfig =  null
InBlock.gif 
InBlock.gif                                 if (!m_isInitialized) 
InBlock.gif                                { 
InBlock.gif 
InBlock.gif                                         // 找到配置文件中“MessageProvider”节点 
InBlock.gif                                        messageConfig = (MessageProviderConfigurationSection)ConfigurationManager.GetSection( "MessageProvider"); 
InBlock.gif 
InBlock.gif                                         if (messageConfig ==  null
InBlock.gif                                                 throw  new ConfigurationErrorsException( "在配置文件中没找到“MessageProvider”节点"); 
InBlock.gif 
InBlock.gif                                        _providers =  new MessageProviderCollection(); 
InBlock.gif 
InBlock.gif                                         // 使用System.Web.Configuration.ProvidersHelper类调用每个Provider的Initialize()方法 
InBlock.gif                                        ProvidersHelper.InstantiateProviders(messageConfig.Providers, _providers,  typeof(MessageProvider)); 
InBlock.gif 
InBlock.gif                                         // 所用的Provider为配置中默认的Provider 
InBlock.gif                                        _provider = _providers[messageConfig.DefaultProvider]  as MessageProvider; 
InBlock.gif 
InBlock.gif                                        m_isInitialized =  true
InBlock.gif 
InBlock.gif                                } 
InBlock.gif                        } 
InBlock.gif                         catch (Exception ex) 
InBlock.gif                        { 
InBlock.gif                                 string msg = ex.Message; 
InBlock.gif                                 throw  new Exception(msg); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  static MessageProvider Provider 
InBlock.gif                { 
InBlock.gif                        get 
InBlock.gif                        { 
InBlock.gif                                 return _provider; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  static MessageProviderCollection Providers 
InBlock.gif                { 
InBlock.gif                        get 
InBlock.gif                        { 
InBlock.gif                                 return _providers; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
Web.config
<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
        <section name="MessageProvider" type="Pattern.Provider.MessageProviderConfigurationSection, Pattern.Provider" /> 
    </configSections> 
    <MessageProvider defaultProvider="SqlMessageProvider"> 
        <providers> 
            <add name="XmlMessageProvider" type="Pattern.Provider.XmlMessageProvider, Pattern.Provider" connectionStringName="XmlConnection" /> 
            <add name="SqlMessageProvider" type="Pattern.Provider.SqlMessageProvider, Pattern.Provider" connectionStringName="SqlConnection" /> 
        </providers> 
    </MessageProvider> 
    <connectionStrings> 
        <add name="SqlConnection" connectionString="server=.;database=db;uid=sa;pwd=sa" /> 
        <add name="XmlConnection" connectionString="XmlPath" /> 
    </connectionStrings> 
</configuration>
 
Test
InBlock.gif using System; 
InBlock.gif using System.Data; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gif using Pattern.Provider; 
InBlock.gif 
InBlock.gif public partial  class Provider : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Response.Write(Message.Insert( new MessageModel( "插入", DateTime.Now))); 
InBlock.gif                Response.Write( "<br />"); 
InBlock.gif                Response.Write(Message.Get()[0].Message +  " " + Message.Get()[0].PublishTime.ToString()); 
InBlock.gif        } 
InBlock.gif}
 
 
运行结果
True
SQL方式,连接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44


OK
[源码下载]



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344595,如需转载请自行联系原作者


相关文章
|
18天前
|
设计模式 SQL 算法
设计模式了解哪些,模版模式
设计模式了解哪些,模版模式
19 0
|
1月前
|
设计模式 Java uml
C++设计模式之 依赖注入模式探索
C++设计模式之 依赖注入模式探索
37 0
|
3月前
|
数据采集 API 开发工具
Baumer工业相机堡盟工业相机如何通过NEOAPISDK设置软件触发模式(C#)
Baumer工业相机堡盟工业相机如何通过NEOAPISDK设置软件触发模式(C#)
40 1
|
3月前
|
设计模式 存储 算法
Java 设计模式最佳实践:三、行为模式
Java 设计模式最佳实践:三、行为模式
22 0
|
2月前
|
设计模式 前端开发 JavaScript
观察者模式 vs 发布-订阅模式:两种设计模式的对决!
欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚开始学习前端的读者们打造的。无论你是初学者还是有一些基础的开发者,我们都会在这里为你提供一个系统而又亲切的学习平台。我们以问答形式更新,为大家呈现精选的前端知识点和最佳实践。通过深入浅出的解释概念,并提供实际案例和练习,让你逐步建立起一个扎实的基础。无论是HTML、CSS、JavaScript还是最新的前端框架和工具,我们都将为你提供丰富的内容和实用技巧,帮助你更好地理解并运用前端开发中的各种技术。
|
14天前
|
设计模式 Java 数据库
小谈设计模式(2)—简单工厂模式
小谈设计模式(2)—简单工厂模式
|
2天前
|
设计模式 存储 JavaScript
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
|
2天前
|
设计模式 Java Go
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
|
4天前
|
设计模式
设计模式(一)简单工厂模式
设计模式(一)简单工厂模式
13 0
|
12天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
16 1