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