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)无关。
<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"));
}
}
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"));
}
}
相关文章推荐
- OpenLDAP学习笔记7——LDAP在Solaris10上的配置
- ASP.NET MVC 学习笔记-7.自定义配置信息
- Redis 学习笔记(三):主从复制、主从配置和性能测试
- 学习笔记,Linux常用命令--MySQL下载安装配置
- Hibernate学习笔记之开发环境的配置
- OpenGL学习笔记:(2)iOS下GLES环境配置
- Zynq-Linux移植学习笔记之十-u-boot网络配置
- GO1.6语言学习笔记2-安装配置及代码组织
- 【学习笔记】——ubuntu gcc安装完成后的配置
- Zookeeper 学习笔记之配置启动
- Hibernate学习笔记之EHCache的配置
- 3.nginx学习笔记:nginx配置反向代理服务器
- coolite1.0 学习笔记(一) -- 配置coolite开发环境
- DirectX 10 学习笔记1:VS2010环境中配置DX10或DX11
- 【Hibernate学习笔记-4】在hibernate.cfg.xml中配置C3P0数据源
- (csdn内转载)spring学习笔记:配置视图解析器ViewResolver
- 4.nginx学习笔记:nginx配置虚拟主机
- Spring Boot学习笔记-配置devtools实现热部署
- google protobuf学习笔记一:windows下环境配置
- struts2学习笔记 -- day02 struts2主配置文件详解