MessageProviderCollection
using System.Configuration.Provider;
using System;
namespace Pattern.Provider
{
/// <summary>
/// Message的Provider集合类
/// </summary>
public
class MessageProviderCollection : ProviderCollection
{
/// <summary>
/// 向集合中添加提供程序。
/// </summary>
/// <param name="provider">要添加的提供程序。</param>
public
override
void Add(ProviderBase provider)
{
if (provider ==
null)
throw
new ArgumentNullException(
"provider参数不能为null");
if (!(provider
is MessageProvider))
throw
new ArgumentException(
"provider参数类型必须是MessageProvider.");
base.Add(provider);
}
}
}
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>
/// 构造函数
/// </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的默认的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; }
}
}
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Configuration;
namespace Pattern.Provider
{
/// <summary>
/// 暴露给客户端用的Message的类(Context)
/// </summary>
public
class Message
{
private
static
bool m_isInitialized =
false;
private
static MessageProviderCollection _providers =
null;
private
static MessageProvider _provider =
null;
/// <summary>
/// 静态构造函数,初始化
/// </summary>
static Message()
{
Initialize();
}
/// <summary>
/// 插入信息
/// </summary>
/// <param name="mm">Message实体对象</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”节点
messageConfig = (MessageProviderConfigurationSection)ConfigurationManager.GetSection(
"MessageProvider");
if (messageConfig ==
null)
throw
new ConfigurationErrorsException(
"在配置文件中没找到“MessageProvider”节点");
_providers =
new MessageProviderCollection();
// 使用System.Web.Configuration.ProvidersHelper类调用每个Provider的Initialize()方法
ProvidersHelper.InstantiateProviders(messageConfig.Providers, _providers,
typeof(MessageProvider));
// 所用的Provider为配置中默认的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;
}
}
}
}
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());
}
}


























MessageProviderConfigurationSection























































Message






























































































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>
<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






















运行结果
True
SQL方式,连接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44
OK
[源码下载]
True
SQL方式,连接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44
OK
[源码下载]
本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344595,如需转载请自行联系原作者