自己开发轻量级ORM(三)
上一篇中简单分享了下ORM的设计思路。现在开始讲如何用代码来实现上篇的设计模型。
我们建2个类库来分别抽象数据库表结构关系映射和SQL增删改查操作。
打开VS2010,新建2个类库。分别起名为Model,和DAL。

Model层为数据库表结构关系映射
DAL层为 SQL增删改查操作的方法抽象封装
我们先从Model层开始。
数据库的表会包含表名,字段名称,字段类型,主键,外键等主要元素。我们在项目中为每张表建立一个Model类来抽象描述。
在Model类中我们定义常量TableName,用来描述数据库表名称。为表的字段逐一添加Model类属性,属性名和字段名相同。
由于SQL字段类型和.net数据类型不一致,我们在字段属性上添加自定义特性类DBType来描述对应的SQL字段类型。
字段会有主键,外键标识或者是虚拟字段标识。我们在字段属性上添加自定义特性类DBField来描述他们。
如:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using System.Data;
namespace Model
{
[Serializable]
public class EmpInfoModel : BaseEntity
{
/// <summary>是否可以修改
/// </summary>
public const bool isCanMod = false;
/// <summary>数据库表名
/// </summary>
public const String TableName = "EmpInfo";
public EmpInfoModel()
{ } private string _Id;
private string _Name;
private int? _isAllMoneyCheck;
private Guid? _MyGuid;
private Int16? _MySmallint;
private bool? _MyBool;
private string _Myntext;
[DBField(KeyType = DbKeyType.PK)]//主键标识
[DBType(SqlDBType = SqlDbType.NVarChar)]//字段对应SQLSERVER数据库字段类型
public virtual string Id
{
set { _Id = value; }
get { return _Id; }
} public string Name
{
set { _Name = value; }
get { return _Name; }
} public int? isAllMoneyCheck
{
set { _isAllMoneyCheck = value; }
get { return _isAllMoneyCheck; }
} [DBType(SqlDBType = SqlDbType.UniqueIdentifier)]//字段对应SQLSERVER数据库字段类型
public Guid? MyGuid
{
set { _MyGuid = value; }
get { return _MyGuid; }
} [DBType(SqlDBType = SqlDbType.SmallInt)]//字段对应SQLSERVER数据库字段类型
public Int16? MySmallint
{
set { _MySmallint = value; }
get { return _MySmallint; }
} [DBType(SqlDBType = SqlDbType.Bit)]//字段对应SQLSERVER数据库字段类型
public bool? MyBool
{
set { _MyBool = value; }
get { return _MyBool; }
}
[DBType(SqlDBType = SqlDbType.NText)]//字段对应SQLSERVER数据库字段类型
public string Myntext
{
set { _Myntext = value; }
get { return _Myntext; }
}
}
}
我在Model类库下添加DbKeyType类,TableJoinType类,DBFieldAttribute类,DBJoinAttribute类,DBTableAttribute类,DBTypeAttribute类。

DbKeyType类为字段键值枚举类,枚举值包含字段否为主键,外键,无,和虚拟字段。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Model.Entities
{
/// <summary>
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
public enum DbKeyType:int
{
Filed = ,
PK = ,
FK = ,
/// <summary>只用于承载数据
/// </summary>
DataFiled =
}
}
TableJoinType为表连接类型枚举类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Model.Entities
{
/// <summary>
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
public enum TableJoinType : int
{
INNER_JOIN = ,
LEFT_JOIN = ,
RIGHT_JOIN =
}
}
DBFieldAttribute类为字段键值特性标识类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Model.Entities
{
/// <summary>字段特性
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public sealed class DBFieldAttribute : Attribute
{
/// <summary>字段名称
/// </summary>
public string FieldName
{
get;
set;
}
/// <summary>键类型
/// </summary>
public DbKeyType KeyType
{
get;
set;
}
/// <summary>字段数据类型
/// </summary>
public Type PropertyType
{
get;
set;
}
/// <summary>构造函数
/// </summary>
public DBFieldAttribute()
{ }
}
}
DBJoinAttribute类为关系表连接类型特性标识类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Model.Entities
{
/// <summary>链接属性
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public sealed class DBJoinAttribute : Attribute
{
/// <summary>主键
/// </summary>
public string PK
{
get;
set;
}
/// <summary>外键
/// </summary>
public string FK
{
get;
set;
}
/// <summary>表连接关系
/// </summary>
public TableJoinType JoinType
{
get;
set;
}
/// <summary>构造函数
/// </summary>
public DBJoinAttribute()
{ }
}
}
DBTableAttribute类用于连接是标识相应表名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Model.Entities
{
/// <summary>所属表属性
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public sealed class DBTableAttribute : Attribute
{
/// <summary>表名称
/// </summary>
public string TableName
{
get;
set;
}
/// <summary>表昵称
/// </summary>
public string TableNickName
{
get;
set;
}
/// <summary>构造函数
/// </summary>
public DBTableAttribute()
{
}
}
}
DBTypeAttribute类为标识字段对应SQLSERVER字段类型标识类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Model.Entities
{
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public sealed class DBTypeAttribute : Attribute
{
/// <summary>SQL数据库字段类型
/// </summary>
public SqlDbType SqlDBType
{
get;
set;
}
/// <summary>构造函数
/// </summary>
public DBTypeAttribute()
{
}
}
}
通过添加上面的类,我们现在可以对数据库表进行抽象,用对应的特性标识字段是否为主键,外键或者为虚拟字段,SQL字段于.net类型的转换关系。各Model类之间的连接关系。
自己开发轻量级ORM(三)的更多相关文章
- 自己开发轻量级ORM(一)
在开发ORM之前,先简单的介绍下ORM的基本概念. 对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的 ...
- 自己开发轻量级ORM(二)
上一篇简单的对轻量级ORM开发开了个头.这篇主要聊下ORM框架的设计思路. ORM本质上是对数据库操作的抽象.大体上我将其分为对数据结构的抽象和对执行方法的抽象. 我的ORM设计图: ORM框架需要完 ...
- 【从零开始搭建自己的.NET Core Api框架】(三)集成轻量级ORM——SqlSugar:3.1 搭建环境
系列目录 一. 创建项目并集成swagger 1.1 创建 1.2 完善 二. 搭建项目整体架构 三. 集成轻量级ORM框架——SqlSugar 3.1 搭建环境 3.2 实战篇:利用SqlSuga ...
- 轻量级ORM框架 QX_Frame.Bantina(一、框架简介)
轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...
- [翻译]现代java开发指南 第三部分
现代java开发指南 第三部分 第三部分:Web开发 第一部分,第二部分,第三部分 =========================== 欢迎来到现代 Java 开发指南第三部分.在第一部分中,我们 ...
- .NET轻量级ORM框架Dapper入门精通
一.课程介绍 本次分享课程包含两个部分<.NET轻量级ORM框架Dapper修炼手册>和<.NET轻量级ORM框架Dapper葵花宝典>,阿笨将带领大家一起领略轻量级ORM框架 ...
- .NET轻量级ORM组件Dapper葵花宝典
一.摘要 为什么取名叫<葵花宝典>? 从行走江湖的世界角度来讲您可以理解为一本"武功秘籍",站在我们IT编程的世界角度应该叫"开发宝典". 如果您在 ...
- 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式
连表查询都用Left Join吧 最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表的数据操作
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射 下一篇:Farseer.net轻量级ORM开源框 ...
随机推荐
- JAVA中字符串函数subString的用法小结
本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int begi ...
- (中等) HDU 2295 , DLX+重复覆盖+二分。
Description N cities of the Java Kingdom need to be covered by radars for being in a state of war. S ...
- iOS 添加导航按钮
iOS设置导航按钮navigationBar中包含了这几个重要组成部分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title. ...
- 3DTouch简单了解
3D Touch的三大模块 代码Demo:https://github.com/haozheMa/3DTouch 在我们的app中使用3D Touch功能,主要分为以下三个模块: 1.Home Scr ...
- FZU 1064 教授的测试
递归构造答案. 根据当前整颗树的编号,可以计算左右子树有几个节点以及编号.因此,不断dfs下去就可以了. #include<cstdio> #include<cstring> ...
- iOS 发布流程
1.登陆苹果开发者中心http://developer.apple.com(99美元账号) 2.进入itunes connect 3.选择Manage Your Apps 4.选择Add New Ap ...
- linux 更换yum源
1.进入存放源配置的文件夹 cd /etc/yum.repos.d 2.备份默认源 mv ./CentOS-Base.repo ./CentOS-Base.repo.bak 3.使用wget下载163 ...
- nodejs抓取数据二(列表解析)
这里做得比较暴力,没有分页取出数据解析,O(∩_∩)O哈哈~,居然没有被挂机.不过解析的坑特别多...不过大部分我想要的数据都拿到了. //解析列表数据 var http = require(&quo ...
- Run Loop简介 分类: ios技术 ios相关 2015-03-11 22:21 73人阅读 评论(0) 收藏
做了一年多的IOS开发,对IOS和Objective-C深层次的了解还十分有限,大多还停留在会用API的级别,这是件挺可悲的事情.想学好一门语言还是需要深层次的了解它,这样才能在使用的时候得心应手,出 ...
- UVa 594 - One Little, Two Little, Three Little Endians
题目大意:大小端模式的转换.所谓的小端模式,是指数据的高位保存在内存的高地址中,而数据的低位保存在内存的低地址中.与此相对,所谓的大端模式,是指数据的高位,保存在内存的低地址中,而数据的低位,保存在内 ...