也谈自定义配置处理类

在现在的WEB开发中不管是JAVA还是.NET大家对配置文件一定不会陌生.但它的实现原理对新手来说却又是那么的模糊.下面的内容是说明如何自定义处理配置类

所有能够处理配置节的类必须要实现IConfigurationSectionHandler接口,
他的作用是处理对特定的以前的配置节的访问。而IConfigurationSectionHandler接口很简单
只有一个object Create(object parent,object configContext,XmlNode section)方法.这个方法不需要主动的调用.
它是在ConfigurationSettings.GetConfig这个静态方法的时候自动调用的,也就是说,当你在程序中使用ConfigurationSettings.GetConfig来获取配置节的时候,.net会根据改配置节声明中所定义的类名和路径自动实例化配置节处理类,并调用Create方法.
相信了解Duwamish解决方案的朋友对下面这一段不会陌生:

 

1、在global.asax的Application_OnStart方法里面调用ApplicationConfiguration.OnApplicationStart静态方法,并获得应用程序根的绝对路径。
    void Application_OnStart()
{
ApplicationConfiguration.OnApplicationStart(Context.Server.MapPath( Context.Request.ApplicationPath));
string configPath = Path.Combine(Context.Server.MapPath( Context.Request.ApplicationPath ),
"remotingclient.cfg");
if(File.Exists(configPath))
RemotingConfiguration.Configure(configPath);
}
2、ApplicationConfiguration.OnApplicationStart静态方法里调用System.Configuration.ConfigurationSettings.GetConfig方法处理配置节:
        public static void OnApplicationStart(String myAppPath)
{
appRoot = myAppPath;
System.Configuration.ConfigurationSettings.GetConfig("ApplicationConfiguration");
System.Configuration.ConfigurationSettings.GetConfig("DuwamishConfiguration");
System.Configuration.ConfigurationSettings.GetConfig("SourceViewer");
}

大家已经注意到了,程序并没有获取GetConfig返回的值,这是因为GetConfig方法会引发配置节处理程序的Create方法,
所以,只需要在Create方法中将配置值取出来就行了。
System.Configuration.ConfigurationSettings.GetConfig("ApplicationConfiguration")
这个静态方法来读取ApplicationConfiguration配置节的时候,
会相应的类来对这个配置节进行处理。




下面是我自定义的一个配置节处理类.还忘大家指教

 

 

  1/// <summary>
  2    /// ConfigurationSetting 的摘要说明。
  3    /// </summary>

  4    public class ConfigurationSetting: IConfigurationSectionHandler
  5    {
  6        /// <summary>
  7        /// 配置
  8        /// </summary>

  9        protected NameValueCollection Settings;
 10        /// <summary>
 11        /// 默读构造函数
 12        /// </summary>

 13        public ConfigurationSetting()
 14        {
 15            //
 16            // TODO: 在此处添加构造函数逻辑
 17            //
 18        }

 19        /// <summary>
 20        /// 
 21        /// </summary>
 22        /// <param name="parent"></param>
 23        /// <param name="configContext"></param>
 24        /// <param name="section"></param>
 25        /// <returns></returns>

 26        public Object Create(Object parent, object configContext, XmlNode section)
 27        {
 28                    
 29            try
 30            {
 31                NameValueSectionHandler baseHandler = new NameValueSectionHandler();
 32                Settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
 33            }

 34            catch
 35            {
 36                Settings = null;
 37            }

 38            return null;
 39        }

 40
 41        ReadSetting
108}


备注:
    

IConfigurationSectionHandler 类的实例必须是线程安全且无状态的。Create 方法必须可由多个线程同时调用。

而且,Create 生成的配置对象必须是线程安全且不可变的。而且,由于配置对象由配置系统来缓存,
因此务必要修改 Create 的父参数。
例如,如果 Create 的返回值只对父参数进行少量修改,则实际修改必须在父参数的复本上进行,而不能在原参数上进行。


参考:
    微软中国社区(Duwamish深入剖析)
MSDN








 

posted @ 2007-09-13 11:00  罗志威  阅读(1643)  评论(0编辑  收藏  举报