乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
生活随笔
收集整理的這篇文章主要介紹了
乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原文:樂(lè)在其中設(shè)計(jì)模式(C#) - 提供者模式(Provider Pattern)
作者:webabcd
介紹
為一個(gè)API進(jìn)行定義和實(shí)現(xiàn)的分離。
示例
有一個(gè)Message實(shí)體類(lèi),對(duì)它的操作有Insert()和Get()方法,持久化數(shù)據(jù)在SqlServer數(shù)據(jù)庫(kù)中或Xml文件里。根據(jù)配置文件中的配置來(lái)決定數(shù)據(jù)持久化方案是使用SqlServer數(shù)據(jù)庫(kù)還是Xml文件。
MessageModel
using?System;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message實(shí)體類(lèi)
????///?</summary>
????public?class?MessageModel
????{
????????/**////?<summary>
????????///?構(gòu)造函數(shù)
????????///?</summary>
????????///?<param?name="msg">Message內(nèi)容</param>
????????///?<param?name="pt">Message發(fā)布時(shí)間</param>
????????public?MessageModel(string?msg,?DateTime?pt)
????????{
????????????this._message?=?msg;
????????????this._publishTime?=?pt;
????????}
????????private?string?_message;
????????/**////?<summary>
????????///?Message內(nèi)容
????????///?</summary>
????????public?string?Message
????????{
????????????get?{?return?_message;?}
????????????set?{?_message?=?value;?}
????????}
????????private?DateTime?_publishTime;
????????/**////?<summary>
????????///?Message發(fā)布時(shí)間
????????///?</summary>
????????public?DateTime?PublishTime
????????{
????????????get?{?return?_publishTime;?}
????????????set?{?_publishTime?=?value;?}
????????}
????}
}
MessageProvider
using?System.Configuration.Provider;
using?System.Collections.Generic;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?操作Message抽象類(lèi)
????///?</summary>
????public?abstract?class?MessageProvider?:?ProviderBase
????{
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?abstract?bool?Insert(MessageModel?mm);
????????/**////?<summary>
????????///?獲得Message
????????///?</summary>
????????///?<returns></returns>
????????public?abstract?List<MessageModel>?Get();
????}
}
SqlMessageProvider
using?System;
using?System.Collections.Specialized;
using?System.Collections.Generic;
using?System.Configuration.Provider;
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Sql方式操作Message
????///?</summary>
????public?class?SqlMessageProvider?:?MessageProvider
????{
????????private?string?_connectionString;
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?override?bool?Insert(MessageModel?mm)
????????{
????????????//?代碼略
????????????return?true;
????????}
????????/**////?<summary>
????????///?獲取Message
????????///?</summary>
????????///?<returns></returns>
????????public?override?List<MessageModel>?Get()
????????{
????????????List<MessageModel>?l?=?new?List<MessageModel>();
????????????l.Add(new?MessageModel("SQL方式,連接字符串是"?+?this._connectionString,?DateTime.Now));
????????????return?l;
????????}
????????/**////?<summary>
????????///?初始化提供程序。
????????///?</summary>
????????///?<param?name="name">該提供程序的友好名稱(chēng)。</param>
????????///?<param?name="config">名稱(chēng)/值對(duì)的集合,表示在配置中為該提供程序指定的、提供程序特定的屬性。</param>
????????public?override?void?Initialize(string?name,?NameValueCollection?config)
????????{
????????????if?(string.IsNullOrEmpty(name))
????????????????name?=?"MessageProvider";
????????????if?(null?==?config)
????????????????throw?new?ArgumentException("config參數(shù)不能為null");
????????????if?(string.IsNullOrEmpty(config["description"]))
????????????{
????????????????config.Remove("description");
????????????????config.Add("description",?"SqlServer操作Message");
????????????}
????????????base.Initialize(name,?config);
????????????string?temp?=?config["connectionStringName"];
????????????if?(temp?==?null?||?temp.Length?<?1)
????????????????throw?new?ProviderException("connectionStringName屬性缺少或?yàn)榭?/span>");
????????????_connectionString?=?ConfigurationManager.ConnectionStrings[temp].ConnectionString;
????????????if?(_connectionString?==?null?||?_connectionString.Length?<?1)
????????????{
????????????????throw?new?ProviderException("沒(méi)找到'"?+?temp?+?"'所指的連接字符串,或所指連接字符串為空");
????????????}
????????????config.Remove("connectionStringName");
????????}
????}
}
XmlMessageProvider
using?System;
using?System.Collections.Specialized;
using?System.Collections.Generic;
using?System.Configuration.Provider;
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Xmll方式操作Message
????///?</summary>
????public?class?XmlMessageProvider?:?MessageProvider
????{
????????private?string?_connectionString;
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?override?bool?Insert(MessageModel?mm)
????????{
????????????//?代碼略
????????????return?true;
????????}
????????/**////?<summary>
????????///?獲取Message
????????///?</summary>
????????///?<returns></returns>
????????public?override?List<MessageModel>?Get()
????????{
????????????List<MessageModel>?l?=?new?List<MessageModel>();
????????????l.Add(new?MessageModel("XML方式,連接字符串是"?+?this._connectionString,?DateTime.Now));
????????????return?l;
????????}
????????/**////?<summary>
????????///?初始化提供程序。
????????///?</summary>
????????///?<param?name="name">該提供程序的友好名稱(chēng)。</param>
????????///?<param?name="config">名稱(chēng)/值對(duì)的集合,表示在配置中為該提供程序指定的、提供程序特定的屬性。</param>
????????public?override?void?Initialize(string?name,?NameValueCollection?config)
????????{
????????????if?(string.IsNullOrEmpty(name))
????????????????name?=?"MessageProvider";
????????????if?(null?==?config)
????????????????throw?new?ArgumentException("config參數(shù)不能為null");
????????????if?(string.IsNullOrEmpty(config["description"]))
????????????{
????????????????config.Remove("description");
????????????????config.Add("description",?"XML操作Message");
????????????}
????????????base.Initialize(name,?config);
????????????string?temp?=?config["connectionStringName"];
????????????if?(temp?==?null?||?temp.Length?<?1)
????????????????throw?new?ProviderException("connectionStringName屬性缺少或?yàn)榭?/span>");
????????????_connectionString?=?ConfigurationManager.ConnectionStrings[temp].ConnectionString;
????????????if?(_connectionString?==?null?||?_connectionString.Length?<?1)
????????????{
????????????????throw?new?ProviderException("沒(méi)找到'"?+?temp?+?"'所指的連接字符串,或所指連接字符串為空");
????????????}
????????????config.Remove("connectionStringName");
????????}
????}
}
MessageProviderCollection
using?System.Configuration.Provider;
using?System;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message的Provider集合類(lèi)
????///?</summary>
????public?class?MessageProviderCollection?:?ProviderCollection
????{
????????/**////?<summary>
????????///?向集合中添加提供程序。
????????///?</summary>
????????///?<param?name="provider">要添加的提供程序。</param>
????????public?override?void?Add(ProviderBase?provider)
????????{
????????????if?(provider?==?null)
????????????????throw?new?ArgumentNullException("provider參數(shù)不能為null");
????????????if?(!(provider?is?MessageProvider))
????????????????throw?new?ArgumentException("provider參數(shù)類(lèi)型必須是MessageProvider.");
????????????base.Add(provider);
????????}
????}
}
MessageProviderConfigurationSection
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message的Provider的配置
????///?</summary>
????public?class?MessageProviderConfigurationSection?:?ConfigurationSection
????{
????????private?readonly?ConfigurationProperty?_defaultProvider;
????????private?readonly?ConfigurationProperty?_providers;
????????private?ConfigurationPropertyCollection?_properties;
????????
????????/**////?<summary>
????????///?構(gòu)造函數(shù)
????????///?</summary>
????????public?MessageProviderConfigurationSection()
????????{
????????????_defaultProvider?=?new?ConfigurationProperty("defaultProvider",?typeof(string),?null);
????????????_providers?=?new?ConfigurationProperty("providers",?typeof(ProviderSettingsCollection),?null);
????????????_properties?=?new?ConfigurationPropertyCollection();
????????????_properties.Add(_providers);
????????????_properties.Add(_defaultProvider);
????????}
????????/**////?<summary>
????????///?Message的默認(rèn)的Provider
????????///?</summary>
????????[ConfigurationProperty("defaultProvider")]
????????public?string?DefaultProvider
????????{
????????????get?{?return?(string)base[_defaultProvider];?}
????????????set?{?base[_defaultProvider]?=?value;?}
????????}
????????/**////?<summary>
????????///?Message的所有的Provider集合
????????///?</summary>
????????[ConfigurationProperty("providers",?DefaultValue?=?"SqlMessageProvider")]
????????[StringValidator(MinLength?=?1)]
????????public?ProviderSettingsCollection?Providers
????????{
????????????get?{?return?(ProviderSettingsCollection)base[_providers];?}
????????}
????????/**////?<summary>
????????///?Message的Provider的屬性集合
????????///?</summary>
????????protected?override?ConfigurationPropertyCollection?Properties
????????{
????????????get?{?return?_properties;?}
????????}
????}
}
Message
using?System;
using?System.Collections.Generic;
using?System.Configuration;
using?System.Web.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?暴露給客戶端用的Message的類(lèi)(Context)
????///?</summary>
????public?class?Message
????{
????????private?static?bool?m_isInitialized?=?false;
????????private?static?MessageProviderCollection?_providers?=?null;
????????private?static?MessageProvider?_provider?=?null;
????????/**////?<summary>
????????///?靜態(tài)構(gòu)造函數(shù),初始化
????????///?</summary>
????????static?Message()
????????{
????????????Initialize();
????????}
????????/**////?<summary>
????????///?插入信息
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?static?bool?Insert(MessageModel?mm)
????????{
????????????return?_provider.Insert(mm);
????????}
????????/**////?<summary>
????????///?獲取信息
????????///?</summary>
????????///?<returns></returns>
????????public?static?List<MessageModel>?Get()
????????{
????????????return?_provider.Get();
????????}
????????private?static?void?Initialize()
????????{
????????????try
????????????{
????????????????MessageProviderConfigurationSection?messageConfig?=?null;
????????????????if?(!m_isInitialized)
????????????????{
????????????????????//?找到配置文件中“MessageProvider”節(jié)點(diǎn)
????????????????????messageConfig?=?(MessageProviderConfigurationSection)ConfigurationManager.GetSection("MessageProvider");
????????????????????if?(messageConfig?==?null)
????????????????????????throw?new?ConfigurationErrorsException("在配置文件中沒(méi)找到“MessageProvider”節(jié)點(diǎn)");
????????????????????_providers?=?new?MessageProviderCollection();
????????????????????//?使用System.Web.Configuration.ProvidersHelper類(lèi)調(diào)用每個(gè)Provider的Initialize()方法
????????????????????ProvidersHelper.InstantiateProviders(messageConfig.Providers,?_providers,?typeof(MessageProvider));
????????????????????//?所用的Provider為配置中默認(rèn)的Provider
????????????????????_provider?=?_providers[messageConfig.DefaultProvider]?as?MessageProvider;
????????????????????m_isInitialized?=?true;
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????string?msg?=?ex.Message;
????????????????throw?new?Exception(msg);
????????????}
????????}
????????private?static?MessageProvider?Provider
????????{
????????????get
????????????{
????????????????return?_provider;
????????????}
????????}
????????private?static?MessageProviderCollection?Providers
????????{
????????????get
????????????{
????????????????return?_providers;
????????????}
????????}
????}
}
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
using?System;
using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?Pattern.Provider;
public?partial?class?Provider?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????Response.Write(Message.Insert(new?MessageModel("插入",?DateTime.Now)));
????????Response.Write("<br?/>");
????????Response.Write(Message.Get()[0].Message?+?"?"?+?Message.Get()[0].PublishTime.ToString());
????}
}
運(yùn)行結(jié)果
True
SQL方式,連接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44
OK
[源碼下載]
[索引頁(yè)]
[源碼下載]
作者:webabcd
介紹
為一個(gè)API進(jìn)行定義和實(shí)現(xiàn)的分離。
示例
有一個(gè)Message實(shí)體類(lèi),對(duì)它的操作有Insert()和Get()方法,持久化數(shù)據(jù)在SqlServer數(shù)據(jù)庫(kù)中或Xml文件里。根據(jù)配置文件中的配置來(lái)決定數(shù)據(jù)持久化方案是使用SqlServer數(shù)據(jù)庫(kù)還是Xml文件。
MessageModel
using?System;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message實(shí)體類(lèi)
????///?</summary>
????public?class?MessageModel
????{
????????/**////?<summary>
????????///?構(gòu)造函數(shù)
????????///?</summary>
????????///?<param?name="msg">Message內(nèi)容</param>
????????///?<param?name="pt">Message發(fā)布時(shí)間</param>
????????public?MessageModel(string?msg,?DateTime?pt)
????????{
????????????this._message?=?msg;
????????????this._publishTime?=?pt;
????????}
????????private?string?_message;
????????/**////?<summary>
????????///?Message內(nèi)容
????????///?</summary>
????????public?string?Message
????????{
????????????get?{?return?_message;?}
????????????set?{?_message?=?value;?}
????????}
????????private?DateTime?_publishTime;
????????/**////?<summary>
????????///?Message發(fā)布時(shí)間
????????///?</summary>
????????public?DateTime?PublishTime
????????{
????????????get?{?return?_publishTime;?}
????????????set?{?_publishTime?=?value;?}
????????}
????}
}
MessageProvider
using?System.Configuration.Provider;
using?System.Collections.Generic;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?操作Message抽象類(lèi)
????///?</summary>
????public?abstract?class?MessageProvider?:?ProviderBase
????{
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?abstract?bool?Insert(MessageModel?mm);
????????/**////?<summary>
????????///?獲得Message
????????///?</summary>
????????///?<returns></returns>
????????public?abstract?List<MessageModel>?Get();
????}
}
SqlMessageProvider
using?System;
using?System.Collections.Specialized;
using?System.Collections.Generic;
using?System.Configuration.Provider;
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Sql方式操作Message
????///?</summary>
????public?class?SqlMessageProvider?:?MessageProvider
????{
????????private?string?_connectionString;
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?override?bool?Insert(MessageModel?mm)
????????{
????????????//?代碼略
????????????return?true;
????????}
????????/**////?<summary>
????????///?獲取Message
????????///?</summary>
????????///?<returns></returns>
????????public?override?List<MessageModel>?Get()
????????{
????????????List<MessageModel>?l?=?new?List<MessageModel>();
????????????l.Add(new?MessageModel("SQL方式,連接字符串是"?+?this._connectionString,?DateTime.Now));
????????????return?l;
????????}
????????/**////?<summary>
????????///?初始化提供程序。
????????///?</summary>
????????///?<param?name="name">該提供程序的友好名稱(chēng)。</param>
????????///?<param?name="config">名稱(chēng)/值對(duì)的集合,表示在配置中為該提供程序指定的、提供程序特定的屬性。</param>
????????public?override?void?Initialize(string?name,?NameValueCollection?config)
????????{
????????????if?(string.IsNullOrEmpty(name))
????????????????name?=?"MessageProvider";
????????????if?(null?==?config)
????????????????throw?new?ArgumentException("config參數(shù)不能為null");
????????????if?(string.IsNullOrEmpty(config["description"]))
????????????{
????????????????config.Remove("description");
????????????????config.Add("description",?"SqlServer操作Message");
????????????}
????????????base.Initialize(name,?config);
????????????string?temp?=?config["connectionStringName"];
????????????if?(temp?==?null?||?temp.Length?<?1)
????????????????throw?new?ProviderException("connectionStringName屬性缺少或?yàn)榭?/span>");
????????????_connectionString?=?ConfigurationManager.ConnectionStrings[temp].ConnectionString;
????????????if?(_connectionString?==?null?||?_connectionString.Length?<?1)
????????????{
????????????????throw?new?ProviderException("沒(méi)找到'"?+?temp?+?"'所指的連接字符串,或所指連接字符串為空");
????????????}
????????????config.Remove("connectionStringName");
????????}
????}
}
XmlMessageProvider
using?System;
using?System.Collections.Specialized;
using?System.Collections.Generic;
using?System.Configuration.Provider;
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Xmll方式操作Message
????///?</summary>
????public?class?XmlMessageProvider?:?MessageProvider
????{
????????private?string?_connectionString;
????????/**////?<summary>
????????///?插入Message
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?override?bool?Insert(MessageModel?mm)
????????{
????????????//?代碼略
????????????return?true;
????????}
????????/**////?<summary>
????????///?獲取Message
????????///?</summary>
????????///?<returns></returns>
????????public?override?List<MessageModel>?Get()
????????{
????????????List<MessageModel>?l?=?new?List<MessageModel>();
????????????l.Add(new?MessageModel("XML方式,連接字符串是"?+?this._connectionString,?DateTime.Now));
????????????return?l;
????????}
????????/**////?<summary>
????????///?初始化提供程序。
????????///?</summary>
????????///?<param?name="name">該提供程序的友好名稱(chēng)。</param>
????????///?<param?name="config">名稱(chēng)/值對(duì)的集合,表示在配置中為該提供程序指定的、提供程序特定的屬性。</param>
????????public?override?void?Initialize(string?name,?NameValueCollection?config)
????????{
????????????if?(string.IsNullOrEmpty(name))
????????????????name?=?"MessageProvider";
????????????if?(null?==?config)
????????????????throw?new?ArgumentException("config參數(shù)不能為null");
????????????if?(string.IsNullOrEmpty(config["description"]))
????????????{
????????????????config.Remove("description");
????????????????config.Add("description",?"XML操作Message");
????????????}
????????????base.Initialize(name,?config);
????????????string?temp?=?config["connectionStringName"];
????????????if?(temp?==?null?||?temp.Length?<?1)
????????????????throw?new?ProviderException("connectionStringName屬性缺少或?yàn)榭?/span>");
????????????_connectionString?=?ConfigurationManager.ConnectionStrings[temp].ConnectionString;
????????????if?(_connectionString?==?null?||?_connectionString.Length?<?1)
????????????{
????????????????throw?new?ProviderException("沒(méi)找到'"?+?temp?+?"'所指的連接字符串,或所指連接字符串為空");
????????????}
????????????config.Remove("connectionStringName");
????????}
????}
}
MessageProviderCollection
using?System.Configuration.Provider;
using?System;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message的Provider集合類(lèi)
????///?</summary>
????public?class?MessageProviderCollection?:?ProviderCollection
????{
????????/**////?<summary>
????????///?向集合中添加提供程序。
????????///?</summary>
????????///?<param?name="provider">要添加的提供程序。</param>
????????public?override?void?Add(ProviderBase?provider)
????????{
????????????if?(provider?==?null)
????????????????throw?new?ArgumentNullException("provider參數(shù)不能為null");
????????????if?(!(provider?is?MessageProvider))
????????????????throw?new?ArgumentException("provider參數(shù)類(lèi)型必須是MessageProvider.");
????????????base.Add(provider);
????????}
????}
}
MessageProviderConfigurationSection
using?System.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?Message的Provider的配置
????///?</summary>
????public?class?MessageProviderConfigurationSection?:?ConfigurationSection
????{
????????private?readonly?ConfigurationProperty?_defaultProvider;
????????private?readonly?ConfigurationProperty?_providers;
????????private?ConfigurationPropertyCollection?_properties;
????????
????????/**////?<summary>
????????///?構(gòu)造函數(shù)
????????///?</summary>
????????public?MessageProviderConfigurationSection()
????????{
????????????_defaultProvider?=?new?ConfigurationProperty("defaultProvider",?typeof(string),?null);
????????????_providers?=?new?ConfigurationProperty("providers",?typeof(ProviderSettingsCollection),?null);
????????????_properties?=?new?ConfigurationPropertyCollection();
????????????_properties.Add(_providers);
????????????_properties.Add(_defaultProvider);
????????}
????????/**////?<summary>
????????///?Message的默認(rèn)的Provider
????????///?</summary>
????????[ConfigurationProperty("defaultProvider")]
????????public?string?DefaultProvider
????????{
????????????get?{?return?(string)base[_defaultProvider];?}
????????????set?{?base[_defaultProvider]?=?value;?}
????????}
????????/**////?<summary>
????????///?Message的所有的Provider集合
????????///?</summary>
????????[ConfigurationProperty("providers",?DefaultValue?=?"SqlMessageProvider")]
????????[StringValidator(MinLength?=?1)]
????????public?ProviderSettingsCollection?Providers
????????{
????????????get?{?return?(ProviderSettingsCollection)base[_providers];?}
????????}
????????/**////?<summary>
????????///?Message的Provider的屬性集合
????????///?</summary>
????????protected?override?ConfigurationPropertyCollection?Properties
????????{
????????????get?{?return?_properties;?}
????????}
????}
}
Message
using?System;
using?System.Collections.Generic;
using?System.Configuration;
using?System.Web.Configuration;
namespace?Pattern.Provider
{
????/**////?<summary>
????///?暴露給客戶端用的Message的類(lèi)(Context)
????///?</summary>
????public?class?Message
????{
????????private?static?bool?m_isInitialized?=?false;
????????private?static?MessageProviderCollection?_providers?=?null;
????????private?static?MessageProvider?_provider?=?null;
????????/**////?<summary>
????????///?靜態(tài)構(gòu)造函數(shù),初始化
????????///?</summary>
????????static?Message()
????????{
????????????Initialize();
????????}
????????/**////?<summary>
????????///?插入信息
????????///?</summary>
????????///?<param?name="mm">Message實(shí)體對(duì)象</param>
????????///?<returns></returns>
????????public?static?bool?Insert(MessageModel?mm)
????????{
????????????return?_provider.Insert(mm);
????????}
????????/**////?<summary>
????????///?獲取信息
????????///?</summary>
????????///?<returns></returns>
????????public?static?List<MessageModel>?Get()
????????{
????????????return?_provider.Get();
????????}
????????private?static?void?Initialize()
????????{
????????????try
????????????{
????????????????MessageProviderConfigurationSection?messageConfig?=?null;
????????????????if?(!m_isInitialized)
????????????????{
????????????????????//?找到配置文件中“MessageProvider”節(jié)點(diǎn)
????????????????????messageConfig?=?(MessageProviderConfigurationSection)ConfigurationManager.GetSection("MessageProvider");
????????????????????if?(messageConfig?==?null)
????????????????????????throw?new?ConfigurationErrorsException("在配置文件中沒(méi)找到“MessageProvider”節(jié)點(diǎn)");
????????????????????_providers?=?new?MessageProviderCollection();
????????????????????//?使用System.Web.Configuration.ProvidersHelper類(lèi)調(diào)用每個(gè)Provider的Initialize()方法
????????????????????ProvidersHelper.InstantiateProviders(messageConfig.Providers,?_providers,?typeof(MessageProvider));
????????????????????//?所用的Provider為配置中默認(rèn)的Provider
????????????????????_provider?=?_providers[messageConfig.DefaultProvider]?as?MessageProvider;
????????????????????m_isInitialized?=?true;
????????????????}
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????string?msg?=?ex.Message;
????????????????throw?new?Exception(msg);
????????????}
????????}
????????private?static?MessageProvider?Provider
????????{
????????????get
????????????{
????????????????return?_provider;
????????????}
????????}
????????private?static?MessageProviderCollection?Providers
????????{
????????????get
????????????{
????????????????return?_providers;
????????????}
????????}
????}
}
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
using?System;
using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?Pattern.Provider;
public?partial?class?Provider?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????Response.Write(Message.Insert(new?MessageModel("插入",?DateTime.Now)));
????????Response.Write("<br?/>");
????????Response.Write(Message.Get()[0].Message?+?"?"?+?Message.Get()[0].PublishTime.ToString());
????}
}
運(yùn)行結(jié)果
True
SQL方式,連接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44
OK
[源碼下載]
總結(jié)
以上是生活随笔為你收集整理的乐在其中设计模式(C#) - 提供者模式(Provider Pattern)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 百词斩app怎么估分(汉典百字的基本解释
- 下一篇: 逆战空间撕裂者怎么用 逆战官方网站