zoukankan      html  css  js  c++  java
  • Provider 模式

    Provider 模式:为一个API进行定义和实现的分离。

    常见场景:DBPrider切换,第3方集成API切换

    以发邮件为例:

    Email Provider Config:

        public abstract class EmailProvider : ProviderBase
        {
              #region Public Methods and Operators
    
            public abstract EmailResponse SendEmail(ApiSetting apiSetting, EmailContent content);
    
              #endregion
        }
    
    
    
    public sealed class EmailProviderCollection : ProviderCollection
        {
            #region Public Indexers
                  
            public new EmailProvider this[string name]
            {
                get { return (EmailProvider) base[name]; }
            }
    
            #endregion
        }
    
    
     public sealed class EmailProviderConfiguration : ConfigurationSection
        {
            #region Public Properties
    
            /// <summary>
            ///     Gets the default provider name.
            /// </summary>
            [ConfigurationProperty("default", DefaultValue = "EDMProvider")]
            public string DefaultProviderName
            {
                get { return base["default"] as string; }
            }
    
            /// <summary>
            ///     Gets the providers.
            /// </summary>
            [ConfigurationProperty("providers")]
            public ProviderSettingsCollection Providers
            {
                get { return (ProviderSettingsCollection) base["providers"]; }
            }
    
            #endregion
        }
    
    
    
     public static class EmailProviderManager
        {
            #region Static Fields
    
            private static readonly EmailProviderCollection Providers = new EmailProviderCollection();
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            ///     Initializes static members of the <see cref="EmailProviderConfiguration" /> class.
            /// </summary>
            static EmailProviderManager()
            {
                Initialize();
            }
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            ///     Gets the default.
            /// </summary>
            public static EmailProvider EmailProvider { get; private set; }
    
            #endregion
    
            #region Methods
    
            /// <summary>
            ///     Reads the configuration related to the set of configured
            ///     providers and sets the default and collection of providers and settings.
            /// </summary>
            private static void Initialize()
            {
                try
                {
                    var section = (EmailProviderConfiguration) ConfigurationManager.GetSection("EmailProviders");
    
                    if (section == null)
                    {
                        throw new ConfigurationErrorsException("Email Provider Section is not set");
                    }
    
                    ProvidersHelper.InstantiateProviders(section.Providers, Providers, typeof (EmailProvider));
    
                    if (Providers[section.DefaultProviderName] == null)
                    {
                        throw new ConfigurationErrorsException("Email provider is not set");
                    }
    
                    EmailProvider = Providers[section.DefaultProviderName];
                }
                catch (Exception ex)
                {
                    Log.Debug(ex.Message, ex);
                }
            }
    

    EDMProvider 

      public sealed class EDMProvider : EmailProvider
        {
            #region Public Methods and Operators
    
            public override EmailResponse SendEmail(ApiSetting apiSetting, EmailContent content)
            {
               。。。。
          }
    }

    Configuration:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    
      <configSections>
        <section name="EmailProviders" type="AAA.Mail.Configuration.EmailProviderConfiguration, AAA.Mail" />
      </configSections>
      
      <EmailProviders default="EDMProvider">
        <providers>
          <add name="EDMProvider" type="AAA.Mail.EDM.EDMProvider, AAA.Mail.EDM" />
        </providers>
      </EmailProviders>
    
    
    </configuration>
    

      

    使用:

     EmailProviderManager.EmailProvider .SendEmail (.....)

     上面是一种provider提供的功能,只能用一种API实现。

      下面这个链接是,一种provider提供的功能, 会有多种API的实现,用户可能选择不同的api.

       http://www.cnblogs.com/webabcd/archive/2007/01/22/626479.html

  • 相关阅读:
    petshop4.0 详解之三(PetShop数据访问层之消息处理)
    MemberShip的使用
    PetShop 详解之一 系统架构设计
    PetShop4,错误提示:没有为 SQL 缓存通知启用数据库"MyCard"
    PetShop4,错误提示:System.Web.Security.SqlMembershipProvider”要求一个与架构版本“1”兼容的数据
    【Linux从零开始】:1.文件与目录的管理和配置(1)
    【笔记】在.NET中使用强类型有以下优点:
    【好文收藏】:Linq to DataSet
    【好文收藏】泛型与非泛型的比较(百度文库)
    HDOJ1102 Constructing Roads[Prim()]
  • 原文地址:https://www.cnblogs.com/lindaWei/p/4079622.html
Copyright © 2011-2022 走看看