using System;
using System.Configuration;
namespace Neo.Afx.Services
{
#region ServiceConfigurationSection
public sealed class ServiceConfigurationSection : ConfigurationSection
{
#region Static "sharedServices" Section Getter
///
/// Gets the services section.
///
///
public ServiceConfigurationSection GetSection()
{
return ConfigurationManager.GetSection("services") as ServiceConfigurationSection;
}
///
/// Gets the services.
///
///
public ServiceConfigurationCollection GetServices()
{
return GetSection().Services;
}
#endregion
private readonly ConfigurationProperty _services = new ConfigurationProperty(null, typeof(ServiceConfigurationCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
private readonly ConfigurationPropertyCollection _properties = new ConfigurationPropertyCollection();
///
/// Initializes a new instance of the class.
///
public ServiceConfigurationSection()
{
this._properties.Add(this._services);
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public ServiceConfigurationCollection Services
{
get
{
return (ServiceConfigurationCollection)base[this._services];
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return this._properties;
}
}
}
#endregion
#region ServiceElement
///
/// Represents a service setting within a configuration file.
///
public sealed class ServiceElement : ConfigurationElement
{
private ConfigurationPropertyCollection _properties;
private readonly ConfigurationProperty _type;
private readonly ConfigurationProperty _pollFrequency;
private readonly ConfigurationProperty _pollTime;
private readonly ConfigurationProperty _sectionName;
private readonly ConfigurationProperty _name;
#region Initialization
public ServiceElement()
{
this._name = new ConfigurationProperty("name", typeof(string), null, ConfigurationPropertyOptions.IsKey);
this._sectionName = new ConfigurationProperty("sectionName", typeof(string), null, ConfigurationPropertyOptions.None);
this._pollFrequency = new ConfigurationProperty("pollFrequency", typeof(PollFrequencyEnum), null, ConfigurationPropertyOptions.IsRequired);
this._pollTime = new ConfigurationProperty("pollTime", typeof(TimeSpan), null, ConfigurationPropertyOptions.IsRequired);
this._type = new ConfigurationProperty("type", typeof(string), null, ConfigurationPropertyOptions.IsRequired);
this._properties = new ConfigurationPropertyCollection();
this._properties.Add(this._name);
this._properties.Add(this._sectionName);
this._properties.Add(this._pollFrequency);
this._properties.Add(this._pollTime);
this._properties.Add(this._type);
}
public ServiceElement(string name)
: this()
{
if(name != ((string)this._name.DefaultValue))
{
this.Name = name;
}
}
#endregion
#region Key
internal string Key
{
get
{
return this.Name;
}
}
#endregion
#region Name
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get
{
return (string)base[this._name];
}
set
{
base[this._name] = value;
}
}
#endregion
#region SectionName
[ConfigurationProperty("sectionName", DefaultValue = "", IsRequired = false)]
public string SectionName
{
get
{
return (string)base[this._sectionName];
}
set
{
base[this._sectionName] = value;
}
}
#endregion
#region PollFrequency
[ConfigurationProperty("pollFrequency", DefaultValue = PollFrequencyEnum.Minutes, IsRequired = true)]
public PollFrequencyEnum PollFrequency
{
get
{
return (PollFrequencyEnum)base[this._pollFrequency];
}
set
{
base[this._pollFrequency] = value;
}
}
#endregion
#region PollTime
[ConfigurationProperty("pollTime", DefaultValue = "02:00", IsRequired = true)]
[TimeSpanValidator(MinValueString = "00:00:00", MaxValueString = "365.23:59:59", ExcludeRange = false)]
public TimeSpan PollTime
{
get
{
return (TimeSpan)base[this._pollTime];
}
set
{
base[this._pollTime] = value;
}
}
#endregion
#region Type
[ConfigurationProperty("type", IsRequired = true)]
public string Type
{
get
{
return (string)base[this._type];
}
set
{
base[this._type] = value;
}
}
#endregion
}
#endregion
#region ServiceCollection
///
/// Represents a collection of service settings within a configuration file.
///
[ConfigurationCollection(typeof(ServiceElement))]
public class ServiceConfigurationCollection : ConfigurationElementCollection
{
public void Add(ServiceElement element)
{
this.BaseAdd(element);
}
public void Clear()
{
base.BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new ServiceElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
if(element == null)
{
throw new ArgumentNullException("element");
}
return ((ServiceElement)element).Key;
}
public int IndexOf(ServiceElement element)
{
return base.BaseIndexOf(element);
}
public void Remove(ServiceElement element)
{
if(element == null)
{
throw new ArgumentNullException("element");
}
base.BaseRemove(element.Key);
}
public void Remove(string name)
{
base.BaseRemove(name);
}
public void RemoveAt(int index)
{
base.BaseRemoveAt(index);
}
// Properties
public new ServiceElement this[string name]
{
get
{
return (ServiceElement)base.BaseGet(name);
}
set
{
if(base.BaseGet(name) != null)
{
base.BaseRemove(name);
}
this.BaseAdd(value);
}
}
public ServiceElement this[int index]
{
get
{
return (ServiceElement)base.BaseGet(index);
}
set
{
if(base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
}
#endregion
}