zoukankan      html  css  js  c++  java
  • Web系统中配置文件管理模块的实例

    配置文件,对于一个系统来说,越来越显得重要,本人也根据正在开发的系统的配置文件管理进行了一个小总结。

    1、首先要确定要配置文件的用途和结构。根据用途来确定结构,根据结构才能反序列化成内存里的对象。

    我设计了一个SystemConfig.config文件,该文件主要是用来配置整个系统的一些内容,比如名称、版本、一些业务上的公共的东西等等。

    View Code
    1 <?xml version="1.0" encoding="utf-8" ?>
    2  <SystemConfigInfo>
    3 <SystemTitle>涛涛ideas的配置系统</SystemTitle>
    4 <SystemVersion>20110705</SystemVersion>
    5  </SystemConfigInfo>

    2、配置文件的结构已经确定了下来,那接下来需要确定的是内存对象的结构了。

    定义SystemConfigInfo的实体类,并将每个元素对应配置文件中的节点。

    View Code
    1 using System;
    2 using System.Data;
    3 using System.Configuration;
    4 using System.Linq;
    5 using System.Web;
    6 using System.Web.Security;
    7 using System.Web.UI;
    8 using System.Web.UI.HtmlControls;
    9 using System.Web.UI.WebControls;
    10 using System.Web.UI.WebControls.WebParts;
    11 using System.Xml.Linq;
    12 using System.Xml.Serialization;
    13
    14 namespace Taotao.Test.Config
    15 {
    16 /// <summary>
    17 /// 系统配置信息
    18 /// </summary>
    19 [Serializable]
    20 public class SystemConfigInfo
    21 {
    22 /// <summary>
    23 /// 系统名称
    24 /// </summary>
    25 [XmlElement("SystemTitle")]
    26 public string SystemTitle;
    27
    28 /// <summary>
    29 /// 系统版本
    30 /// </summary>
    31 [XmlElement("SystemVersion")]
    32 public string SystemVersion;
    33 }
    34 }

    在类定义和字段定义上都使用了特性,这里做个解释:Serializable表示该类可以被序列化和反序列化,XmlElement表示对应xml文件的element节点,举一反三,XmlAttribute对应xml文件节点的属性,还有很多其他的xml...特性,读者可以自己尝试。这些特性在xml文件被序列化和反序列化的时候起到很重要的作用,因为这些特性告诉我们哪些是节点哪些是节点属性。

    3、既然两种结构都已经确定了下来,下一步就应该是转换的问题了。

    对于xml和对象之间的转换,网上有很多详细的解析,我这里就不详细解析接下来需要使用到的关于xml的类。

    View Code
    1 using System;
    2 using System.Data;
    3 using System.Configuration;
    4 using System.Linq;
    5 using System.Web;
    6 using System.Web.Security;
    7 using System.Web.UI;
    8 using System.Web.UI.HtmlControls;
    9 using System.Web.UI.WebControls;
    10 using System.Web.UI.WebControls.WebParts;
    11 using System.Xml.Linq;
    12 using System.IO;
    13 using System.Xml.Serialization;
    14
    15 namespace Taotao.Test.Config
    16 {
    17 /// <summary>
    18 /// 系统配置
    19 /// </summary>
    20 public class SystemConfig
    21 {
    22 /// <summary>
    23 /// 文件路径
    24 /// </summary>
    25 private static string filePath;
    26 /// <summary>
    27 /// 配置对象
    28 /// </summary>
    29 private static SystemConfigInfo systemConfigInfo;
    30 /// <summary>
    31 /// 锁对象
    32 /// </summary>
    33 private static readonly object lockObj = new object();
    34 /// <summary>
    35 /// 文件最后修改时间
    36 /// </summary>
    37 private static DateTime fileLastChangeTime;
    38
    39 static SystemConfig()
    40 {
    41 filePath = HttpContext.Current.Server.MapPath("~/Config/SystemConfig.config");
    42 fileLastChangeTime = File.GetLastWriteTime(filePath);
    43 systemConfigInfo = GetConfig();
    44 }
    45
    46 /// <summary>
    47 /// 获取配置对象实例
    48 /// </summary>
    49 /// <returns></returns>
    50 public static SystemConfigInfo GetConfig()
    51 {
    52 DateTime newFileLastChangeTime = File.GetLastWriteTime(filePath);
    53 //配置文件被修改
    54 if (newFileLastChangeTime != fileLastChangeTime)
    55 {
    56 systemConfigInfo = (SystemConfigInfo)DeSerialize(typeof(SystemConfigInfo), filePath);
    57 }
    58 else
    59 {
    60 if (systemConfigInfo == null)
    61 {
    62 lock (lockObj)
    63 {
    64 if (systemConfigInfo == null)
    65 {
    66 systemConfigInfo = (SystemConfigInfo)DeSerialize(typeof(SystemConfigInfo), filePath);
    67 }
    68 }
    69 }
    70 }
    71
    72 return systemConfigInfo;
    73 }
    74
    75 /// <summary>
    76 /// 将配置文件内容发序列化成对象
    77 /// </summary>
    78 /// <param name="type"></param>
    79 /// <param name="filePath"></param>
    80 /// <returns></returns>
    81 private static object DeSerialize(Type type, string filePath)
    82 {
    83 try
    84 {
    85 using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    86 {
    87 XmlSerializer xs = new XmlSerializer(type);
    88 return xs.Deserialize(fs);
    89 }
    90 }
    91 catch (Exception ex)
    92 {
    93 throw ex;
    94 }
    95 }
    96 }
    97 }

    代码中需要锁对象,是因为内存中只需要维护一个该配置对象的实例;添加文件修改时间的判断,是因为在系统运行的过程中,我们会时不时地修改配置,这样配置文件被修改后,可以立即显示效果。

    4、最后,来看看怎么使用该配置对象吧。

    View Code
    1 protected void Page_Load(object sender, EventArgs e)
    2 {
    3 this.txtSystemTitle.Text = SystemConfig.GetConfig().SystemTitle;
    4 this.txtSystemVersion.Text = SystemConfig.GetConfig().SystemVersion;
    5 }

    使用还是比较简单的,看看效果图:

  • 相关阅读:
    CSS 选择器
    HTML lable和fieldset
    html image和表格
    HTML a标签
    html 提交后台的标签
    HTML INPUT系列使用
    HTML内标签、换行
    HTML 头部详解
    单例模式
    const 指针的三种使用方式
  • 原文地址:https://www.cnblogs.com/jiangzhichao/p/2098744.html
Copyright © 2011-2022 走看看