技术开发 频道

提供多样化配置 .NET应用框架架构设计

  实现

  假设某框架的配置节定义如下:

using System.Configuration;
public class ApplicationElement : ConfigurationElement {
[ConfigurationProperty(
"provider", IsKey=true, IsRequired=true)]
public string Provider {
get { return (string)base["provider"]; }
set { base["provider"] = value; }
}
}
public class ObjectContainerElement : ConfigurationElement {
[ConfigurationProperty(
"provider", IsKey=true, IsRequired=true)]
public string Provider {
get { return (string)base["provider"]; }
set { base["provider"] = value; }
}
}
[ConfigurationCollection(
typeof(ObjectContainerElement),
AddItemName
="objectContainer",  CollectionType=ConfigurationElementCollectionType.BasicMap)]
public class ObjectContainerElementCollection :
ConfigurationElementCollection {
protected override string ElementName {
get { return "objectContainer"; }
}
public override ConfigurationElementCollectionType CollectionType {
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override ConfigurationElement CreateNewElement() {
return new ObjectContainerElement();
}
protected override object GetElementKey(ConfigurationElement element) {
return (element as ObjectContainerElement).Provider;
}
public void Add(ObjectContainerElement element) {
this.BaseAdd(element);
}
}
public class FrameworkConfigSection : ConfigurationSection {
[ConfigurationProperty(
"application", IsRequired=true)]
public ApplicationElement Application {
get { return (ApplicationElement)base["application"]; }
set { base["application"] = value; }
}
[ConfigurationProperty(
"objectContainers")]
public ObjectContainerElementCollection ObjectContainers {
get { return (ObjectContainerElementCollection)base["objectContainers"]; }
set { base["objectContainers"] = value; }
}
}

  根据上面的设计分析,配置源的实现如下:

public interface IConfigurationSource {
FrameworkConfigSection Config {
get; }
}
public class ConfigFileConfigurationSource : IConfigurationSource {
private FrameworkConfigSection config;
public ConfigFileConfigurationSource() {
this.ReadFromConfigFile();
}
private void ReadFromConfigFile() {
this.config
= (FrameworkConfigSection)
ConfigurationManager.GetSection(
"frameworkConfig");
}
public FrameworkConfigSection Config {
get { return this.config; }
}
}
public class RegularConfigurationSource : IConfigurationSource {
private FrameworkConfigSection config;
public RegularConfigurationSource() {
config
= new FrameworkConfigSection();
config.Application
= new ApplicationElement();
config.ObjectContainers
= new ObjectContainerElementCollection();
}
public void AddElement(ObjectContainerElement element) {
config.ObjectContainers.Add(element);
}
public void SetElement(ApplicationElement element) {
config.Application
= element;
}
public void AddObjectContainer(string provider) {
config.ObjectContainers.Add(
new ObjectContainerElement
{ Provider
= provider });
}
public void SetApplication(string provider) {
config.Application
= new ApplicationElement
{ Provider
= provider };
}
public FrameworkConfigSection Config {
get { return this.config; }
}
}

  在框架中,通常需要一个引导器(BootStrapper)来启动一些框架所依赖的服务或者进行一些数据准备工作。为了简化描述,在此我们仅用上面实现部分介绍的Application以及ApplicationFactory来模拟引导器的这部分功能。首先定义IApplication接口,然后创建一个实现该接口的类Application:

public interface IApplication {
IConfigurationSource ConfigSource {
get; }
}
public class Application : IApplication {
private readonly IConfigurationSource configSource;
public Application(IConfigurationSource configSource) {
this.configSource
= configSource;
}
public IConfigurationSource ConfigSource {
get { return this.configSource; }
}
}
0
相关文章