ASP.NET应用程序中,很大部分重要功能可以通过web.config来配置。其中有一个<configSection>的节点,里面包含了.net定义的配置节处理程序与配置节之间的关联。也可以自定义节点和自定义配置节点的处理程序。如图所示:

最近项目中使用到了自定义节点,简单总结下:在web.config中的<configSection>节点中添加要自定义的节点<section>,指定name为获取自定义配置的类实体的类名(AppAplication),Type为自定义节点处理程序的 (程序集.类名,程序集) 格式,如web.config中的自定义配置节点示例图中所示:

设计读取自定义配置节点的类结构,类关系视图如下:

 

CmfuConfig为对外使用web.config配置节点的类,包含一个静态属性Instance,通过调用AppConfig泛型类得到对应类型的对象(AppAplication:获取可以再.cs中配置web.config节点的对象(AppSetting)的类),通过获取到的AppAplication,获取字段AppSetting(返回此对象),然后就可以获取到AppSetting的所有字段了。注意:会优先从web.config获取,web.config没有配置的,才会从本地的AppSetting类中获取配置值。

每个类的代码示例如下:

CmfuConfig.cs

View Code
namespace SectionTest
{
    public class CmfuConfig
    {
        public static AppAplication Instance
        {
            get
            {
                return AppConfig<AppAplication>.Instance;
            }
        }
    }
}

AppConfig.cs

View Code
namespace SectionTest
{
    public class AppConfig<T>
    {
        public static T instance = default(T);

        public static T Instance
        {
            get
            {
                if (instance == null||instance.Equals(default(T)))
                {
                    instance = (T)ConfigurationManager.GetSection("AppAplication");
                }
                return instance;
            }
            set
            {
                instance = value;
            }
        }
    }
}

AppAplication.cs

View Code
namespace SectionTest
{
    public class AppAplication
    {
        public AppSetting AppSetting = new AppSetting();
    }
}

AppConfigHandler.cs

View Code
namespace SectionTest
{
    class AppCofigHandler:IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler 成员

        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            XPathNavigator nav = section.CreateNavigator();
            string typename = (string)nav.Evaluate("string(@type)");
            Type t = Type.GetType(typename);
            //利用XML反序列化的方式将配置和属性配对
            XmlSerializer ser = new XmlSerializer(t);
            return ser.Deserialize(new XmlNodeReader(section));
        }

        #endregion
    }
}

AppSetting.cs

View Code
namespace SectionTest
{
    public partial class AppSetting
    {
        [XmlAttribute]
        public string BookName = "傲世九重天";
    }
}

AppSetting.Author.cs

namespace SectionTest
{
    public partial class AppSetting
    {
        [XmlAttribute]
        public string Author = "西红柿吃土豆";
    }
}

使用客户端:(和config同一个项目)

        protected void Page_Load(object sender, EventArgs e)
        {
            AppSetting app = CmfuConfig.Instance.AppSetting;

            string pp = app.BookName;
        }

      得到的书名字符串pp为:武动乾坤。(即 config中配置的BookName覆盖了AppSetting中的值)

      在使用过程中,可以在AppSetting类中配置需要配置的节点,从而使开发环境中避免了因不同开发人员的需要,任意更改web.config造成的意外错误。(默认情况下,以web.config中的配置为最高优先级,如果web.config中没有配置,则自动读取AppSetting中的配置。)

 

------------------------------------------------------------------------------------------------------------------

菜鸟之作,希望指正!

posted on 2013-04-09 10:23  零点漂移  阅读(419)  评论(0编辑  收藏  举报