基于XML的配置文件访问接口设计和实现(3)
目录
摘要
增加缓存支持
增加配置文件监视
增加ConfigurationSettings类
摘要
前面的两篇中,我们实现了XmlConfigReader和XmlConfigWriter的基本功能.由于XmlConfigReader的实现方式是每请求一次,就去解析配置文件一次,性能很低下.同时,为了更方便使用,我们增加一个ConfigurationSettings类,用来调用XmlConfigReader和XmlConfigWriter,使之用起来和System.Configuration中的类使用方式一样.
增加缓存支持
由于XmlConfigReader的实现方式是请求一次,解析配置文件一次,而且配置文件的信息在程序运行的时会大量使用,这样子显然效率太低.因此,这里就要使用到缓存.
缓存,其实就相当于一个静态的变量,在整个程序运行时是唯一的,通过这样的一个变量,把信息存储到这个变量里面,在程序的其它地方就可以直接得到这个信息了.从而避免了频繁的解析配置文件.这里,我们选择使用Hashtable做为缓存变量.
在MSDN中,我们可以查到System.Configuration命名空间中的AppSettings类返回的是一个NameValueCollection(Key/Value键值对).为了方便使用,我们将配置文件解析后的信息也存成NameValueCollection这样的集合.
这样定义好了后,对于Hashtable中的Key设置为Section节点的名字(appSettings/SockBaseSettings),其Value值即为此节点的所有子节点的NameValueCollection类的对象.
修改代码.给XmlConfigReader增加一个静态Hashtable变量,并修改相关函数.把得到的信息直接以NameValueCollection的形式存入到此Hashtable中.
private static Hashtable confTypes = new Hashtable();
private string rootname;
public void Process(){
XmlTextReader reader = new XmlTextReader(_filepath);
while( reader.Read()){
if( reader.IsStartElement()){
#region Analyze the files
if( reader.Prefix == String.Empty)
{
if( reader.LocalName == "configuration")
{
inConfiguration = true;
}
else if( inConfiguration == true){
if(reader.LocalName == "add")
{
if( reader.GetAttribute("key") == null || reader.GetAttribute("value") == null)
{
throw new Exception(rootname + " key or value is null");
}
AddKey(tables,reader.GetAttribute("key"),reader.GetAttribute("value"));
}
else
{
rootname = reader.LocalName;
}
}
}
#endregion
}
else if ( reader.LocalName == "configuration"){
inConfiguration = false;
}
}
reader.Close();
}
private void AddKey(string key,string value){
NameValueCollection collection ;
if(confTypes.ContainsKey( rootname )){
collection = (NameValueCollection) confTypes [rootname];
}
else{
lock(confTypes.SyncRoot){
collection = new NameValueCollection();
confTypes.Add( rootname,collection);
}
}
collection.Add(key,value);
}
上面代码中,我们修改了Process函数.把原来的直接return结果的地方改成调用AddKey函数.通过一个类成员 rootname临时储存当前的SectionName,通过AddKey把得到的Key/Value加入到Hashtable中.
现在这样修改后,就不能直接通过Process得到我们想到得到的Key的Value了.所以我们再写一个函数,
public NameValueCollection GetCollection(string SectionName){
if( confTypes.ContainsKey(SectionName)){
return (NameValueCollection)confTypes[SectionName];
}
else{
throw new Exception(confName + " is not found in XmlConfiguration files");
}
}
这里,我们直接通过SectionName得到此节点所有的子节点的NameValueCollection集合.这样,我们就可以得到我们想要的值了.
增加配置文件监视
上面的代码实现了配置文件的缓存.大大提高了灵活性.但是存在一个问题,就是,如果配置文件修改了,这个缓存不会自动更新.
要解决这个问题,我们得使用FileSystemWatcher这个类,用来订阅文件修改消息,进而更新缓存.由于在第一次解析前就要把此配置文件加入到监视文件表中,所以我们修改XmlConfigReader,增加一个静态的FileSystemWatcher,用来保存监视文件的对象,增加一个静态的Bool值表明是否修改过.再修改构造函数,使配置文件在一开始就加入到监视列表中.代码如下:
Private static FileSystemWatcher watch = new FileSystemWatcher();
Private static bool isModify = true;
public XmlConfigReader(string filepath){
_filepath = Path.GetFullPath(filepath).ToUpper();
watch.IncludeSubdirectories = false;
watch.Path = Path.GetDirectoryName(filepath);
watch.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;
watch.Filter = Path.GetFileName(filepath);
watch.Changed += new FileSystemEventHandler(Change_Even);
watch.EnableRaisingEvents = true;
}
由于是通过事件机制实现文件修改通知的,所以我们还要实现Chane_Even这个函数,通过这个函数修改isModify的值.
private void Change_Even(object sender, FileSystemEventArgs e){
isModify = true;
}
这样子,对于配置文件的监视的代码就完成了,现在就是修改我们的GetCollection代码的时候了.
修改后的代码如下:
public NameValueCollection GetCollection(string SectionName){
if( isModify ){
lock(confTypes.SyncRoot){
confTypes.Clear();
Process();
}
isModify = false;
}
if( confTypes.ContainsKey(SectionName)){
return (NameValueCollection)confTypes[SectionName];
}
else{
throw new Exception(confName + " is not found in XmlConfiguration files");
}
}
到现在,整个XmlConfigReader的代码已完成了,可以实现对文件的监视,从而动态修改缓存中的值.
增加ConfigurationSettings类
为了便于使用,我们增加了一个ConfigurationSettings的类,使用他的用法和System.Configuration命名空间中的类的用法一样.代码定义如下:
public class ConfigurationSettings : XmlConfigWriter
{
private static string _filepath = @"AppConfig.xml";
public static string DefaultFilePath
private static XmlConfigReader reader;
{
get{return _filepath;}
set{_filepath = Path.GetFullPath(value);}
}
public static NameValueCollection AppSettings
{
get{
if( reader == null){
reader = new XmlConfigReader(DefaultFilePath);
}
return reader.GetCollection("appSettings");
}
}
public static NameValueCollection GetConfig(string sectionName){
get{
if( reader == null){
reader = new XmlConfigReader(DefaultFilePath);
}
return reader.GetCollection(sectionName);
}
}
这样,使用起来就和系统自带的一样了.
总结