笔记之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来对配置文件的管理。
笔记之Utility.DataAccess的更多相关文章
- CodeGenerator.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CiCe ...
- This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed(在64位模式下运行安装了32位的Oracle客户端组件时,会发生此问题)
部署win服务时出现下面的问题: 在事件查看器中看到如下错误: 日志名称: Application来源: ***调度服务日期: 2014/5/21 12:53:21事件 ID: 0任务类别: 无级别: ...
- amazeui学习笔记--css(布局相关3)--辅助类Utility
amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- 《C#本质论》读书笔记(18)多线程处理
.NET Framework 4.0 看(本质论第3版) .NET Framework 4.5 看(本质论第4版) .NET 4.0为多线程引入了两组新API:TPL(Task Parallel Li ...
- UE4 中Struct Emum 类型的定义方式 笔记
UE4 基础,但是不经常用总是忘记,做个笔记加深记忆: 图方便就随便贴一个项目中的STRUCT和 Enum 的.h 文件 Note:虽然USTRUCT可以定义函数,但是不能加UFUNCTION 标签喔 ...
- 机器学习&数据挖掘笔记_22(PGM练习六:制定决策)
前言: 本次实验是将一些简单的决策理论和PGM推理结合,实验内容相对前面的图模型推理要简单些.决策理论采用的是influence diagrams,和常见图模型本质一样, 其中的决策节点也可以用CPD ...
- django笔记-模型数据模板呈现过程记录(多对多关系)
首先,推荐一个网址:http://www.tuicool.com/articles/BfqYz2F,因为这里的比我的要有条理,更有利于各位的理解. 以下仅为为个人一次不完整的笔记: 环境:ubuntu ...
- Linq_Lambda GroupBy使用笔记
今天看MVC遇到了GroupBY的Lambda表达式..有兴趣详细的看下去..得此笔记..记录之... 不罗嗦..上代码... //得到List<GroupEmail>对象 数据源 var ...
随机推荐
- Robotframework自动化6-基础关键字介绍3
这一章节介绍一下断言时用到的关键字,断言是写测试用例的必备,没有断言的测试用例是没有灵魂的. 一:Should Be Equal Should Be Equal 是用来判断实践结果和预期结果是否一致 ...
- Swift入门
Swift 入门 简介 Swift 语言由苹果公司在 2014 年推出,用来撰写 OS X 和 iOS 应用程序 2014 年,在 Apple WWDC 发布 历史 2010 年 7 月,苹果开发者工 ...
- [LeetCode]64. 最小路径和(DP)
题目 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4 ...
- [SpringBoot项目]问题及解决总结
问题:MySQL 8.0版本连接报错:Could not create connection to database server 原因 MySQL8.0版本需要更换驱动为"com.mysq ...
- Noip2017 Day2 T1 奶酪
题目描述 现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪中间有许多半径相同的球形空洞.我们可以在这块奶酪中建立空间坐标系,在坐标系中,奶酪的下表面为z =0,奶酪的上表面为 ...
- ECMAScript6入门学习--第一天
ECMAScript与javascript的关系 ECMA是是一个国际标准化的一个组织,规定了浏览器脚本的语言标准,在上个实际,javascript公司Netscape把javascript托付给EC ...
- 关于button和submit的form提交以及 页面跳转问题
最近在做官网的注册登录form提交时遇到了这个问题,1.0时因为使用普通的模板并没有出现页面跳转失败问题 由于2.0时更换了注册模板,此时按钮样式是以下样式 而在css样式的模板里使用的是button ...
- jdk在linux下安装、配置环境变量
1.jdk下载: 下载地址:https://www.oracle.com/java/technologies/javase-downloads.html 2. 3. 4.解压jdk到/usr/loca ...
- Dledger的是如何实现主从自动切换的
前言 hello小伙伴们,今天王子又来继续和大家聊RocketMQ了,之前的文章我们一直说Broker的主从切换是可以基于Dledger实现自动切换的,那么小伙伴们是不是很好奇它究竟是如何实现的呢?今 ...
- Python-in is == 区别
in 判断单个元素是否在序列中, 对字典来说只能判断key,在不在关系 print("ab" in "abcdefg") print("abc&quo ...