应用框架的设计与实现——.NET平台(6.获取外部配置信息)
来源:互联网 发布:python 停止运行 编辑:程序博客网 时间:2024/05/21 10:00
使用 IConfigurationAgent 接口可以实现从外部资源获得配置数据。
配置文件的 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 的信息处理类
ConfigurationAgentManager --负责与代理对象打交道的管理类
IConfigurationAgent --代理类接口
ConfigurationWSAgent -- 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>
<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"));
}
}
/// 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;
}
}
/// 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();
}
/// 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);
}
}
/// 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平台(6.3.1访问集中式配置.WebService)
- .NET平台应用框架设计与实现
- 应用框架的设计与实现——.NET平台(1)
- 应用框架的设计与实现——.NET平台(2)
- 应用框架的设计与实现——.NET平台(3)
- 应用框架的设计与实现——.NET平台(4)
- 应用框架的设计与实现——.NET平台4.2代码测试
- 应用框架的设计与实现——.NET平台4.2代码测试问题解决
- 应用框架的设计与实现——.NET平台(4.2 Remoting 客户端激活)
- 应用框架的设计与实现——.NET平台(4.2 Remoting 无配置文件)
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析)
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析.源码1)
- 应用框架的设计与实现——.NET平台(4.3 SAF代码分析.源码2)
- 应用框架的设计与实现——.NET平台(6 源码分析)
- 应用框架的设计与实现——.NET平台(5 缓存服务.源码分析)
- 应用框架的设计与实现——.NET平台(7.事件通知服务.源码分析)
- 应用框架的设计与实现——.NET平台(8.1 Windows服务.线程)
- 下载的福音--Metalink
- 计算某个城市的所得税
- 别了,我的萨达姆
- uC/OS-II初级程序员指南
- 我佛山人"编写的表单验证类 Validator使用说明
- 应用框架的设计与实现——.NET平台(6.获取外部配置信息)
- Hibernate API简介,记忆几个接口
- 一个统计线程占用CPU时间的类
- 关于PVPGN的一些经验总结
- 久违的感觉
- 网友的电脑中发现scvhsot.exe的进程和启动项
- CD转换高品质MP3,其实也可以很简单
- 关于“网页对话框”中链接和表单提交会在新窗口中打开的问题
- 思绪如潮