应用框架的设计与实现——.NET平台(6.获取外部配置信息)

来源:互联网 发布:python 停止运行 编辑:程序博客网 时间:2024/05/21 10:00
使用 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);
    }

}
原创粉丝点击