几个开源项目配置信息的存储和处理的方式

最近在看duwamish7,asp.net forums, dottext几个优秀的开源(微软官方)的项目
因我目前所处的技术水平的阶段的原因,我看这些项目程序,更加关注的是具体的实现
次之才是架构

比较第一篇:几个开源项目实体层实现方式比较

这次的关注点是它们存储和处理配置信息的不同方式

一,duwamish7和asp.net forums
这两者处理方式有相同之处,都是通过实现IConfigurationSectionHandler来实现配置类
配置类的代码如下:

None.gif    public class  DuwamishConfiguration : IConfigurationSectionHandler
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
{
InBlock.gif        
private static string
 dbConnectionString  ;
InBlock.gif        
private static bool
 enablePageCache ;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IConfigurationSectionHandler 成员
#region IConfigurationSectionHandler 成员
InBlock.gif
InBlock.gif        
public object Create(object parent, object configContext, System.Xml.XmlNode section)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
InBlock.gif            NameValueCollection settings ;
InBlock.gif            
try

ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NameValueSectionHandler baseHandler 
= new
 NameValueSectionHandler() ;
InBlock.gif                settings 
=
 (NameValueCollection)baseHandler.Create(parent,configContext,section) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                settings 
= null
 ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (settings != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif                dbConnectionString 
= (string)settings["dbConnectionString"
] ;
InBlock.gif                enablePageCache    
= Convert.ToBoolean(settings["enablePageCache"
]) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return settings ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
public static string ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn dbConnectionString ; }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool EnablePageCache
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn enablePageCache ; }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
web.config如下:
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<configuration>
None.gif  
<configSections>
None.gif     
<section name="DuwamishConfiguration" type="Duwamish7.Common.DuwamishConfiguration, Duwamish7.Common"  />
None.gif  
</configSections>
None.gif  
<DuwamishConfiguration>
None.gif     
<add key="dbConnectionString" value="server=localhost;User ID=Duwamish7;Password=password;database=Duwamish7;Connection Reset=FALSE" />
None.gif     
<add key="enablePageCache" value="true" />
None.gif  
</DuwamishConfiguration>
None.gif  
<system.web>  
None.gif  
<compilation debug="true" />

None.gif 
</system.web>
None.gif
</configuration>
None.gif

然后就可以DuwamishConfiguration.ConnectionString获得数据库连接,DuwamishConfiguration.xxxx获得你定义的其他
数据了,不过这样用之前,需要先调用这个方法哦
System.Configuration.ConfigurationSettings.GetConfig("DuwamishConfiguration") ;
通常这个方法会放在Global.asa的application_start事件处理中,或者自己定义的
httpmodule的application_start类似事件中

关于duwamish7配置信息处理的更多信息,可以参考:
Duwamish深入剖析-配置篇
由Duwamish学习web.config的配置

二,dottext配置信息的存储和处理
dottext配置信息不是放在web.config,而是放在一个自己定义的blog.config文件中:

None.gif<?xml version="1.0" encoding="utf-8" ?>  
None.gif
<BlogConfigurationSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

None.gif  
<ConnectionString>Data Source=KWK;Database=blog;UID=sa;Pwd=sa;</ConnectionString>
None.gif  
<EnablePageCache>true</EnablePageCache>
None.gif
</BlogConfigurationSettings>
然后通过串行化的方式获取数据,先定义对应的类:
None.gif [Serializable]
None.gif
public class
 BlogConfigurationSettings
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
{
InBlock.gif   
private string
 _connectionString ;
InBlock.gif   
public string
 ConnectionString
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
get dot.gifreturn _connectionString ; }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
set dot.gif{ _connectionString = value ; }
ExpandedSubBlockEnd.gif   }

InBlock.gif   
InBlock.gif   
private bool _enablePageCache ;
InBlock.gif   
public bool
 EnablePageCache
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif       
get dot.gifreturn _enablePageCache ; }

ExpandedSubBlockStart.gifContractedSubBlock.gif       
set dot.gif{ _enablePageCache = value ; }
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif

然后可以通过如下方法获得这些配置类对象:
None.gif                public static  BlogConfigurationSettings Instance(HttpContext context)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif                      
//在实际的应用中,别忘了加上缓存

InBlock.gif
              string filepath = context.Server.MapPath("~/blog.config");
InBlock.gif              settings 
= (BlogConfigurationSettings)LoadSerializedObject(typeof
(BookConfigurationSettings),filepath);
InBlock.gif              
return
 settings;
ExpandedBlockEnd.gif        }

None.gif        
public static object LoadSerializedObject(Type type, string  filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            FileStream fs 
= null
;
InBlock.gif            
try

ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
// open the streamdot.gif

InBlock.gif
                fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
InBlock.gif                XmlSerializer serializer 
= new
 XmlSerializer(type);
InBlock.gif                
return
 serializer.Deserialize(fs);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif                
throw
 e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(fs != null
)
InBlock.gif                    fs.Close();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

至于孰优孰劣,那就看具体的应用了,这个是见人见智的问题了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值