分享
 
 
 

DotText源码阅读(3)-框架配置体系和反序列化

王朝html/css/js·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

DotText源码阅读(3)-框架配置体系和反序列化

DotText源码阅读(3)-框架配置体系和反序列化 dottext框架配置体系 和反序列化 配置节是一个比较容易混淆人的专题。Dottext的系统环境配置、单独每一个人的blog配置都是通过自定义的配置节实现的,并且dottext自己实现了其中的处理程序(handler)。也就是说,利用asp.net系统的配置文件作为存储机制,加上了单独处理机制,实现了系统的灵活配置。在web.config的根元素<configuration>下一开始就声明了自定义配置节处理程序:<configSections> <section name='BlogConfigurationSettings' type='Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.Framework' /> <section name='HandlerConfiguration' type='Dottext.Framework.Util.XmlSerializerSectionHandler, Dottext.Framework' /> <section name='SearchConfiguration' type='Dottext.Framework.Util.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' /> <section name='codeHighlighter' type='ActiproSoftware.CodeHighlighter.CodeHighlighterConfigurationSectionHandler, ActiproSoftware.CodeHighlighter' /></configSections> 其中的最后2项自然不必多讲,属于微软的提供程序和第三方提供程序,在此忽略。我们逐步来看看 Dottext.Framework.Util 下的实现过程,理解其中的逻辑。用到的自定义处理程序都是 XmlSerializerSectionHandler ,我们看看其中的处理逻辑,蕴含的.net特性: 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));}string typename = (string) nav.Evaluate('string(@type)'); 是从当前XML(配置文件是一个符合xml要求的文档)节点处,获取”type”属性,然后按照属性描述,获得一个.net的类型。这里使用到了.net的反射机制。此处的type可以是类 、值类型 、数组 、接口 、指针 、枚举类型。这样,通过配置文件中的xml流(相当于字符串),系统就指定了特定的类。这种生成类的方法是区别于new 方法生成具体类的另外途径,好处就是灵活根据具体环境内容(甚至是用户交互输入的类型描述字符串)就可以生成获得托管类型。(此处反射细节请参考MSDN)。坏处就是可能隐藏着类型错误,运行时出错,导致不可预料(好像这个词在windows编程时代相当的常见)例外甚至系统崩溃。当然,仅仅创建了类型是不够用的,还需要通过一定途径来确定生成类的具体状态,这有用到OOP语言的重要特性—序列化,将一个对象存储器来,以及从存储中还原具体对象的机制。这里使用的是System提供的 XmlSerializer 类的反序列化方法Deserialize。反序列化后面还有很多代码涉及到,我认为现在就大致理解为“通过这个反序列化,我们刚刚得到的类实例中的属性、成员变量获得了赋值,进入到某个状态,就好像我们此处运行了new 语法和进行了对象的构造以及赋值”即可。更进一步的可以从权威资料MSDN获取。 基于以上理解,我们来阅读相关的配置节,并进行解释。从简单的入手:<SearchConfiguration type='Dottext.Search.SearchConfiguration, Dottext.Search' urlFormat='http://{0}/{1}/{2}/{3}.aspx' virtualPath ='~/SearchIndex' physicalPath='\SearchIndex' domains='localhost' pageSize='20' />这个配置节,定义了一个 在Dottext.Search程序集中存在的名为“Dottext.Search.SearchConfiguration”的类,在反序列化的时候,我们会对其中的某些属性(urlFormat、virtualPath、physicalPath、domains、pageSize )进行赋值。那么,这些可以反序列化的类有否什么区别于其他“正常类”的地方呢?我们打开这个类看看:[Serializable] //注意,这里用到的是属性编程,这个是.net的新特性,通过这个语法声明了名为Serializable的属性给类 SearchConfiguration,告诉.net框架这个类是可以进行序列化和反序列化。默认情况下,所有该类的字段(包括私有)都要序列化和反序列化,但是通过另外指定属性声明,可以灵活处理。 public class SearchConfiguration { public static readonly string PermaLink = 'permalink'; 。。。。。。//一大堆只读静态字段 public static readonly string TempIndex = 'tempIndex'; public static SearchConfiguration Instance() { return (SearchConfiguration)ConfigurationSettings.GetConfig('SearchConfiguration');//此处就是利用了反射来构造类实例。 } public SearchConfiguration() { //缺盛构造函数 } private string _urlFormat = 'http://{0}/{1}/{2}/{3}.aspx'; [XmlAttribute('urlFormat')] //此处另外声明了UrlFormat属性的序列化和反序列化属性,告诉.net运行时环境此处的字段采用XML节点作为存储进行序列化和反序列化,并且读取的节点名称是“urlFormat”。 public string UrlFormat { get {return this._urlFormat;} set {this._urlFormat = value;} } private string _domains; [XmlAttribute('domains')] public string Domains { get {return this._domains;} set {this._domains = value;} } private int _rebuildInterval = 60; [XmlAttribute('rebuildInterval')] public int RebuildInterval { get {return this._rebuildInterval;} set {this._rebuildInterval = value;} } private int _updateInterval = 30; [XmlAttribute('updateInterval')] public int UpdateInterval { get {return this._updateInterval;} set {this._updateInterval = value;} } private int _pageSize = 50; [XmlAttribute('pageSize')] public int PageSize { get {return this._pageSize;} set {this._pageSize = value;} } private int _searchResultLimit = 100; [XmlAttribute('searchResultLimit')] public int SearchResultLimit { get {return this._searchResultLimit;} set {this._searchResultLimit = value;} } private string _virtualPath; [XmlAttribute('virtualPath')] public string VirtualPath { get {return this._virtualPath;} set {this._virtualPath = value;} } private string _physicalPath; [XmlAttribute('physicalPath')] public string PhysicalPath { get { if(this._physicalPath == null) { if(VirtualPath != null) { this._physicalPath = HttpContext.Current.Server.MapPath(VirtualPath); } else { throw new ApplicationException('Physical location of the search index could not be found. Either the physical or virtual location must be specified in your configuration file'); } } return this._physicalPath; } set {this._physicalPath = value;} } } 如果哪一个字段不需要参与序列化和反序列化,应该指定[XmlIgnore]属性标记。 接下来,看看后面经常用到的 BlogConfigurationSettings ,该类为“Dottext.Framework.Configuration.BlogConfigurationSettings”,察看该类源代码,我们发现该类也是可序列化和反序列化的。不过注意的是,其中的部分成员属于数组,而数组属于复合数据类型,所以在配置文件声明中是这样的:<EntryHandlers> <EntryHandler type='Dottext.Framework.EntryHandling.CommentFormatHandler, Dottext.Framework' postType='Comment' processAction='Insert' processState='PreCommit' isAsync='false' /> <EntryHandler type='Dottext.Framework.EntryHandling.CommentDeliveryHandler, Dottext.Framework' postType='Comment' processAction='Insert' processState='PostCommit' isAsync='true' /> ……</EntryHandlers>而在类源代码中是这样的来说明改成员:private EntryHandler[] _entryHandlers;[XmlArray('EntryHandlers')]public EntryHandler[] EntryHandlers{ get {return this._entryHandlers;} set {this._entryHandlers = value;}}通过XmlArray属性,指出了要按照数组方式进行序列化和反序列化,节点的名称是“EntryHandlers”。.net CLR会通过反射机制将配置文件的描述生成EntryHandler[],而其中每一个元素都是Dottext.Framework.EntryHandling.CommentDeliveryHandler,这个过程通过一个短小的[XmlArray('EntryHandlers')]就完成,且又达到了灵活是应需求,展示了.net提供的新特性的威力。HandlerConfiguration也是通过配置获得一个数组,类似机理。另外,打击需要着重看看<BlogProviders> <!-- Controls how .Text formats Urls --> <UrlFormatProvider type='Dottext.Framework.Format.UrlFormats, Dottext.Framework' /> <DTOProvider type='Dottext.Framework.Data.DataDTOProvider, Dottext.Framework' /> <!-- By default .Text uses SQL Server as the backend data store. The DbProvider determines which DbProvider (a class which implements IDbProvider) is used. This is optional. --> <DbProvider type='Dottext.Framework.Data.SqlDataProvider, Dottext.Framework' connectionString='user id=ad;password=cbiqadjsd;initial Catalog=Zixun_dataBase;Data Source=211' /> <ConfigProvider type='Dottext.Common.Config.MultipleBlogConfig, Dottext.Common' host='localhost' cacheTime='120' /> <!-- <ConfigProvider type = 'AspNetWeb.MSBlogsConfigProvider, MsftBlogsHttpModule' cacheTime = '120'/> --> <!-- Controls how .Text sends email. By default, SystemMail is used. --> <EmailProvider type='Dottext.Framework.Email.SystemMail, Dottext.Framework' smtpServer='localhost' adminEmail='EMAIL' /> </BlogProviders>此处的配置信息在后面的很多部分都涉及到。看看BlogProviders类(呵呵,当作课外吧),也是一个可序列化和反序列化的类。整个dottext很多灵活性就是通过这机制体现。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有