欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

.Net配置文件——统一节点配置管理

程序员文章站 2022-06-09 18:54:18
...
对于.Net中的自定义节点,如果要单独处理,就要为这个节点添加一个指定的类,如果我们在一个应用程序中,定义了很多个自定义节点的话,还按照这样做,就会多出很多个处理类来,为了避免太多的类,我们将所有自定节点都指定一个自定义节点作为入口,然后只写一个处理类,以此为入口,读取其他节点。

例如,配置文件只定义一个入口节点:

 <!--通用配置存储方法-->
  <traceFact type="ClassLib.ConfigManager,ClassLib">
    
    <forum name="TraceFact.Net Community"> 
      <root url="http:192.168.24.204"/>
      <replyCount>20</replyCount>
      <pageSize>30</pageSize>
      <offlineTime>20</offlineTime>
    </forum>
    
    <blog>
      <root url="http:asdakfj.com"/>
      <urlMappings>
        <rewriteRule>
          <request>~/(\d{4})/Default\.aspx</request>
          <sendTo>~BlogDetail.aspx?year=$1</sendTo>
        </rewriteRule>
      </urlMappings>
    </blog>   
    
    <mailServer address="www.baidu.com" userName="lhc" password="234r32r"/>
    
    <greetingStrategy type="ClassLib.ChineseGreeting,ClassLib"/>
    
  </traceFact>

处理类:

 /// <summary>
    /// Class GeneralConfigHandler
    /// </summary>
    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:40</remarks>
    public class GeneralConfigHandler:IConfigurationSectionHandler
    {
        /// <summary>
        /// 创建配置节处理程序。
        /// </summary>
        /// <param name="parent">父对象。</param>
        /// <param name="configContext">配置上下文对象。</param>
        /// <param name="section">节 XML 节点。</param>
        /// <returns>创建的节处理程序对象。</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 10:50:42</remarks>
        public object Create(object parent, object configContext, XmlNode section)
        {
            //获取节点type属性的值
            Type t=Type.GetType(section.Attributes["type"].Value);//返回configManager
            //直接将section进行传递
            object[] paramters={section};
            //将要创建的类型实例
            object obj = null;

            try
            {
                obj = Activator.CreateInstance(t, paramters);//使用有参数的构造函数

            }
            catch (Exception)
            {

                return null;
            }
            return obj;
        }
    }

上面的类主要是为了实现创建具体配置节点管理类,在配置节点的管理类中,存有各个子节点类的引用。

 /// <summary>
    /// GeneralConfigHandler通过反射动态创建的,它实际上只是作为一个容器
    ///这个类仅仅是作为一个容器,包含对其下具体节点配置的引用,并通过traceFact根节点,获取traceFact其下的子节点,然后再创建用于映射具体的子节点的类型实例
    /// </summary>
    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:06:21</remarks>
    public class ConfigManager
    {
        private XmlNode section;
        private FroumConfiguration forumConfig;
        //对于其他节点的处理强类型访问类也定义为成员变量放在这里。。。。。。。

        public FroumConfiguration froumConfig {

            get { return forumConfig; }
        }

        public ConfigManager(XmlNode section) {
            this.section = section;
            forumConfig = new FroumConfiguration(section.SelectSingleNode("forum"));
        }
    }

    /// <summary>
    /// 具体子节点配置forum结点:处理<forum name="TraceFact.Net Community"> 
    /// </summary>
    /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:22:08</remarks>
    public class FroumConfiguration{
        private XmlNode forumNode;
        
        //将forum结点传递进来
        public FroumConfiguration(XmlNode section){
            this.forumNode=section;
        }

        /// <summary>
        /// Gets the name. <forum name="TraceFact.Net Community"> 
        /// </summary>
        /// <value>The name.</value>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:58:00</remarks>
        public string Name{
            get{
                return forumNode.Attributes["name"].Value;
                }
        }
        /// <summary>
        /// Gets the root URL.:<root url="http:192.168.24.204"/>
        /// </summary>
        /// <value>The root URL.</value>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:56:08</remarks>
        public string RootUrl{
        
            get{
                return forumNode.SelectSingleNode("root").Attributes["url"].Value;
            }
        }

        /// <summary>
        /// Gets the size of the page.<pageSize>30</pageSize>
        /// </summary>
        /// <value>The size of the page.</value>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:18</remarks>
        public int PageSize{
        
            get{
                return int.Parse(forumNode.SelectSingleNode("pageSize").InnerText);
            }
        }

        /// <summary>
        /// Gets the reply count.<replyCount>20</replyCount>
        /// </summary>
        /// <value>The reply count.</value>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:03</remarks>
        public int ReplyCount{
        
            get{
                return int.Parse(forumNode.SelectSingleNode("replyCount").InnerText);
            }
        }

        /// <summary>
        /// Gets the offline time.<offlineTime>20</offlineTime>
        /// </summary>
        /// <value>The offline time.</value>
        /// <remarks>Editor:v-liuhch CreateTime:2015/7/1 14:57:35</remarks>
        public int OfflineTime {
            get {
                return int.Parse(forumNode.SelectSingleNode("offlineTime").InnerText);

            }
        }
    }

这样,我们使用的时候,每次都用这一个类来实现对节点的访问:

 ConfigManager config = ConfigurationManager.GetSection("traceFact") as ConfigManager;

            //取值
            ltrName.Text = config.froumConfig.Name;
            ltrOfflineTime.Text = config.froumConfig.OfflineTime.ToString();
            ltrPageSize.Text = config.froumConfig.PageSize.ToString();
            ltrReplyCount.Text = config.froumConfig.ReplyCount.ToString();
            ltrRootUrl.Text = config.froumConfig.RootUrl.ToString();

以上就是.Net配置文件——统一节点配置管理的内容,更多相关内容请关注PHP中文网(www.php.cn)!