MessageProviderCollection
using System.Configuration.Provider;
using System;
namespace Pattern.Provider
{
/**//// <summary>
/// Message的Provider集合類
/// </summary>
public class MessageProviderCollection : ProviderCollection
{
/**//// <summary>
/// 向集合中添加提供程序。
/// </summary>
/// <param name="provider">要添加的提供程序。</param>
public override void Add(ProviderBase provider)
{
if (provider == null)
throw new ArgumentNullException("provider參數不能為null");
if (!(provider is MessageProvider))
throw new ArgumentException("provider參數類型必須是MessageProvider.");
base.Add(provider);
}
}
}
MessageProviderConfigurationSection
using System.Configuration;
namespace Pattern.Provider
{
/**//// <summary>
/// Message的Provider的配置
/// </summary>
public class MessageProviderConfigurationSection : ConfigurationSection
{
private readonly ConfigurationProperty _defaultProvider;
private readonly ConfigurationProperty _providers;
private ConfigurationPropertyCollection _propertIEs;
/**//// <summary>
/// 構造函數
/// </summary>
public MessageProviderConfigurationSection()
{
_defaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), null);
_providers = new ConfigurationProperty("providers", typeof(ProviderSettingsCollection), null);
_propertIEs = new ConfigurationPropertyCollection();
_propertIEs.Add(_providers);
_propertIEs.Add(_defaultProvider);
}
/**//// <summary>
/// Message的默認的Provider
/// </summary>
[ConfigurationProperty("defaultProvider")]
public string DefaultProvider
{
get { return (string)base[_defaultProvider]; }
set { base[_defaultProvider] = value; }
}
/**//// <summary>
/// Message的所有的Provider集合
/// </summary>
[ConfigurationProperty("providers", DefaultValue = "SqlMessageProvider")]
[StringValidator(MinLength = 1)]
public ProviderSettingsCollection Providers
{
get { return (ProviderSettingsCollection)base[_providers]; }
}
/**//// <summary>
/// Message的Provider的屬性集合
/// </summary>
protected override ConfigurationPropertyCollection PropertIEs
{
get { return _propertIEs; }
}
}
}