提供者模式(Provider Pattern)


提供者模式(Provider Pattern)

1.jpg.png
介绍
为一个API进行定义和实现的分离。


示例
有一个Message实体类,对它的操作有Insert()和Get()方法,持久化数据在SqlServer数据库中或Xml文件里。根据配置文件中的配置来决定数据持久化方案是使用SqlServer数据库还是Xml文件。

MessageModel

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Message实体类
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class MessageModel
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="msg">Message内容</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="pt">Message发布时间</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageModel(string msg, DateTime pt)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._message = msg;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
this._publishTime = pt;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private string _message;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message内容
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public string Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _message; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _message = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private DateTime _publishTime;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message发布时间
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public DateTime PublishTime
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _publishTime; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { _publishTime = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


MessageProvider

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System.Configuration.Provider;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// 操作Message抽象类
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public abstract class MessageProvider : ProviderBase
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public abstract bool Insert(MessageModel mm);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获得Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public abstract List<MessageModel> Get();
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


SqlMessageProvider

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Specialized;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration.Provider;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Sql方式操作Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class SqlMessageProvider : MessageProvider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private string _connectionString;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
// 代码略
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            return true;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            List<MessageModel> l = 
new List<MessageModel>();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            l.Add(
new MessageModel("SQL方式,连接字符串是" + this._connectionString, DateTime.Now));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return l;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 初始化提供程序。
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="name">该提供程序的友好名称。</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="config">名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override void Initialize(string name, NameValueCollection config)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (string.IsNullOrEmpty(name))
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                name = "MessageProvider";
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (null == config)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ArgumentException("config参数不能为null");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (string.IsNullOrEmpty(config["description"]))
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                config.Remove("description");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                config.Add("description", "SqlServer
操作Message");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
base.Initialize(name, config);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
string temp = config["connectionStringName"];
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (temp == null || temp.Length < 1)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ProviderException("connectionStringName属性缺少或为空");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _connectionString = ConfigurationManager.ConnectionStrings[temp].ConnectionString;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (_connectionString == null || _connectionString.Length < 1)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ProviderException("没找到'" + temp + "'所指的连接字符串,或所指连接字符串为空");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            config.Remove("connectionStringName");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


XmlMessageProvider

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Specialized;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration.Provider;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// Xmll方式操作Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class XmlMessageProvider : MessageProvider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private string _connectionString;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
// 代码略
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
            return true;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取Message
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            List<MessageModel> l = 
new List<MessageModel>();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            l.Add(
new MessageModel("XML方式,连接字符串是" + this._connectionString, DateTime.Now));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return l;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 初始化提供程序。
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="name">该提供程序的友好名称。</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="config">名称/值对的集合,表示在配置中为该提供程序指定的、提供程序特定的属性。</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override void Initialize(string name, NameValueCollection config)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (string.IsNullOrEmpty(name))
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                name = "MessageProvider";
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (null == config)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ArgumentException("config参数不能为null");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (string.IsNullOrEmpty(config["description"]))
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                config.Remove("description");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                config.Add("description", "XML
操作Message");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
base.Initialize(name, config);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
string temp = config["connectionStringName"];
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (temp == null || temp.Length < 1)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ProviderException("connectionStringName属性缺少或为空");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _connectionString = ConfigurationManager.ConnectionStrings[temp].ConnectionString;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (_connectionString == null || _connectionString.Length < 1)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ProviderException("没找到'" + temp + "'所指的连接字符串,或所指连接字符串为空");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            config.Remove("connectionStringName");
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


MessageProviderCollection

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System.Configuration.Provider;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// MessageProvider集合类
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class MessageProviderCollection : ProviderCollection
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 向集合中添加提供程序。
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <param name="provider">要添加的提供程序。</param>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public override void Add(ProviderBase provider)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (provider == null)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ArgumentNullException("provider参数不能为null");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
if (!(provider is MessageProvider))
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new ArgumentException("provider参数类型必须是MessageProvider.");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
base.Add(provider);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


MessageProviderConfigurationSection

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// MessageProvider的配置
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class MessageProviderConfigurationSection : ConfigurationSection
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private readonly ConfigurationProperty _defaultProvider;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private readonly ConfigurationProperty _providers;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private ConfigurationPropertyCollection _properties;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 构造函数
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public MessageProviderConfigurationSection()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _defaultProvider = 
new ConfigurationProperty("defaultProvider", typeof(string), null);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _providers = 
new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _properties = 
new ConfigurationPropertyCollection();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _properties.Add(_providers);
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            _properties.Add(_defaultProvider);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message的默认的Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        [ConfigurationProperty("defaultProvider")]
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public string DefaultProvider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return (string)base[_defaultProvider]; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
set { base[_defaultProvider] = value; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// Message的所有的Provider集合
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        [ConfigurationProperty("providers", DefaultValue = "SqlMessageProvider")]
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        [StringValidator(MinLength = 1)]
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public ProviderSettingsCollection Providers
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return (ProviderSettingsCollection)base[_providers]; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// MessageProvider的属性集合
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
protected override ConfigurationPropertyCollection Properties
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            
get { return _properties; }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


Message

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections.Generic;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
namespace Pattern.Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
/// 暴露给客户端用的Message的类(Context
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
public class Message
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static bool m_isInitialized = false;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static MessageProviderCollection _providers = null;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static MessageProvider _provider = null;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 静态构造函数,初始化
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
static Message()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            Initialize();
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 插入信息
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// <param name="mm">Message实体对象</param>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public static bool Insert(MessageModel mm)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return _provider.Insert(mm);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        
/// <summary>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// 获取信息
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
/// </summary>
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        
/// <returns></returns>
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
public static List<MessageModel> Get()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
return _provider.Get();
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static void Initialize()
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
try
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                MessageProviderConfigurationSection messageConfig = 
null;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
if (!m_isInitialized)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif                {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    
// 找到配置文件中“MessageProvider”节点
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                    messageConfig = (MessageProviderConfigurationSection)ConfigurationManager.GetSection("MessageProvider");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    
if (messageConfig == null)
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                        
throw new ConfigurationErrorsException("在配置文件中没找到“MessageProvider”节点");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    _providers = 
new MessageProviderCollection();
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    
// 使用System.Web.Configuration.ProvidersHelper类调用每个ProviderInitialize()方法
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                    ProvidersHelper.InstantiateProviders(messageConfig.Providers, _providers, typeof(MessageProvider));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    
// 所用的Provider为配置中默认的Provider
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
                    _provider = _providers[messageConfig.DefaultProvider] as MessageProvider;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                    m_isInitialized = 
true;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif                }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
catch (Exception ex)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
string msg = ex.Message;
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
throw new Exception(msg);
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static MessageProvider Provider
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
get
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
return _provider;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        
private static MessageProviderCollection Providers
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif        {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif            
get
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif            {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif                
return _providers;
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}
http://www.cnblogs.com/Images/OutliningIndicators/None.gif



Web.config

http://www.cnblogs.com/Images/OutliningIndicators/None.gif<?xml version="1.0" encoding="utf-8"?>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
<configuration>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
<configSections>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
<section name="MessageProvider" type="Pattern.Provider.MessageProviderConfigurationSection, Pattern.Provider" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
</configSections>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
<MessageProvider defaultProvider="SqlMessageProvider">
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
<providers>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif      
<add name="XmlMessageProvider" type="Pattern.Provider.XmlMessageProvider, Pattern.Provider" connectionStringName="XmlConnection" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gif      
<add name="SqlMessageProvider" type="Pattern.Provider.SqlMessageProvider, Pattern.Provider" connectionStringName="SqlConnection" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
</providers>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
</MessageProvider>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
<connectionStrings>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
<add name="SqlConnection" connectionString="server=.;database=db;uid=sa;pwd=sa" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gif    
<add name="XmlConnection" connectionString="XmlPath" />
http://www.cnblogs.com/Images/OutliningIndicators/None.gif  
</connectionStrings>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
</configuration>
http://www.cnblogs.com/Images/OutliningIndicators/None.gif


Test

http://www.cnblogs.com/Images/OutliningIndicators/None.gifusing System;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Data;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Configuration;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Collections;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.Security;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.WebControls.WebParts;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using System.Web.UI.HtmlControls;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
using Pattern.Provider;
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
http://www.cnblogs.com/Images/OutliningIndicators/None.gif
public partial class Provider : System.Web.UI.Page
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif    {
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(Message.Insert(
new MessageModel("插入", DateTime.Now)));
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write("<br />");
http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif        Response.Write(Message.Get()[0].Message + " " + Message.Get()[0].PublishTime.ToString());
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif    }
http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}


运行结果
True
SQL方式,连接字符串是server=.;database=db;uid=sa;pwd=sa 2007-1-22 8:21:44



果糖网