欢迎来到.net学习网

欢迎联系站长一起更新本网站!QQ:879621940

您当前所在位置:首页 » ASP.Net » 正文

热门阅读

为自定义配置的编辑提供“智能感知”的支持

创建时间:2011年06月12日 23:33  阅读次数:(4892)
分享到:

当我们在设计一个框架的时候,必然会涉及一系列的配置。为了让使用者更好地使用你提供的框架,让他们能够容易地维护这些配置是一项基本的要求。对于一些配置过于复杂的框架,比如EnterLib,比如WCF,往往会提供一个配置的工具。但是,不过这样的配置工具是否提供,手工编译配置文件是在所难免的。如果在通过VS编辑配置的时候,能够提供智能感知和提示性描述的支持,这无疑会使配置的编辑变得非常的容易。这里是一个简单的例子。

一、配置文件的结构

假设我们设计一个MessageGenerator的组件,用于生成程序我们需要的文本消息,比如验证控件的验证消息,异常的消息等。消息的内容根据需要可以保存在数据库中或者是XML文件中,并且采用不同模式(粒度)的缓存(None、Single、Category和All)。我们采用插件式的设计,定义一个IMessageProvider接口用于从数据源中获取消息列表,两个具体的DbMessageProvider和XmlMessageProvider实现了这个接口。具体采用那个MessageProvider,通过配置来决定(defaultProvider),配置结构如下所示:

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="artech.messages"type="Artech.Messages.MessagesSetting, Artech.Messages"/>
</configSections>
<artech.messages defaultProvider="DbProvider"cachingMode="None">
<providers>
<add name="DbProvider"type="Artech.Messages.DbMessageProvider, Artech.Messages"
connectionString="" applicationName="" />
<add name="XmlProvider"type="Artech.Messages.XmlMessageProvider, Artech.Messages" source="~/Messages.xml"/>
</providers>
</artech.messages>
</configuration> 

二、为配置定义XSD

为了在VS中编辑配置文件的时候获得智能感知的支持,我们需要为它定义XSD。整个XSD如下所示,结构比较清晰。不过在这里有几点需要强调:

(1)我们通过采用<xs:documentation>节点定义一些表述性的文字。当你通过VS编辑配置文件的时候,针对当前配置节点定义的这些文字将会以Tips的形式显示出来;

(2)在定义MessageProvider的schema的时候,我们将针对所有预定义MessageProvider的配置属性都定义出来(DbMessageProvider的applicationName和connectionStringName,以及XmlMessageProvider的source)。如果这些配置属性不是对于所有的MessageProvider都是必须的(比如name和type),将它们定义成可选属性;

(3)对于一些枚举,或者限定在某个列表范围内固定的值,通过<xs:restriction>/<xs:enumeration>来定义,比如这里使用到的CachingMode。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
<xs:element name="artech.messages"type="MessagesConfigSection"/>
<!--MessagesConfigSection-->
<xs:complexType name="MessagesConfigSection">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="providers" minOccurs="1"maxOccurs="1"> <xs:complexType>
<xs:sequence>
<xs:element name="add" type="MessageProvider"minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute name="defaultProvider" type="xs:string"use="required">
<xs:annotation>
<xs:documentation>The configuration name of the default essageProvider.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="cachingMode" type="CachingMode" use="optional">
<xs:annotation>
<xs:documentation>The caching mode (None, Single, Category, All).</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<!--MessageProvider-->
<xs:complexType name="MessageProvider">
<xs:attribute name="name" use="required">
<xs:annotation>
<xs:documentation>A friendly name of MessageProvider.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="type" type="MessageProviderTypes" use="required">
<xs:annotation>
<xs:documentation>The assembly qualified name of the MessageProvider type.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="connectionString" type="xs:string" use="optional">
<xs:annotation>
<xs:documentation>[DbMessageProvider]The connection string configuration name.</xs:documentation> 
</xs:annotation>
</xs:attribute>
<xs:attribute name="applicationName" type="xs:string" use="optional">
<xs:annotation>
<xs:documentation>[DbMessageProvider]The name of application.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="source" type="xs:string" use="optional"> 
<xs:annotation>
<xs:documentation>[XMLMessageProvider] The path of the XML file to store message entry list.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<!--CachingMode-->
<xs:simpleType name="CachingMode">
<xs:restriction base="xs:string">
<xs:enumeration value="None"/>
<xs:enumeration value="Single"/>
<xs:enumeration value="Category"/>
<xs:enumeration value="All"/>
</xs:restriction> 
</xs:simpleType>
<!--MessageProviderTypes-->
<xs:simpleType name="MessageProviderTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="Artech.Messages.DbMessageProvider, Artech.Messages"/>
<xs:enumeration value="Artech.Messages.XmlMessageProvider, Artech.Messages"/>
</xs:restriction>
</xs:simpleType>
</xs:schema> 

三、应用XSD以获得职能感知的支持

如果这样一个XSD已经定义好,在对配置文件进行编辑的时候,通过VS的菜单XML\Schemas打开XML Schemas对话框。点击Add按钮将这个XSD文件添加近来,并Use属性设置成Use this schema。这样你编译配置文件的时候就可以获得只能感知的支持了,配置将会变得非常的容易,即使对配置结构不太了解的人也能根据智能感知和提示性的描述完成配置工作。

0=\颯齹€{US剉韹IN!j媁0奲韹IN!j媁=\颯齹`椦?Wl?S:N錝誰!j媁鉔x  =\颯齹Y0W衏汷縹"} 縊嶯`OKNT鄀簨闠*N篘 w`O剉鉔x 龕齹蛻鷁螾`O
来源:
说明:所有来源为 .net学习网的文章均为原创,如有转载,请在转载处标注本页地址,谢谢!
【编辑:Wyf

打赏

取消

感谢您的支持,我会做的更好!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

最新评论

共有评论0条
  • 暂无任何评论,请留下您对本文章的看法,共同参入讨论!
发表评论:
留言人:
内  容:
请输入问题 19+38=? 的结果(结果是:57)
结  果: