无论是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
1
public class LogHandleConfigSection : ConfigurationSection
2
{
3
public LogHandleConfigSection()
{ }
4
5
[ConfigurationProperty("ReportHandle")]
6
public ReportHandleElement ReportHandle
7
{
8
get
9
{
10
return (ReportHandleElement)this["ReportHandle"];
11
}
12
set
13
{
14
this["ReportHandle"] = value;
15
}
16
}
17
18
[ConfigurationProperty("ErrorHandle")]
19
public ErrorHandleElement ErrorHandle
20
{
21
get
22
{
23
return (ErrorHandleElement)this["ErrorHandle"];
24
}
25
set
26
{
27
this["ErrorHandle"] = value;
28
}
29
}
30
}
31
32
public class ReportHandleElement : ConfigurationElement
33
{
34
[ConfigurationProperty("Use", IsRequired = true)]
35
public bool Use
36
{
37
get
38
{
39
return (bool)this["Use"];
40
}
41
set
42
{
43
this["Use"] = value;
44
}
45
}
46
/**//// <summary>
47
/// 表示刷新频率
48
/// 有2中使用方式:
49
/// 1.使用interval作为判断依据,这样可能会出现漏洞。
50
/// 2.使用interval*2作为判断依据,然后规定每个interval时间内只能有一次响应。
51
/// </summary>
52
[ConfigurationProperty("Interval", IsRequired = true)]
53
public int Interval
54
{
55
get
56
{
57
return (int)this["Interval"];
58
}
59
set
60
{
61
this["Interval"] = value;
62
}
63
}
64
65
[ConfigurationProperty("ReportTimes")]
66
public ReportTimeElementCollection ReportTimes
67
{
68
get
69
{
70
return (ReportTimeElementCollection)this["ReportTimes"];
71
}
72
set
73
{
74
this["ReportTimes"] = value;
75
}
76
}
77
[ConfigurationProperty("ManualHandleType")]
78
public string ManualHandleType
79
{
80
get
81
{
82
return (string)this["ManualHandleType"];
83
}
84
set
85
{
86
this["ManualHandleType"] = value;
87
}
88
}
89
}
90
91
public class ReportTimeElementCollection : ConfigurationElementCollection
92
{
93
94
protected override ConfigurationElement CreateNewElement()
95
{
96
return new ReportTimeElement();
97
}
98
99
protected override object GetElementKey(ConfigurationElement element)
100
{
101
return ((ReportTimeElement)element).ID;
102
}
103
}
104
105
public class ReportTimeElement : ConfigurationElement
106
{
107
[ConfigurationProperty("ID", IsKey = true, IsRequired = true)]
108
public int ID
109
{
110
get
111
{
112
return (int)this["ID"];
113
}
114
set
115
{
116
this["ID"] = value;
117
}
118
}
119
120
[ConfigurationProperty("Hour")]
121
public double Hour
122
{
123
get
124
{
125
return (double)this["Hour"];
126
}
127
set
128
{
129
this["Hour"] = value;
130
}
131
}
132
133
[ConfigurationProperty("Minute")]
134
public double Minute
135
{
136
get
137
{
138
return (double)this["Minute"];
139
}
140
set
141
{
142
this["Minute"] = value;
143
}
144
}
145
146
[ConfigurationProperty("HandleType")]
147
public string HandleType
148
{
149
get
150
{
151
return (string)this["HandleType"];
152
}
153
set
154
{
155
this["HandleType"] = value;
156
}
157
}
158
159
}
160
161
public class ErrorHandleElement : ConfigurationElement
162
{
163
[ConfigurationProperty("Use", IsRequired = true)]
164
public bool Use
165
{
166
get
167
{
168
return (bool)this["Use"];
169
}
170
set
171
{
172
this["Use"] = value;
173
}
174
}
175
176
[ConfigurationProperty("Rules")]
177
public RuleElementCollection Rules
178
{
179
get
180
{
181
return (RuleElementCollection)this["Rules"];
182
}
183
set
184
{
185
this["Rules"] = value;
186
}
187
}
188
}
189
190
public class RuleElementCollection : ConfigurationElementCollection
191
{
192
public RuleElementCollection()
{ }
193
194
protected override ConfigurationElement CreateNewElement()
195
{
196
return new RuleElement();
197
}
198
199
protected override object GetElementKey(ConfigurationElement element)
200
{
201
return ((RuleElement)element).ID;
202
203
}
204
}
205
206
public class RuleElement : ConfigurationElement
207
{
208
[ConfigurationProperty("ID", IsKey = true, IsRequired = true)]
209
public int ID
210
{
211
get
212
{
213
return (int)this["ID"];
214
}
215
set
216
{
217
this["ID"] = value;
218
}
219
}
220
221
[ConfigurationProperty("Begin")]
222
public int Begin
223
{
224
get
225
{
226
return (int)this["Begin"];
227
}
228
set
229
{
230
this["Begin"] = value;
231
}
232
}
233
234
[ConfigurationProperty("End")]
235
public int End
236
{
237
get
238
{
239
return (int)this["End"];
240
}
241
set
242
{
243
this["End"] = value;
244
}
245
}
246
247
[ConfigurationProperty("HandleType")]
248
public string HandleType
249
{
250
get
251
{
252
return (string)this["HandleType"];
253
}
254
set
255
{
256
this["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:使用验证特性。