应用框架的设计与实现——.NET平台(6.获取外部配置信息)
2006-12-30 21:32
1111 查看
使用 IConfigurationAgent 接口可以实现从外部资源获得配置数据。
配置文件的 Framework 节:
<configSections>
<section name="Framework" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
</configSections>
<Framework type="SAF.Configuration.ConfigurationManager,SAF.Configuration">
<SAF.Configuration>
<ConfigurationAgent>
<Agent name = "WSAgent1" type="TestConfigurationAgent.ConfigurationWSAgent,TestConfigurationAgent">
<Parameters>
<Section>SAF.ClassFactory</Section>
<Environment>QAEnvironment</Environment>
</Parameters>
<Url>http://localhost/ConfigurationData/ConfigurationService.asmx</Url>
</Agent>
</ConfigurationAgent>
</SAF.Configuration>
<SAF.ClassFactory ConfigurationAgent="WSAgent1"/>
</Framework>
配置管理类如何使用代理类的?
1.系统启动时读入配置文件,按照配置信息 Framework 节被交给 SAF.Configuration.ConfigurationManager 处理;
2.ConfigurationManager 建立一个 ConfigurationAgentManager 对象帮助获取配置信息;
3.当 ConfigurationManager 向 ConfigurationAgentManager 请求取得 SAF.ClassFactory 的配置信息时,
4.ConfigurationAgentManager 对象判断 SAF.ClassFactory 节是否有 ConfigurationAgent 属性,
4.1.如果此属性不存在则返回这个节的配置信息,
4.2.否则根据属性名找到对应的 Agent 节,使用节信息构造一个 IConfigurationAgent 对象,由这个对象取得配置信息,然后返回;
5.当 ConfigurationManager 取得返回信息后用返回信息构造 SAF.ClassFactory 节的配置对象。
使用代理类的顺序图:
<<csdn的blog真tmd的烂;上传文件更是让人绝望>>
ConfigurationManager --配置段 Framework 的信息处理类
/**//// <summary>
/// Provides access to configuraiton object for the
/// framework component
/// </summary>
public class ConfigurationManager
...{
public SAF.Configuration.ServiceConfiguration ServiceConfig;
public SAF.Configuration.AuthorizationConfiguration AuthorizationConfig;
public SAF.Configuration.CryptographyConfiguration CryptographyConfig;
public SAF.Configuration.AuthenticationConfiguration AuthenticationConfig;
public SAF.Configuration.ClassFactoryConfiguration ClassFactoryConfig;
public SAF.Configuration.EventNotificationConfiguration EventNotificationConfig;
public SAF.Configuration.CacheConfigration CacheConfig;
public SAF.Configuration.MessageQueueConfiguration MessageQueueConfig;
private XmlNode configurationData;
/**//// <summary>
/// Initialize all the configuration objects accessible through
/// this configuration manager.
/// </summary>
/// <param name="sections"></param>
public ConfigurationManager (XmlNode sections)
...{
configurationData = sections;
ConfigurationAgentManager cam = new ConfigurationAgentManager(configurationData);
ServiceConfig = new ServiceConfiguration(cam.GetData("SAF.WindowsService"));
AuthorizationConfig = new AuthorizationConfiguration(cam.GetData("SAF.Authorization"));
CryptographyConfig = new CryptographyConfiguration(cam.GetData("SAF.Cryptography"));
AuthenticationConfig = new AuthenticationConfiguration(cam.GetData("SAF.Authentication"));
ClassFactoryConfig = new ClassFactoryConfiguration(cam.GetData("SAF.ClassFactory"));
EventNotificationConfig = new EventNotificationConfiguration(cam.GetData("SAF.EventNotification"));
MessageQueueConfig = new MessageQueueConfiguration(cam.GetData("SAF.MessageQueue"));
CacheConfig = new CacheConfigration(cam.GetData("SAF.Cache"));
}
}
ConfigurationAgentManager --负责与代理对象打交道的管理类
/**//// <summary>
/// It is responsible for loading the agent object which
/// is responsible for retrieving the configuration data
/// </summary>
public class ConfigurationAgentManager
...{
private XmlNode configurationData;
public ConfigurationAgentManager(XmlNode configData)
...{
configurationData = configData;
}
/**//// <summary>
/// it return the Xml containing the configuraiton settings for a given key
/// </summary>
/// <param name="key">name of the Xml section in the configuration file, such as <SAF.ClassFactory>. </param>
/// <returns>XmlNode that contains the configuration settings </returns>
public XmlNode GetData(string key)
...{
XmlNode result=null;
XmlAttribute agentAttribute =null;
if (configurationData.SelectSingleNode(key) != null)
...{
//check if there is agent defined for a particular section or key
//if there is, load the agent and make it retrieve the data
//otherwise, just load the data from the configuraiton file
agentAttribute = configurationData.SelectSingleNode(key).Attributes["ConfigurationAgent"];
if ( agentAttribute == null)
...{
result = configurationData.SelectSingleNode(key);
}
else
...{
//retrive the data using the agent
string data = GetAgent(agentAttribute.Value).GetConfigurationSetting();
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
result = (XmlNode)xml.DocumentElement;
}
}
return result;
}
/**//// <summary>
/// the method load the agent using reflection and return an instance of agent
/// to the caller
/// </summary>
/// <param name="agentName">name of the agent referenced in the configuration file</param>
/// <returns>an agent object</returns>
private IConfigurationAgent GetAgent(string agentName)
...{
XmlNode agentNode = configurationData.SelectSingleNode("//Agent[@name ='" + agentName + "']");
Type type = Type.GetType(agentNode.Attributes["type"].Value);
IConfigurationAgent agent = (IConfigurationAgent)Activator.CreateInstance(type,null);
//Initialize method setup the agent object with the parameter information specified
//in the file that is needed for the agent to do its job
agent.Initialize(agentNode);
return agent;
}
}
IConfigurationAgent --代理类接口
/**//// <summary>
/// Interface that each agent class must implement.
/// its two methods are called by agent manager at runtime.
/// </summary>
public interface IConfigurationAgent
...{
void Initialize(XmlNode xml);
string GetConfigurationSetting();
}
ConfigurationWSAgent -- IConfigurationAgent 实现代理接口的一个范例类
/**//// <summary>
/// A sample Agent class that is responsible for retrieve configuration
/// data stored in other system via web service.
/// </summary>
public class ConfigurationWSAgent : IConfigurationAgent
...{
private string section;
private string environment;
private string url;
public ConfigurationWSAgent()...{}
/**//// <summary>
/// this method sets up the agent with parameters information
/// defined in the configuration file.
/// </summary>
/// <param name="configData"></param>
public void Initialize(XmlNode configData)
...{
section= configData.SelectSingleNode("Parameters/Section").InnerText;
environment = configData.SelectSingleNode("Parameters/Environment").InnerText;
url = configData.SelectSingleNode("Url").InnerText;
}
/**//// <summary>
/// this method call the web service and retrieve the actual configration data
/// </summary>
/// <returns>the configuration data</returns>
public string GetConfigurationSetting()
...{
localhost.ConfigurationService cs = new localhost.ConfigurationService();
cs.Url = url;
return cs.GetConfiguration(section,environment);
}
}
配置文件的 Framework 节:
<configSections>
<section name="Framework" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
</configSections>
<Framework type="SAF.Configuration.ConfigurationManager,SAF.Configuration">
<SAF.Configuration>
<ConfigurationAgent>
<Agent name = "WSAgent1" type="TestConfigurationAgent.ConfigurationWSAgent,TestConfigurationAgent">
<Parameters>
<Section>SAF.ClassFactory</Section>
<Environment>QAEnvironment</Environment>
</Parameters>
<Url>http://localhost/ConfigurationData/ConfigurationService.asmx</Url>
</Agent>
</ConfigurationAgent>
</SAF.Configuration>
<SAF.ClassFactory ConfigurationAgent="WSAgent1"/>
</Framework>
配置管理类如何使用代理类的?
1.系统启动时读入配置文件,按照配置信息 Framework 节被交给 SAF.Configuration.ConfigurationManager 处理;
2.ConfigurationManager 建立一个 ConfigurationAgentManager 对象帮助获取配置信息;
3.当 ConfigurationManager 向 ConfigurationAgentManager 请求取得 SAF.ClassFactory 的配置信息时,
4.ConfigurationAgentManager 对象判断 SAF.ClassFactory 节是否有 ConfigurationAgent 属性,
4.1.如果此属性不存在则返回这个节的配置信息,
4.2.否则根据属性名找到对应的 Agent 节,使用节信息构造一个 IConfigurationAgent 对象,由这个对象取得配置信息,然后返回;
5.当 ConfigurationManager 取得返回信息后用返回信息构造 SAF.ClassFactory 节的配置对象。
使用代理类的顺序图:
<<csdn的blog真tmd的烂;上传文件更是让人绝望>>
ConfigurationManager --配置段 Framework 的信息处理类
/**//// <summary>
/// Provides access to configuraiton object for the
/// framework component
/// </summary>
public class ConfigurationManager
...{
public SAF.Configuration.ServiceConfiguration ServiceConfig;
public SAF.Configuration.AuthorizationConfiguration AuthorizationConfig;
public SAF.Configuration.CryptographyConfiguration CryptographyConfig;
public SAF.Configuration.AuthenticationConfiguration AuthenticationConfig;
public SAF.Configuration.ClassFactoryConfiguration ClassFactoryConfig;
public SAF.Configuration.EventNotificationConfiguration EventNotificationConfig;
public SAF.Configuration.CacheConfigration CacheConfig;
public SAF.Configuration.MessageQueueConfiguration MessageQueueConfig;
private XmlNode configurationData;
/**//// <summary>
/// Initialize all the configuration objects accessible through
/// this configuration manager.
/// </summary>
/// <param name="sections"></param>
public ConfigurationManager (XmlNode sections)
...{
configurationData = sections;
ConfigurationAgentManager cam = new ConfigurationAgentManager(configurationData);
ServiceConfig = new ServiceConfiguration(cam.GetData("SAF.WindowsService"));
AuthorizationConfig = new AuthorizationConfiguration(cam.GetData("SAF.Authorization"));
CryptographyConfig = new CryptographyConfiguration(cam.GetData("SAF.Cryptography"));
AuthenticationConfig = new AuthenticationConfiguration(cam.GetData("SAF.Authentication"));
ClassFactoryConfig = new ClassFactoryConfiguration(cam.GetData("SAF.ClassFactory"));
EventNotificationConfig = new EventNotificationConfiguration(cam.GetData("SAF.EventNotification"));
MessageQueueConfig = new MessageQueueConfiguration(cam.GetData("SAF.MessageQueue"));
CacheConfig = new CacheConfigration(cam.GetData("SAF.Cache"));
}
}
ConfigurationAgentManager --负责与代理对象打交道的管理类
/**//// <summary>
/// It is responsible for loading the agent object which
/// is responsible for retrieving the configuration data
/// </summary>
public class ConfigurationAgentManager
...{
private XmlNode configurationData;
public ConfigurationAgentManager(XmlNode configData)
...{
configurationData = configData;
}
/**//// <summary>
/// it return the Xml containing the configuraiton settings for a given key
/// </summary>
/// <param name="key">name of the Xml section in the configuration file, such as <SAF.ClassFactory>. </param>
/// <returns>XmlNode that contains the configuration settings </returns>
public XmlNode GetData(string key)
...{
XmlNode result=null;
XmlAttribute agentAttribute =null;
if (configurationData.SelectSingleNode(key) != null)
...{
//check if there is agent defined for a particular section or key
//if there is, load the agent and make it retrieve the data
//otherwise, just load the data from the configuraiton file
agentAttribute = configurationData.SelectSingleNode(key).Attributes["ConfigurationAgent"];
if ( agentAttribute == null)
...{
result = configurationData.SelectSingleNode(key);
}
else
...{
//retrive the data using the agent
string data = GetAgent(agentAttribute.Value).GetConfigurationSetting();
XmlDocument xml = new XmlDocument();
xml.LoadXml(data);
result = (XmlNode)xml.DocumentElement;
}
}
return result;
}
/**//// <summary>
/// the method load the agent using reflection and return an instance of agent
/// to the caller
/// </summary>
/// <param name="agentName">name of the agent referenced in the configuration file</param>
/// <returns>an agent object</returns>
private IConfigurationAgent GetAgent(string agentName)
...{
XmlNode agentNode = configurationData.SelectSingleNode("//Agent[@name ='" + agentName + "']");
Type type = Type.GetType(agentNode.Attributes["type"].Value);
IConfigurationAgent agent = (IConfigurationAgent)Activator.CreateInstance(type,null);
//Initialize method setup the agent object with the parameter information specified
//in the file that is needed for the agent to do its job
agent.Initialize(agentNode);
return agent;
}
}
IConfigurationAgent --代理类接口
/**//// <summary>
/// Interface that each agent class must implement.
/// its two methods are called by agent manager at runtime.
/// </summary>
public interface IConfigurationAgent
...{
void Initialize(XmlNode xml);
string GetConfigurationSetting();
}
ConfigurationWSAgent -- IConfigurationAgent 实现代理接口的一个范例类
/**//// <summary>
/// A sample Agent class that is responsible for retrieve configuration
/// data stored in other system via web service.
/// </summary>
public class ConfigurationWSAgent : IConfigurationAgent
...{
private string section;
private string environment;
private string url;
public ConfigurationWSAgent()...{}
/**//// <summary>
/// this method sets up the agent with parameters information
/// defined in the configuration file.
/// </summary>
/// <param name="configData"></param>
public void Initialize(XmlNode configData)
...{
section= configData.SelectSingleNode("Parameters/Section").InnerText;
environment = configData.SelectSingleNode("Parameters/Environment").InnerText;
url = configData.SelectSingleNode("Url").InnerText;
}
/**//// <summary>
/// this method call the web service and retrieve the actual configration data
/// </summary>
/// <returns>the configuration data</returns>
public string GetConfigurationSetting()
...{
localhost.ConfigurationService cs = new localhost.ConfigurationService();
cs.Url = url;
return cs.GetConfiguration(section,environment);
}
}
相关文章推荐
- 应用框架的设计与实现——.NET平台(6 自定配置文件节读取类)
- 应用框架的设计与实现——.NET平台(4.2 Remoting 无配置文件)
- 应用框架的设计与实现——.NET平台4.2代码测试问题解决
- 应用框架的设计与实现——.NET平台(9 消息队列服务代码分析)
- 应用框架的设计与实现——.NET平台(10 授权服务.PrincipalPermissionAttribute)
- 应用框架的设计与实现——.NET平台(9 消息队列产品安装)
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析)
- 应用框架的设计与实现——.NET平台(2)
- 应用框架的设计与实现——.NET平台(8.1 Windows服务.线程)
- 基于元数据配置的asp.net数据库应用快速开发框架设计及实现
- 应用框架的设计与实现——.NET平台(7.事件通知服务.委托.异步编程)
- 应用框架的设计与实现——.NET平台(6 源码分析)
- 应用框架的设计与实现——.NET平台(4.2 Remoting 客户端激活)
- 应用框架的设计与实现——.NET平台(7.事件通知服务.源码分析)
- 应用框架的设计与实现——.NET平台(5 缓存服务.源码分析)
- 应用框架的设计与实现——.NET平台(3)
- 应用框架的设计与实现——.NET平台(1)
- 应用框架的设计与实现——.NET平台(8.2 Windows服务)
- 应用框架的设计与实现——.NET平台(8.3 调用非托管代码的方法)
- 应用框架的设计与实现——.NET平台(10 授权服务.源码分析)