Message
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;
}
}
}
}