zoukankan      html  css  js  c++  java
  • 点点滴滴的成长[2011114]:自定义config文件

          最近对于web开发的信息管理的系统的架构兴趣浓厚,对于应用于自定义平台的开发非常感兴趣,最近也自不量力想开发出自己的一套应用平台。避免繁琐而又容易出错的编码工作。平台刚刚起步,具体功能为实现。今天先以自定义config文件为例,讲解描述一下自定义config文件的读取工作。

         Platform.config 为基于标准的xml文件的自定义config文件,用于存储平台的各个模块的配置信息。首先包含的就是 数据库的切换模块 config文件信息如下:

    View Code
    <?xml version="1.0" encoding="utf-8" ?>
    <Platform>
    <DataBase>
    <!--使用的数据库-->
    <DBApp Name="SqlServer"></DBApp>
    <DBList>

    <add DBName="Access" DBProvider="" DBConnectionString=""></add>

    <add DBName="SqlServer" DBProvider="System.Data.SqlClient" DBConnectionString=""></add>

    <add DBName="MySql" DBProvider="" DBConnectionString=""></add>

    <add DBName="Oracle" DBProvider="" DBConnectionString=""></add>

    <add DBName="DB" DBProvider="" DBConnectionString=""></add>
    </DBList>
    </DataBase>
    <Cache>

    </Cache>
    </Platform>

    读取Platform类如下:

    1. 实体类 返回 DBHelper需要的两个参数,Provider 和 ConnectionString

    View Code
    namespace Donet.Configuration
    {
    public class DBItem
    {
    private string _DBProvider;
    ///<summary>
    /// 数据库,提供商
    ///</summary>
    public string DBProvider
    {
    get { return _DBProvider; }
    set { _DBProvider = value; }
    }
    private string _DBConnectionString;
    ///<summary>
    /// 连接字符串
    ///</summary>
    public string DBConnectionString
    {
    get { return _DBConnectionString; }
    set { _DBConnectionString = value; }
    }
    }
    }

    2. 文件读取类

    View Code
    namespace Donet.Configuration
    {
    public static class PlatformConfig
    {

    public static DBItem GetDBItem(string filePath)
    {
    DBItem dbItem = new DBItem();
    // new一个XMLTextReader实例
    XmlTextReader reader = new XmlTextReader(filePath);
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);
    //关闭reader,不然config文件就变成只读的了
    reader.Close();
    XmlNodeList nodeList = doc.SelectSingleNode("/Platform").ChildNodes;
    string dbName = nodeList[0]["DBApp"].Attributes["Name"].Value;
    XmlNodeList addList = nodeList[0]["DBList"].ChildNodes;
    foreach (XmlNode node in addList)
    {
    if (node.Attributes["DBName"].Value == dbName)
    {
    dbItem.DBProvider = node.Attributes["DBProvider"].Value;
    dbItem.DBConnectionString = node.Attributes["DBConnectionString"].Value;
    }
    }
    return dbItem;

    }
    }
    }

    3.0 调用 方式

    View Code
    Donet.Configuration.PlatformConfig.GetDBItem(Server.MapPath("Platform.config")).DBProvider;

    实例图:

    晕了不会插图图片。

    好了就这些吧,希望有平台开发的前辈指点一下,平台开发。不是那个平台怎么用,而是怎样开发出一个成熟的平台,来应对日新月异的需求。

    这个config的管理类我还会继续更新,以达到高质量的代码。欢迎各位前辈指正。谢绝喷子。V




     

  • 相关阅读:
    数据库-第十章 数据库恢复技术-10.5 恢复策略
    数据库-第十章 数据库恢复技术-10.4 恢复的实现技术
    数据库-第十章 数据库恢复技术-10.3 故障的种类
    数据库-第十章 数据库恢复技术-10.2 数据库恢复概述
    数据库-第十章 数据库恢复技术-10.1 事务的基本概念
    数据库-第九章 关系查询处理和查询优化-9.4 物理优化
    数据库-第九章 关系查询处理和查询优化-9.3 代数优化
    数据库-第九章 关系查询处理和查询优化-9.2 关系数据库系统的查询优化
    数据库-第九章 关系查询处理和查询优化-9.1 关系数据库系统的查询处理
    编译原理-第五章 语法制导翻译-5.2 语法制导翻译的应用
  • 原文地址:https://www.cnblogs.com/BinaryBoy/p/2235881.html
Copyright © 2011-2022 走看看