笔记之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
<?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>
[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; } }
XML文件与Class的对应关系,当然可以定义你喜欢的任何格式,只要满足相关规范。
当前我们会用一个ConfigManager来对配置文件的管理。
10+互联网电商公司技术开发管理工作,现已入坑项目管理,吐槽项目管理过程中的人与事。

浙公网安备 33010602011771号