Provider 模式

Provider 模式:爲一個API進行定義和實現的分離。html

常見場景:DBPrider切換,第3方集成API切換 web

以發郵件爲例:api

 

Email Provider Config:ide

    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 this

  public sealed class EDMProvider : EmailProvider
    {
        #region Public Methods and Operators

        public override EmailResponse SendEmail(ApiSetting apiSetting, EmailContent content)
        {
           。。。。
      }
}

 

Configuration:spa

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

  

使用:code

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

 

 上面是一種provider提供的功能,只能用一種API實現。xml

  下面這個連接是,一種provider提供的功能, 會有多種API的實現,用戶可能選擇不一樣的api.htm

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

相關文章
相關標籤/搜索