这个框架用来解决自己的配置问题,为整个CWF框架的底层服务构架,他为上面的数据持久和缓存或者其它服务提供配置信息.
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
namespace CWF.ConfigManager
{
/// <summary>
/// 配置文件自定管理类
/// 开发者:欧元寒玟
/// 开发时间:2010.02.05
/// 最后修改时间:2010.02.05
/// </summary>
public class ConfigManagerHander:IConfigurationSectionHandler
{
#region IConfigurationSectionHandler 成员
/// <summary>
/// 实现接口的方法处理xml入口
/// </summary>
/// <param name="parent"></param>
/// <param name="configContext"></param>
/// <param name="section"></param>
/// <returns></returns>
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
return new ConfigManager(section);
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection ;
namespace CWF.ConfigManager
{
/// <summary>
/// 实现配置文件处理的主要类
/// 开发者:欧元寒玟
/// 开发时间:2010.02.05
/// 最后修改时间:2010.02.05
/// </summary>
public class ConfigManager
{
/// <summary>
/// 配置文件根节点
/// </summary>
private XmlNode section;
/// <summary>
/// 实例化
/// </summary>
/// <param name="Section"></param>
public ConfigManager(XmlNode Section)
{
section = Section;
}
/// <summary>
/// 获取类工厂
/// </summary>
/// <returns></returns>
public ClassFactory getFactory()
{
XmlNode factory = section.SelectSingleNode("//classFactory");
return new ClassFactory(factory);
}
/// <summary>
/// 获取Windows服务
/// </summary>
/// <returns></returns>
public WinServiceConfig getWinService()
{
XmlNode ServiceNode = section.SelectSingleNode("//windowsSercice");
return new WinServiceConfig(ServiceNode);
}
/// <summary>
/// 获取缓存对象类
/// </summary>
/// <returns></returns>
public CacheConfig getCacheConfig()
{
XmlNode ServiceNode = section.SelectSingleNode("//cacheService");
return new CacheConfig(ServiceNode);
}
}
/// <summary>
/// 类工厂服务
/// </summary>
public class ClassFactory
{
private XmlNode node;
/// <summary>
/// 实例化
/// </summary>
/// <param name="factoryNode"></param>
public ClassFactory(XmlNode factoryNode)
{
node = factoryNode;
}
/// <summary>
/// 获取类
/// </summary>
/// <param name="className">类名</param>
/// <returns>object</returns>
public object getClass(string className)
{
XmlNode classNode = node.SelectSingleNode("//class[@name='"+className+"']");
object classObject = null;
if(classNode !=null)
{
string StrType= classNode.Attributes["type"].Value;
string url = null;
Type type = Type.GetType(StrType);
if (classNode.Attributes["url"] != null && classNode.Attributes["url"].Value != "")
{
url = classNode.Attributes["url"].Value;
try
{
classObject = Activator.GetObject(type, url);
}
catch
{
}
}
else
{
try
{
classObject = Activator.CreateInstance(type, null);
}
catch
{
}
}
}
return classObject;
}
/// <summary>
/// 获取一个类工厂组的所有实例
/// </summary>
/// <param name="GroupName">组名</param>
/// <returns></returns>
public List<object> getClassGroup(string GroupName)
{
XmlNode GroupNode = node.SelectSingleNode("//classGroup[@name='" + GroupName + "']");
List<object> list = new List<object>();
for (int i = 0; i < GroupNode.ChildNodes.Count; i++)
{
if (GroupNode.ChildNodes[i].Attributes["type"].Value != "")
{
Type type = Type.GetType(GroupNode.ChildNodes[i].Attributes["type"].Value);
list.Add(Activator.CreateInstance(type,null));
}
}
return list;
}
}
/// <summary>
/// Windows Services服务
/// </summary>
public class WinServiceConfig
{
private XmlNode RootNode;
public WinServiceConfig(XmlNode ServiceNode)
{
RootNode = ServiceNode;
}
/// <summary>
/// 获取所有服务的列表
/// </summary>
/// <returns></returns>
public List<object> getIServers()
{
List<object> objServerList=new List<object>();
foreach (XmlNode node in RootNode.SelectNodes("service"))
{
if (node != null && node.Attributes["type"] != null && node.Attributes["type"].Value != "")
{
Type type=Type.GetType(node.Attributes["type"].Value);
IServerObj obj = new IServerObj(Activator.CreateInstance(type, null), node.Attributes["type"].Value, node.Attributes["type"].Value, node.Attributes["type"].Value);
objServerList.Add(obj);
}
}
return objServerList;
}
}
/// <summary>
/// windows服务接口的结构类
/// </summary>
public class IServerObj
{
private object service;
private string domain;
private string username;
private string userpwd;
public IServerObj(object _service, string _domain,string _username, string _userpwd)
{
service = _service;
domain = _domain;
username = _username;
userpwd = _userpwd;
}
/// <summary>
/// 服务对象
/// </summary>
public object Service
{
get {return service; }
}
/// <summary>
/// 服务域
/// </summary>
public string Domain
{
get { return domain; }
}
/// <summary>
/// 安全帐号
/// </summary>
public string Username
{
get { return username; }
}
/// <summary>
/// 安全密码
/// </summary>
public string Userpwd
{
get { return userpwd; }
}
}
/// <summary>
/// 缓存服务
/// </summary>
public class CacheConfig
{
private XmlNode node;
private object cacheObj = null;
public CacheConfig(XmlNode xml)
{
node = xml;
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <param name="CacheName">缓存名称</param>
/// <returns></returns>
public object getCache(string CacheName)
{
if (node != null && node.HasChildNodes)
{
XmlNode nodech = node.SelectSingleNode("//cache[@name='" + CacheName + "']");
string typestr = nodech.Attributes["type"].Value;
Type type = Type.GetType(typestr);
cacheObj = Activator.CreateInstance(type, null);
}
return cacheObj;
}
}
}
通过这样的配置后我们需要的额外信息就可以直接通过这个类找配置文件要了.