|
|
在面向服务中讲配置文件,肯定是要把它与具体领域分离,即它有普遍的一般性。在程序开发过程中,难免会用到一些易变性,全局的常量信息,我们通常的作法是把它们放在Web.config或者自定义的文件中,当然你的配置文件可以是XML,二进制的等等,但一般时候我们选择用XML标准的文件。
看全局配置项目的结构如下:

下面我来介绍一下每个文件的使用:
ConfigFactory它是一个配置文件的工厂类,作用当然就是“从配置文件中生产对象”了,呵呵。(这讲不考虑性能问题)
1 /// <summary> 2 /// 配置信息生产工厂 3 /// </summary> 4 public class ConfigFactory 5 { 6 #region 私有 7 8 /// <summary> 9 /// 配置文件管理类10 /// </summary>11 static ConfigFilesManager cfm;12 13 #endregion14 15 #region 公开的属性16 public T GetConfig<T>() where T : IConfiger17 {18 string configFilePath = string.Empty;19 string filename = typeof(T).Name;20 21 HttpContext context = HttpContext.Current;22 string siteVirtrualPath = string.IsNullOrEmpty(ConfigurationManager.AppSettings["SiteVirtrualPath"]) ?23 "/" : ConfigurationManager.AppSettings["SiteVirtrualPath"];24 if (context != null)25 {26 configFilePath = context.Server.MapPath(string.Format("{0}/Configs/{1}.Config", siteVirtrualPath, filename));27 }28 else29 {30 configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Configs\{0}.Config", filename);31 }32 33 if (!File.Exists(configFilePath))34 {35 throw new Exception("发生错误: 网站" +36 new FileInfo("fileName").DirectoryName37 + "目录下没有正确的.Config文件");38 }39 40 cfm = new ConfigFilesManager(configFilePath, typeof(T));41 return (T)cfm.LoadConfig();42 }43 #endregion44 45 } |
|