无论是winform,还是asp.net,如果可以开发成可配置的,就可以大大减少更新程序的次数了,而.net对这方面的支持也很好,是以面向对象方式处理的。
1.配置节点,配置元素。
下面的代码示例是用于处理这样的xml配置片段
<LogHandle>
<ReportHandle Use ="true" Interval="1" ManualHandleType="EMAIL">
<ReportTimes>
<!--SMS,EMAIL,RICHEMAIL-->
<add ID="1" Hour= "8" Minute ="30" HandleType="EMAIL"/>
<add ID="2" Hour= "12" Minute ="0" HandleType="EMAIL"/>
<add ID="3" Hour= "17" Minute ="0" HandleType="EMAIL"/>
</ReportTimes>
</ReportHandle>
<ErrorHandle Use ="true">
<Rules>
<!--SMS,EMAIL,MSMQ-->
<add ID="1" Begin= "1" End= "4" HandleType ="MSMQ"/>
<!--错误小于等于0~3次 重发消息-->
<add ID="2" Begin= "3" End= "4" HandleType ="EMAIL"/>
<!--错误小于等于3~4次 发邮件-->
<!--<add ID="3" Begin= "5" End= "5" HandleType ="SMS"/>
--><!--错误小于等于4~5次 发短信-->
</Rules>
</ErrorHandle>
</LogHandle>根据xml片段的结构,应该增加下面这些类。
ConfigSection
1public class LogHandleConfigSection : ConfigurationSection
2![]()
{
3public LogHandleConfigSection()
{ }
4
5[ConfigurationProperty("ReportHandle")]
6public ReportHandleElement ReportHandle
7![]()
{
8get
9![]()
{
10return (ReportHandleElement)this["ReportHandle"];
11}
12set
13![]()
{
14this["ReportHandle"] = value;
15}
16}
17
18[ConfigurationProperty("ErrorHandle")]
19public ErrorHandleElement ErrorHandle
20![]()
{
21get
22![]()
{
23return (ErrorHandleElement)this["ErrorHandle"];
24}
25set
26![]()
{
27this["ErrorHandle"] = value;
28}
29}
30}
31
32public class ReportHandleElement : ConfigurationElement
33![]()
{
34[ConfigurationProperty("Use", IsRequired = true)]
35public bool Use
36![]()
{
37get
38![]()
{
39return (bool)this["Use"];
40}
41set
42![]()
{
43this["Use"] = value;
44}
45}
46/**//// <summary>
47/// 表示刷新频率
48/// 有2中使用方式:
49/// 1.使用interval作为判断依据,这样可能会出现漏洞。
50/// 2.使用interval*2作为判断依据,然后规定每个interval时间内只能有一次响应。
51/// </summary>
52[ConfigurationProperty("Interval", IsRequired = true)]
53public int Interval
54![]()
{
55get
56![]()
{
57return (int)this["Interval"];
58}
59set
60![]()
{
61this["Interval"] = value;
62}
63}
64
65[ConfigurationProperty("ReportTimes")]
66public ReportTimeElementCollection ReportTimes
67![]()
{
68get
69![]()
{
70return (ReportTimeElementCollection)this["ReportTimes"];
71}
72set
73![]()
{
74this["ReportTimes"] = value;
75}
76}
77[ConfigurationProperty("ManualHandleType")]
78public string ManualHandleType
79![]()
{
80get
81![]()
{
82return (string)this["ManualHandleType"];
83}
84set
85![]()
{
86this["ManualHandleType"] = value;
87}
88}
89}
90
91public class ReportTimeElementCollection : ConfigurationElementCollection
92![]()
{
93
94protected override ConfigurationElement CreateNewElement()
95![]()
{
96return new ReportTimeElement();
97}
98
99protected override object GetElementKey(ConfigurationElement element)
100![]()
{
101return ((ReportTimeElement)element).ID;
102}
103}
104
105public class ReportTimeElement : ConfigurationElement
106![]()
{
107[ConfigurationProperty("ID", IsKey = true, IsRequired = true)]
108public int ID
109![]()
{
110get
111![]()
{
112return (int)this["ID"];
113}
114set
115![]()
{
116this["ID"] = value;
117}
118}
119
120[ConfigurationProperty("Hour")]
121public double Hour
122![]()
{
123get
124![]()
{
125return (double)this["Hour"];
126}
127set
128![]()
{
129this["Hour"] = value;
130}
131}
132
133[ConfigurationProperty("Minute")]
134public double Minute
135![]()
{
136get
137![]()
{
138return (double)this["Minute"];
139}
140set
141![]()
{
142this["Minute"] = value;
143}
144}
145
146[ConfigurationProperty("HandleType")]
147public string HandleType
148![]()
{
149get
150![]()
{
151return (string)this["HandleType"];
152}
153set
154![]()
{
155this["HandleType"] = value;
156}
157}
158
159}
160
161public class ErrorHandleElement : ConfigurationElement
162![]()
{
163[ConfigurationProperty("Use", IsRequired = true)]
164public bool Use
165![]()
{
166get
167![]()
{
168return (bool)this["Use"];
169}
170set
171![]()
{
172this["Use"] = value;
173}
174}
175
176[ConfigurationProperty("Rules")]
177public RuleElementCollection Rules
178![]()
{
179get
180![]()
{
181return (RuleElementCollection)this["Rules"];
182}
183set
184![]()
{
185this["Rules"] = value;
186}
187}
188}
189
190public class RuleElementCollection : ConfigurationElementCollection
191![]()
{
192public RuleElementCollection()
{ }
193
194protected override ConfigurationElement CreateNewElement()
195![]()
{
196return new RuleElement();
197}
198
199protected override object GetElementKey(ConfigurationElement element)
200![]()
{
201return ((RuleElement)element).ID;
202
203}
204}
205
206public class RuleElement : ConfigurationElement
207![]()
{
208[ConfigurationProperty("ID", IsKey = true, IsRequired = true)]
209public int ID
210![]()
{
211get
212![]()
{
213return (int)this["ID"];
214}
215set
216![]()
{
217this["ID"] = value;
218}
219}
220
221[ConfigurationProperty("Begin")]
222public int Begin
223![]()
{
224get
225![]()
{
226return (int)this["Begin"];
227}
228set
229![]()
{
230this["Begin"] = value;
231}
232}
233
234[ConfigurationProperty("End")]
235public int End
236![]()
{
237get
238![]()
{
239return (int)this["End"];
240}
241set
242![]()
{
243this["End"] = value;
244}
245}
246
247[ConfigurationProperty("HandleType")]
248public string HandleType
249![]()
{
250get
251![]()
{
252return (string)this["HandleType"];
253}
254set
255![]()
{
256this["HandleType"] = value;
257}
258}
259}
260
2.如果在xml中声明配置节点
<configSections>
<section name="LogHandle" type="WMSJIEKOUDao.BLL.Configuration.LogHandleConfigSection,WMSJIEKOUDao" />
</configSections>3.程序加载方式
config = (LogHandleConfigSection)ConfigurationManager.GetSection("LogHandle");
循环处理
foreach (RuleElement element in config.ErrorHandle.Rules)
{
if (count >= element.Begin && count <= element.End)
{
errorHandler = ErrorHandlerFactory.GetErrorHandler(element.HandleType);
errorHandler.Handle(curErrorLog);
}
}
这样这个配置模块的基础部分就开发完了,但是还是有几个问题需要研究一下
1.如何处理上面的HandleType的内容,在实际使用的时候,是使用枚举,类型,还是字符串?
answer:
使用枚举 或者使用字符串都可以,本来想研究一下FormsAuthenticationModule,这些.net标准模块是怎么处理config的,结果发现.net使用的方式是直接处理xmlnode。
2.如何处理isrequire的情况,如果没有某一个元素,那么怎么处理,怎么判断
answer:
3.config的提示文档,xsd是如何创建的。answer: 可以参考一些现成的比如ibatis里面的
xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
targetNamespace="http://ibatis.apache.org/dataAccess"
elementFormDefault="qualified"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://ibatis.apache.org/dataAccess"
xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense"
vs:friendlyname="iBATIS.NET DataAccess file Configuration Schema"
vs:ishtmlschema="false"
vs:iscasesensitive="true"
vs:requireattributequotes="true"
vs:defaultnamespacequalifier=""
vs:defaultnsprefix="" >
<xs:element name="providers">
<xs:complexType>
<xs:attribute name="resource" type="xs:string"/>
<xs:attribute name="url" type="xs:string"/>
<xs:attribute name="embedded" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="context">
<xs:complexType>
<xs:sequence>
<xs:element ref="properties" minOccurs="0"/>
<xs:element ref="database"/>
<xs:element ref="daoSessionHandler" minOccurs="0"/>
<xs:element ref="daoFactory"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="default" type="xs:boolean"/>
</xs:complexType>
</xs:element>
<xs:element name="dao">
<xs:complexType>
<xs:attribute name="interface" type="xs:string" use="required"/>
<xs:attribute name="implementation" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="daoConfig">
<xs:complexType>
<xs:sequence>
<xs:element ref="daoSessionHandlers" minOccurs="0"/>
<xs:element ref="providers" minOccurs="0" maxOccurs="1"/>
<xs:element ref="context" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="daoSessionHandlers">
<xs:complexType>
<xs:sequence>
<xs:element ref="handler"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="handler">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="implementation" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="daoFactory">
<xs:complexType>
<xs:sequence>
<xs:element ref="dao" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="daoSessionHandler">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="dataSource">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="connectionString" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="database">
<xs:complexType>
<xs:sequence>
<xs:element ref="provider" minOccurs="0"/>
<xs:element ref="dataSource"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="properties">
<xs:complexType>
<xs:attribute name="resource" type="xs:string"/>
<xs:attribute name="url" type="xs:string"/>
<xs:attribute name="embedded" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="property">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="value" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="provider">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>4.如果加入验证
answer:使用验证特性。