天天看點

筆記之Utility.DataAccess

擠出時間看了一些代碼,做一些筆記,備忘!!!

現在ORM随處可見,為什麼不要已有的ORM而要手動寫SQL呢?這肯定是有因為滴,存在必合理嘛!

自認為關于性能、維護、Maybe還有其他的,歡迎大家拍磚!

當然SQL是不會寫在代碼中,而存在于配置檔案(想起了iBatis),便于統一管理和維護,不會污染代碼。

以下就是關于此内容:

思路很簡單,大家應該都明白,說白了就是從XML檔案(以XML形式存在,當然其他任何檔案形式都可)

裡讀取SQL到記憶體,然後組織DbCommand進行DB操作。但是還是有很多細節需要處理。

必竟理論與實踐還是有差別的。

 SQL語句在XML檔案裡,肯定會有對XML配置檔案的相關操作(這裡主要是讀取的監視)和配置檔案的格式定義

配置檔案包含:1.Database.config(資料庫連接配接字元串的配置檔案)

       2.DbCommandFiles.config(SQL語句檔案的集合(有哪些SQL配置檔案需要注冊到此檔案))

       3.****.config(具體SQL語句配置檔案)

Database.config格式:

<?xml version="1.0"?>
<databaseList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://***.com/DatabaseList">
  <database name="**Service">
 <connectionString>Hn3t+SQCaz3ZRQDdawhd6njUqoNX1BXcfMvvUaOvBtRi9O/9fPPZEuPSYvzs</connectionString>
  </database>

  <database name="**Service">      
<connectionString>Hn3t+SQCaz3ZRQDdawh</connectionString>
  </database>
  <database name="MailService">
    
</databaseList>      

Database.config 對應實體

[XmlRoot("databaseList", Namespace = "http://***.com/DatabaseList")]
    public class DatabaseList
    {
        [XmlElement("database")]
        public DatabaseInstance[] DatabaseInstances
        {
            get;
            set;
        }
    }

    [XmlRoot("database")]
    public class DatabaseInstance
    {
        [XmlAttribute("name")]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute("type")]
        public string Type
        {
            get;
            set;
        }

        [XmlElement("connectionString")]
        public string ConnectionString
        {
            get;
            set;
        }
    }      

***.config

筆記之Utility.DataAccess
筆記之Utility.DataAccess
<?xml version="1.0" encoding="utf-8" ?>
<dataOperations xmlns="http://***.com/DataOperation">
  <dataCommand name="GetAreaList" database="NCService" commandType="Text">
    <commandText>
      <![CDATA[
SELECT * FROM DDD.DBO.DDD WITH(NOLOCK) WHERE SYSNO = @SysNo
       ]]>
    </commandText>
<parameters>
      <param name="@SysNo" dbType="Int32"/>
    </parameters>
  </dataCommand>
</dataOperations>      

View Code

筆記之Utility.DataAccess
筆記之Utility.DataAccess
[XmlRoot("dataOperations", Namespace = "http://****.com/DataOperation")]
    public partial class DataOperations
    {
        [XmlElement("dataCommand")]
        public DataCommandConfig[] DataCommand
        {
            get;
            set;
        }
    }

    [XmlRoot("dataCommand")]
    public partial class DataCommandConfig
    {
        private CommandType m_CommandType = CommandType.Text;
        private int m_TimeOut = 300;

        [XmlElement("commandText")]
        public string CommandText
        {
            get;
            set;
        }

        [XmlElement("parameters")]
        public Parameters Parameters
        {
            get;
            set;
        }

        [XmlAttribute("name")]
        public string Name
        {
            get;
            set;
        }

        [XmlAttributeAttribute("database")]
        public string Database
        {
            get;
            set;
        }

        [XmlAttributeAttribute("commandType")]
        [DefaultValueAttribute(CommandType.Text)]
        public CommandType CommandType
        {
            get
            {
                return this.m_CommandType;
            }
            set
            {
                this.m_CommandType = value;
            }
        }

        [XmlAttributeAttribute("timeOut")]
        [DefaultValueAttribute(300)]
        public int TimeOut
        {
            get
            {
                return this.m_TimeOut;
            }
            set
            {
                this.m_TimeOut = value;
            }
        }

        public DataCommandConfig Clone()
        {
            DataCommandConfig config = new DataCommandConfig();
            config.CommandText = this.CommandText;
            config.CommandType = this.CommandType;
            config.Database = this.Database;
            config.Name = this.Name;
            config.Parameters = this.Parameters == null ? null : this.Parameters.Clone();
            config.TimeOut = this.TimeOut;
            return config;
        }
    }

    [XmlRoot("parameters")]
    public partial class Parameters
    {
        [XmlElement("param")]
        public Param[] Param
        {
            get;
            set;
        }

        public Parameters Clone()
        {
            Parameters p = new Parameters();
            if (this.Param != null)
            {
                p.Param = new Param[this.Param.Length];
                for (int i = 0; i < this.Param.Length; i++)
                {
                    p.Param[i] = this.Param[i].Clone();
                }
            }
            return p;
        }
    }

    [XmlRoot("param")]
    public partial class Param
    {
        private ParameterDirection m_Direction = ParameterDirection.Input;
        private int m_Size = -1;
        private byte m_Scale = 0;
        private byte m_Precision = 0;

        public Param Clone()
        {
            Param p = new Param();
            p.DbType = this.DbType;
            p.Direction = this.Direction;
            p.Name = this.Name;
            p.Precision = this.Precision;
            p.Property = this.Property;
            p.Scale = this.Scale;
            p.Size = this.Size;
            return p;
        }

        [XmlAttribute("name")]
        public string Name
        {
            get;
            set;
        }

        [XmlAttribute("dbType")]
        public DbType DbType
        {
            get;
            set;
        }

        [XmlAttribute("direction")]
        [DefaultValue(ParameterDirection.Input)]
        public ParameterDirection Direction
        {
            get
            {
                return this.m_Direction;
            }
            set
            {
                this.m_Direction = value;
            }
        }

        [XmlAttribute("size")]
        [DefaultValue(-1)]
        public int Size
        {
            get
            {
                return this.m_Size;
            }
            set
            {
                this.m_Size = value;
            }
        }

        [XmlAttribute("scale")]
        [DefaultValue(0)]
        public byte Scale
        {
            get
            {
                return this.m_Scale;
            }
            set
            {
                this.m_Scale = value;
            }
        }

        [XmlAttribute("precision")]
        [DefaultValue(0)]
        public byte Precision
        {
            get
            {
                return this.m_Precision;
            }
            set
            {
                this.m_Precision = value;
            }
        }

        [XmlAttribute("property")]
        public string Property
        {
            get;
            set;
        }
    }      

View Code

XML檔案與Class的對應關系,當然可以定義你喜歡的任何格式,隻要滿足相關規範。

目前我們會用一個ConfigManager來對配置檔案的管理。

10+網際網路電商公司技術開發管理工作,現已入坑項目管理,吐槽項目管理過程中的人與事。