您的位置:首页 > 其它

dottext学习笔记1 配置

2006-09-17 21:34 274 查看
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#region cnstr


public BlogConfigurationSettings()




...{






}


#endregion




Static#region Static




public static BlogConfigurationSettings Instance()




...{


//return BlogConfigurationSettings.Instance(HttpContext.Current);


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


}


public static BlogConfigurationSettings Instance(HttpContext context)




...{


return Instance();


}


#endregion




Helper#region Helper


private void ConfigException(string message)




...{


throw new Exception(message);


}


#endregion




Properties#region Properties


private Tracking _tracking;


public Tracking Tracking




...{


get




...{


if(this._tracking == null)




...{


this._tracking = new Tracking();


}


return this._tracking;


}




set ...{this._tracking = value;}


}


#endregion




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"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: