轻量级的.NET对象查找服务和AOP开发框架Netop.Core源码解说(2)--配置

来源:互联网 发布:c# 数据库接口开发 编辑:程序博客网 时间:2024/06/06 10:04

先把Netop.Core的最核心部分“对象查找服务”放一放,先说说应用系统的配置。

一个应用系统的配置是少不了的,除非你是一个纯硬代码族顽固者。

也见过有的应用系统通过系统提供的健值(key-value)方法在appSettings节点下设了几十个甚至上百个,不堪入目,更别说条理性了。

开发一个应用框架,配置一般是少不了,如log4net就有自己的配置,不会让你在appSettings设几十个条目。

开发配置是很简单的,下面慢慢说来。

在NET应用中,配置信息以XML文档储存。WEB应用为Web.config文件,可执行文件为*.exe.config(编译时由app.config复制并改名)。

在NET中,配置文件的信息经实现System.Configuration.IConfigurationSectionHandler接口的处理器类读出,其接口只有一个方法:

object Create(Object parent, object configContext, XmlNode section);

在Netop.Core中,实现的配置处理器类是Netop.Core.Configuration.ConfigurationHandler.

实现了配置处理器类,就获得了此节点的XML信息,再对它进行解析读出其相关内容即可。在Netop.Core中,ConfigurationHandler把这个工作交给了Netop.Core.Configuration.AppConfigurationManager。

看看实际的配置信息就清楚了:

  <configSections>
     <section name="Netop.Application" type="Netop.Core.Configuration.ConfigurationHandler,Netop.Core" />    
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

这里可以看出,log4net的配置处理器类是log4net.Config.Log4NetConfigurationSectionHandler
Netop.Core的配置处理器类是Netop.Core.Configuration.ConfigurationHandler,节点名为Netop.Application,再看Netop.Application段:

 <Netop.Application type="Netop.Core.Configuration.AppConfigurationManager,Netop.Core">
    <Application.ObjectLocator>
      <DefaultAppObjectLocatorService>Netop.Core.LocatorService.DefaultAppObjectLocatorService,Netop.Core</DefaultAppObjectLocatorService>
      <ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>
      <AspectAgentName>AppAspect</AspectAgentName>
    </Application.ObjectLocator>

    <Application.Log>
      <LogCategory>Netop.Core.Log.Log4Net,Netop.Core</LogCategory>
      <LogParameter>Log</LogParameter>
    </Application.Log>

    <Application.Agent>
      <Agent name = "AppObjectService" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Service.xml</File>
      </Agent>
      <Agent name = "AppAspect"  type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Aspect.xml</File>
      </Agent>
      <Agent name = "AppEntity"  type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Entity.xml</File>
      </Agent>
    </Application.Agent>
 </Netop.Application>

<Netop.Application type="Netop.Core.Configuration.AppConfigurationManager,Netop.Core">中type的值即是AppConfigurationManager。

在Netop.Core中,有一个类Netop.Core.Configuration.SettingConfiguration实现读取类似“<LogCategory>Netop.Core.Log.Log4Net,Netop.Core</LogCategory>”这样的信息。
对于SettingConfiguration,Netop.Core有个默认的节点:
<Netop.Application type="Netop.Core.Configuration.AppConfigurationManager,Netop.Core">
       ……
    <Application.Settings>
    ……
    </Application.Settings>
</Netop.Application>

如在Application.Settings加项目如:<Host>smtp.sohu.com</Host>
使用则为:Netop.Core.Configuration.AppConfiguration.Current.ApplicationSettingsConfig.GetKeyValue("Host")而得到值smtp.sohu.com .

也可以自己加一Section如:
   <Application.SmtpServer>
      <Host>smtp.sohu.com</Host>
      <RequriedAuthenticate>true</RequriedAuthenticate>
      ……
   </Application.SmtpServer>
使用则为:Netop.Core.Configuration.AppConfiguration.Current.GetAppSettingConfiguration("Application.SmtpServer").GetKeyValue("Host")而得到值smtp.sohu.com .

在Netop.Core中,还有一种代理配置,代理配置类只要实现Netop.Core.Configuration.IConfigurationAgent接口就可挺拔式使用。IConfigurationAgent如下:
    public interface IConfigurationAgent
    {
        void Initialize(XmlNode xml);
        AgentConfigurationInfo GetConfigurationInfo();
    }

Netop.Core已经实现一个XML文件代理配置类:Netop.Core.Configuration.FileAgentConfiguration 。
 
对于配置信息:
 <Netop.Application type="Netop.Core.Configuration.AppConfigurationManager,Netop.Core">
    <Application.ObjectLocator>
      <DefaultAppObjectLocatorService>Netop.Core.LocatorService.DefaultAppObjectLocatorService,Netop.Core</DefaultAppObjectLocatorService>
      <ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>
      <AspectAgentName>AppAspect</AspectAgentName>
    </Application.ObjectLocator>

    <Application.Log>
      <LogCategory>Netop.Core.Log.Log4Net,Netop.Core</LogCategory>
      <LogParameter>Log</LogParameter>
    </Application.Log>

    <Application.Agent>
      <Agent name = "AppObjectService" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Service.xml</File>
      </Agent>
      <Agent name = "AppAspect"  type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Aspect.xml</File>
      </Agent>
      <Agent name = "AppEntity"  type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Entity.xml</File>
      </Agent>
    </Application.Agent>
 </Netop.Application>


<Application.Agent>节点内的就是代理配置信息:
<Agent name = "AppObjectService" type="Netop.Core.Configuration.FileAgentConfiguration,Netop.Core">
        <File>Service.xml</File>
</Agent>
name为代理配置名,type为代理配置类,File为文件路径。
程序使用为:
object p = null;
AppConfiguration.Current.GetAgentData("AppObjectService", out p);//返回Service.xml文件的XML信息
当然代理配置名AppObjectService是跟<ObjectServiceAgentName>AppObjectService</ObjectServiceAgentName>联系的,感兴趣的可以看ObjectServiceConfigurationManager的内容。

对于开发者要自己在Netop.Application加节点(不属于上面所说的类型),怎么办呢?
AppConfigurationManager有一方法:XmlNode GetData(string key) ,调用后自己解析XML信息即可(调用AppConfiguration.Current.GetData("节点名")返回XmlNode)。

这是Netop.Core的配置服务。

当然还可以直接使用NET系统的配置处理器类来处理配置信息,如:
    System.Configuration.NameValueSectionHandler <!--以NameValue键值/对的形式返回配置节中的信息-->
    System.Configuration.DictionarySectionHandler  <!--以Dictionary字典键值对的形式返回配置节中的信息-->
    System.Configuration.SingleTagSectionHandler <!--基础结构。处理 .config 文件中由单个 XML 标记所表示的各配置节。-->

 

    轻量级的.NET对象查找服务和AOP开发框架源码Netop.Core3.5下载地址:http://download.csdn.NET/detail/tom_cat_xie_jxdy/9837303

    轻量级的.NET对象查找服务和AOP开发框架测试源码 下载地址:http://download.csdn.Net/detail/tom_cat_xie_jxdy/9837278

   Netop.Core--轻量级的.NET对象查找服务和AOP开发框架文档下载地址:http://download.csdn.net/detail/tom_cat_xie_jxdy/9838212

谢富鸿fhxie@sohu.com


转载自: http://www.cnblogs.com/fhxie/p/6884204.html
阅读全文
0 0
原创粉丝点击