找传奇、传世资源到传世资源站!

C# 提供者模式(Provider Pattern)实例源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

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

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复