dottext学习笔记1 配置

 
1           Web.config配置
1.1      configSections. Section节配置自定义节的处理器
1.2      自定义节配置
自定义节中定义属性type,处理器在解析时,根据type进行后期绑定创建。
在自定义节中,可以定义子或节,叶子elem对应于[Serializable]类对象。节在类成员属性中部分使用[XmlArray("EntryHandlers")]修饰,返回和设置是数组;部分节直接对应一个类对象,然后进行类似的递归。
2         类设计
2.1           自定义节处理器
XmlSerializerSectionHandler : IConfigurationSectionHandler
根据type进行反序列化生成节对应的类
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);
       XmlSerializer ser 
= new XmlSerializer(t);
       
return ser.Deserialize(new XmlNodeReader(section));
}
 

2.2           子定节的类设计
BlogConfigurationSettings,跟elem名相同,用[Serializable]修饰。
ConfigProviderConfiguration,跟elem名不同,用[XmlRoot("ConfigProvider")]修饰。
子节对应的是数组对象,用[XmlArray("EntryHandlers")]修饰
属性对应的值为elem的attribute,用[XmlAttribute("imageDirectory")]修饰
注意,属性(子节点)的反序列化,跟XmlSerializerSectionHandler无关,而是由.NET的XML自己序列化。
如:
<Event type = "Dottext.Framework.Tracking.StatsQueueSchedule, Dottext.Framework" minutes = "5" key = "StatsQueue" />
对应的类跟type(StatsQueueSchedule)无关。

2.3 读取使用

return ((HandlerConfiguration)ConfigurationSettings.GetConfig("HandlerConfiguration"));
 
 

3. 具体相关代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="HandlerConfiguration" type="Dottext.xx.XmlSerializerSectionHandler, Dottext.Framework" /> 
        ...
        <section name="microsoft.web.services" type="Microsoft.Web.Services.Configuration.WebServicesConfiguration, Microsoft.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </configSections>
    <HandlerConfiguration defaultPageLocation="default.aspx" type="Dottext.Common.UrlManager.HandlerConfiguration, Dottext.Common">
        <HttpHandlers>
            <HttpHandler pattern="(/.config|/.asax|/.ascx|/.config|/.cs|/.vb|/.vbproj|/.asp|/.licx|/.resx|/.resources)$" type="Dottext.Framework.UrlManager.HttpForbiddenHandler, Dottext.Framework" handlerType="Direct" />
            <HttpHandler pattern="(/.gif|/.js|/.jpg|/.zip|/.jpeg|/.jpe|/.css|/.rar|/.xml|/.xsl)$" type="Dottext.Common.UrlManager.BlogStaticFileHandler, Dottext.Common" handlerType="Direct" />
            <HttpHandler pattern="/rss/.aspx$" type="Dottext.Common.Syndication.RssHandler, Dottext.Common" handlerType="Direct" />
            <HttpHandler pattern="/CommentsRSS/.aspx$" type="Dottext.Common.Syndication.RecentCommentsRSS, Dottext.Common" handlerType="Direct" />
            <HttpHandler pattern="/RecentCommentsRSS/.aspx$" type="Dottext.Common.Syndication.RecentCommentsRSS, Dottext.Common" handlerType="Direct" />
            <HttpHandler pattern="/atom/.aspx$" type="Dottext.Common.Syndication.AtomHandler, Dottext.Common" handlerType="Direct" />
        </HttpHandlers>
    </HandlerConfiguration>
</configuration>
 
using  System;
using  System.Configuration;
using  System.Xml;
using  System.Xml.Serialization;
using  System.Xml.XPath;
namespace  Dottext.Framework.Util
{
 
public class XmlSerializerSectionHandler : 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);
   XmlSerializer ser 
= new XmlSerializer(t);
   
return ser.Deserialize(new XmlNodeReader(section));
  }

 }

}


using  System;
using  Dottext.Framework.Configuration;
using  System.Xml.Serialization;
namespace  Dottext.Framework.Providers
{
 
/// <summary>
 
/// Summary description for ConfigProvider.
 
/// </summary>

 [XmlRoot("ConfigProvider")]
 
public class ConfigProviderConfiguration : BaseProvider 
 
{
  
public ConfigProviderConfiguration(){}
  
private string _imageDirectory;
  [XmlAttribute(
"imageDirectory")]
  
public string ImageDirectory
  
{
   
get {return this._imageDirectory;}
   
set {this._imageDirectory = value;}
  }

  
private int _blogID;
  [XmlAttribute(
"blogID")]
  
public int BlogID
  
{
   
get {return this._blogID;}
   
set {this._blogID = value;}
  }

 }

}

namespace  Dottext.Framework.Configuration
{
 
/// <summary>
 
/// Summary description for BlogConfigurationSettings.
 
/// </summary>

 [Serializable]
 
public class BlogConfigurationSettings 
 
{
  
cnstr
  
Static
  
Helper
  
Properties
        
  
private EntryHandler[] _entryHandlers;
  
  
/// <summary>
  
/// Property EntryFactoryItems (EntryFactoryItem[])
  
/// </summary>

  [XmlArray("EntryHandlers")]
  
public EntryHandler[] EntryHandlers
  
{
   
get {return this._entryHandlers;}
   
set {this._entryHandlers = value;}
  }

  
 }

}
class
HandlerConfiguration {
        private string _defaultPageLocation;
        [XmlAttribute("defaultPageLocation")]
        public string DefualtPageLocation
        {
            get {return this._defaultPageLocation;}
            set {this._defaultPageLocation = value;}
        }
       .....
    public static HandlerConfiguration Instance()
        {
            return ((HandlerConfiguration)ConfigurationSettings.GetConfig("HandlerConfiguration"));
        }
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值