最近在看duwamish7,asp.net forums, dottext几个优秀的开源(微软官方)的项目
因我目前所处的技术水平的阶段的原因,我看这些项目程序,更加关注的是具体的实现
次之才是架构
比较第一篇:几个开源项目实体层实现方式比较
这次的关注点是它们存储和处理配置信息的不同方式
一,duwamish7和asp.net forums
这两者处理方式有相同之处,都是通过实现IConfigurationSectionHandler来实现配置类
配置类的代码如下:
{
private static string dbConnectionString ;
private static bool enablePageCache ;
IConfigurationSectionHandler 成员#region IConfigurationSectionHandler 成员
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
NameValueCollection settings ;
try
{
NameValueSectionHandler baseHandler = new NameValueSectionHandler() ;
settings = (NameValueCollection)baseHandler.Create(parent,configContext,section) ;
}
catch
{
settings = null ;
}
if (settings != null)
{
dbConnectionString = (string)settings["dbConnectionString"] ;
enablePageCache = Convert.ToBoolean(settings["enablePageCache"]) ;
}
return settings ;
}
#endregion
public static string ConnectionString
{
get { return dbConnectionString ; }
}
public static bool EnablePageCache
{
get { return enablePageCache ; }
}
}
<configuration>
<configSections>
<section name="DuwamishConfiguration" type="Duwamish7.Common.DuwamishConfiguration, Duwamish7.Common" />
</configSections>
<DuwamishConfiguration>
<add key="dbConnectionString" value="server=localhost;User ID=Duwamish7;Password=password;database=Duwamish7;Connection Reset=FALSE" />
<add key="enablePageCache" value="true" />
</DuwamishConfiguration>
<system.web>
<compilation debug="true" />
</system.web>
</configuration>
然后就可以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文件中:
<BlogConfigurationSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ConnectionString>Data Source=KWK;Database=blog;UID=sa;Pwd=sa;</ConnectionString>
<EnablePageCache>true</EnablePageCache>
</BlogConfigurationSettings>
public class BlogConfigurationSettings
{
private string _connectionString ;
public string ConnectionString
{
get { return _connectionString ; }
set { _connectionString = value ; }
}
private bool _enablePageCache ;
public bool EnablePageCache
{
get { return _enablePageCache ; }
set { _enablePageCache = value ; }
}
}
然后可以通过如下方法获得这些配置类对象:
{
//在实际的应用中,别忘了加上缓存
string filepath = context.Server.MapPath("~/blog.config");
settings = (BlogConfigurationSettings)LoadSerializedObject(typeof(BookConfigurationSettings),filepath);
return settings;
}
public static object LoadSerializedObject(Type type, string filename)
{
FileStream fs = null;
try
{
// open the stream
fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
XmlSerializer serializer = new XmlSerializer(type);
return serializer.Deserialize(fs);
}
catch(Exception e)
{
throw e;
}
finally
{
if(fs != null)
fs.Close();
}
}
至于孰优孰劣,那就看具体的应用了,这个是见人见智的问题了