配置模块开发(原创)

无论是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片段的结构,应该增加下面这些类。

 

ContractedBlock.gifExpandedBlockStart.gifConfigSection
  1public class LogHandleConfigSection : ConfigurationSection 
  2ExpandedBlockStart.gifContractedBlock.gif  
  3ExpandedSubBlockStart.gifContractedSubBlock.gif      public LogHandleConfigSection() { } 
  4
  5      [ConfigurationProperty("ReportHandle")] 
  6      public ReportHandleElement ReportHandle 
  7ExpandedSubBlockStart.gifContractedSubBlock.gif      
  8          get 
  9ExpandedSubBlockStart.gifContractedSubBlock.gif          
 10              return (ReportHandleElement)this["ReportHandle"]; 
 11          }
 
 12          set 
 13ExpandedSubBlockStart.gifContractedSubBlock.gif          
 14              this["ReportHandle"= value; 
 15          }
 
 16      }
 
 17
 18      [ConfigurationProperty("ErrorHandle")] 
 19      public ErrorHandleElement ErrorHandle 
 20ExpandedSubBlockStart.gifContractedSubBlock.gif      
 21          get 
 22ExpandedSubBlockStart.gifContractedSubBlock.gif          
 23              return (ErrorHandleElement)this["ErrorHandle"]; 
 24          }
 
 25          set 
 26ExpandedSubBlockStart.gifContractedSubBlock.gif          
 27              this["ErrorHandle"= value; 
 28          }
 
 29      }
 
 30  }
 
 31
 32  public class ReportHandleElement : ConfigurationElement 
 33ExpandedBlockStart.gifContractedBlock.gif  
 34      [ConfigurationProperty("Use", IsRequired = true)] 
 35      public bool Use 
 36ExpandedSubBlockStart.gifContractedSubBlock.gif      
 37          get 
 38ExpandedSubBlockStart.gifContractedSubBlock.gif          
 39              return (bool)this["Use"]; 
 40          }
 
 41          set 
 42ExpandedSubBlockStart.gifContractedSubBlock.gif          
 43              this["Use"= value; 
 44          }
 
 45      }
 
 46ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//// <summary> 
 47      /// 表示刷新频率 
 48      /// 有2中使用方式: 
 49      ///     1.使用interval作为判断依据,这样可能会出现漏洞。 
 50      ///     2.使用interval*2作为判断依据,然后规定每个interval时间内只能有一次响应。 
 51      /// </summary> 

 52      [ConfigurationProperty("Interval", IsRequired = true)] 
 53      public int Interval 
 54ExpandedSubBlockStart.gifContractedSubBlock.gif      
 55          get 
 56ExpandedSubBlockStart.gifContractedSubBlock.gif          
 57              return (int)this["Interval"]; 
 58          }
 
 59          set 
 60ExpandedSubBlockStart.gifContractedSubBlock.gif          
 61              this["Interval"= value; 
 62          }
 
 63      }
 
 64
 65      [ConfigurationProperty("ReportTimes")] 
 66      public ReportTimeElementCollection ReportTimes 
 67ExpandedSubBlockStart.gifContractedSubBlock.gif      
 68          get 
 69ExpandedSubBlockStart.gifContractedSubBlock.gif          
 70              return (ReportTimeElementCollection)this["ReportTimes"]; 
 71          }
 
 72          set 
 73ExpandedSubBlockStart.gifContractedSubBlock.gif          
 74              this["ReportTimes"= value; 
 75          }
 
 76      }
 
 77      [ConfigurationProperty("ManualHandleType")] 
 78      public string ManualHandleType 
 79ExpandedSubBlockStart.gifContractedSubBlock.gif      
 80          get 
 81ExpandedSubBlockStart.gifContractedSubBlock.gif          
 82              return (string)this["ManualHandleType"]; 
 83          }
 
 84          set 
 85ExpandedSubBlockStart.gifContractedSubBlock.gif          
 86              this["ManualHandleType"= value; 
 87          }
 
 88      }
 
 89  }
 
 90
 91  public class ReportTimeElementCollection : ConfigurationElementCollection 
 92ExpandedBlockStart.gifContractedBlock.gif  
 93
 94      protected override ConfigurationElement CreateNewElement() 
 95ExpandedSubBlockStart.gifContractedSubBlock.gif      
 96          return new ReportTimeElement(); 
 97      }
 
 98
 99      protected override object GetElementKey(ConfigurationElement element) 
100ExpandedSubBlockStart.gifContractedSubBlock.gif      
101          return ((ReportTimeElement)element).ID; 
102      }
 
103  }
 
104
105  public class ReportTimeElement : ConfigurationElement 
106ExpandedBlockStart.gifContractedBlock.gif  
107      [ConfigurationProperty("ID", IsKey = true, IsRequired = true)] 
108      public int ID 
109ExpandedSubBlockStart.gifContractedSubBlock.gif      
110          get 
111ExpandedSubBlockStart.gifContractedSubBlock.gif          
112              return (int)this["ID"]; 
113          }
 
114          set 
115ExpandedSubBlockStart.gifContractedSubBlock.gif          
116              this["ID"= value; 
117          }
 
118      }
 
119
120      [ConfigurationProperty("Hour")] 
121      public double Hour 
122ExpandedSubBlockStart.gifContractedSubBlock.gif      
123          get 
124ExpandedSubBlockStart.gifContractedSubBlock.gif          
125              return (double)this["Hour"]; 
126          }
 
127          set 
128ExpandedSubBlockStart.gifContractedSubBlock.gif          
129              this["Hour"= value; 
130          }
 
131      }
 
132
133      [ConfigurationProperty("Minute")] 
134      public double Minute 
135ExpandedSubBlockStart.gifContractedSubBlock.gif      
136          get 
137ExpandedSubBlockStart.gifContractedSubBlock.gif          
138              return (double)this["Minute"]; 
139          }
 
140          set 
141ExpandedSubBlockStart.gifContractedSubBlock.gif          
142              this["Minute"= value; 
143          }
 
144      }
 
145
146      [ConfigurationProperty("HandleType")] 
147      public string HandleType 
148ExpandedSubBlockStart.gifContractedSubBlock.gif      
149          get 
150ExpandedSubBlockStart.gifContractedSubBlock.gif          
151              return (string)this["HandleType"]; 
152          }
 
153          set 
154ExpandedSubBlockStart.gifContractedSubBlock.gif          
155              this["HandleType"= value; 
156          }
 
157      }
 
158
159  }
 
160
161  public class ErrorHandleElement : ConfigurationElement 
162ExpandedBlockStart.gifContractedBlock.gif  
163      [ConfigurationProperty("Use", IsRequired = true)] 
164      public bool Use 
165ExpandedSubBlockStart.gifContractedSubBlock.gif      
166          get 
167ExpandedSubBlockStart.gifContractedSubBlock.gif          
168              return (bool)this["Use"]; 
169          }
 
170          set 
171ExpandedSubBlockStart.gifContractedSubBlock.gif          
172              this["Use"= value; 
173          }
 
174      }
 
175
176      [ConfigurationProperty("Rules")] 
177      public RuleElementCollection Rules 
178ExpandedSubBlockStart.gifContractedSubBlock.gif      
179          get 
180ExpandedSubBlockStart.gifContractedSubBlock.gif          
181              return (RuleElementCollection)this["Rules"]; 
182          }
 
183          set 
184ExpandedSubBlockStart.gifContractedSubBlock.gif          
185              this["Rules"= value; 
186          }
 
187      }
 
188  }
 
189
190  public class RuleElementCollection : ConfigurationElementCollection 
191ExpandedBlockStart.gifContractedBlock.gif  
192ExpandedSubBlockStart.gifContractedSubBlock.gif      public RuleElementCollection() { } 
193
194      protected override ConfigurationElement CreateNewElement() 
195ExpandedSubBlockStart.gifContractedSubBlock.gif      
196          return new RuleElement(); 
197      }
 
198
199      protected override object GetElementKey(ConfigurationElement element) 
200ExpandedSubBlockStart.gifContractedSubBlock.gif      
201          return ((RuleElement)element).ID; 
202
203      }
 
204  }
 
205
206  public class RuleElement : ConfigurationElement 
207ExpandedBlockStart.gifContractedBlock.gif  
208      [ConfigurationProperty("ID", IsKey = true, IsRequired = true)] 
209      public int ID 
210ExpandedSubBlockStart.gifContractedSubBlock.gif      
211          get 
212ExpandedSubBlockStart.gifContractedSubBlock.gif          
213              return (int)this["ID"]; 
214          }
 
215          set 
216ExpandedSubBlockStart.gifContractedSubBlock.gif          
217              this["ID"= value; 
218          }
 
219      }
 
220
221      [ConfigurationProperty("Begin")] 
222      public int Begin 
223ExpandedSubBlockStart.gifContractedSubBlock.gif      
224          get 
225ExpandedSubBlockStart.gifContractedSubBlock.gif          
226              return (int)this["Begin"]; 
227          }
 
228          set 
229ExpandedSubBlockStart.gifContractedSubBlock.gif          
230              this["Begin"= value; 
231          }
 
232      }
 
233
234      [ConfigurationProperty("End")] 
235      public int End 
236ExpandedSubBlockStart.gifContractedSubBlock.gif      
237          get 
238ExpandedSubBlockStart.gifContractedSubBlock.gif          
239              return (int)this["End"]; 
240          }
 
241          set 
242ExpandedSubBlockStart.gifContractedSubBlock.gif          
243              this["End"= value; 
244          }
 
245      }
 
246
247      [ConfigurationProperty("HandleType")] 
248      public string HandleType 
249ExpandedSubBlockStart.gifContractedSubBlock.gif      
250          get 
251ExpandedSubBlockStart.gifContractedSubBlock.gif          
252              return (string)this["HandleType"]; 
253          }
 
254          set 
255ExpandedSubBlockStart.gifContractedSubBlock.gif          
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里面的

 


ContractedBlock.gifExpandedBlockStart.gifxsd
<?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:使用验证特性。

转载于:https://www.cnblogs.com/zhuispeed/archive/2009/11/04/1595881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值