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實體對象</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">該提供程序的友好名稱。</param>
/// <param name="config">名稱/值對的集合,表示在配置中為該提供程序指定的、提供程序特定的屬性。</param>
public override void Initialize(string name, NameValueCollection config)
{
if (string.IsNullOrEmpty(name))
name = "MessageProvider";
if (null == config)
throw new ArgumentException("config參數不能為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屬性缺少或為空");
_connectionString = ConfigurationManager.ConnectionStrings[temp].ConnectionString;
if (_connectionString == null || _connectionString.Length < 1)
{
throw new ProviderException("沒找到'" + temp + "'所指的連接字符串,或所指連接字符串為空");
}
config.Remove("connectionStringName");
}
}
}