在开发ORM之前,先简单的介绍下ORM的基本概念。

对象关系映射(Object Relational Mapping,简称ORM)是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术。 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中。本质上就是将数据从一种形式转换到另外 一种形式。

产生渊源
你在DAL中写了很多的方法来读取对象数据,改变状态对象等等任务。而这些代码写起来总是重复的。看看DAL代码,你肯定会看到很多近似的通用的模式。人们就想了,能不能自动化实现DAL呢,其实大部分是可以的,所以ORM的各种工具诞生了,他们会自动为你生成dal的代码。

目前.net平台下比较流行的 ORM 产品有NHibernate,Entity Framework等。
相对于NHibernate这些重量级的ORM产品,对数据库的一张表做关系映射,就必须为其建立一张hbm.xml的映射文件,显得有些繁琐。c#支持自定义特性,对于数据库字段的一些属性关系映射,我们可以通过在Model类(数据库结构关系映射类)的属性上标注自定义特性来抽象描述数据库相关字段。

目标效果预览

数据库表结构:

 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>tableName
/// </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)]
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)]
public Guid? MyGuid
{
set { _MyGuid = value; }
get { return _MyGuid; }
} [DBType(SqlDBType = SqlDbType.SmallInt)]
public Int16? MySmallint
{
set { _MySmallint = value; }
get { return _MySmallint; }
} [DBType(SqlDBType = SqlDbType.Bit)]
public bool? MyBool
{
set { _MyBool = value; }
get { return _MyBool; }
}
[DBType(SqlDBType = SqlDbType.NText)]
public string Myntext
{
set { _Myntext = value; }
get { return _Myntext; }
}
}
}

Model层

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using Model;
using DAL.ErpSqlDAL.SqlFactory;
using DAL.ErpSqlDAL.SqlFactory.Common;
namespace DAL.Data
{
public class EmpInfoDAL : BaseDAL<EmpInfoModel>
{
public EmpInfoDAL(DbCmd dbCmd)
: base(dbCmd, EmpInfoModel.TableName)
{ } public IList<EmpInfoModel> getTopOneList()
{
return base.GetEntityBySql("select * from EmpInfo where id=1", null);
}
}
}

DAL层

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
namespace DAL.ErpSqlDAL.SqlFactory
{
public class BaseDAL<T> : OperateEntity<T>
where T : BaseEntity
{
private string entityName; public BaseDAL(DbCmd dbCmd, string entityName)
: base(dbCmd, entityName, typeof(T))
{
this.dbCmd = dbCmd;
this.entityName = entityName;
} /// <summary>得到一个实体的内容
/// </summary>
/// <param name="conditionList">条件实体</param>
/// <returns></returns>
public virtual T GetOneEntity(T conditionList)
{
try
{
IList<T> entityList = base.GetEntity(conditionList);
if (entityList.Count > )
return entityList[];
return null;
}
catch
{
throw;
}
}
/// <summary>添加实体
/// </summary>
/// <param name="entity">要添加的实体</param>
/// <returns></returns>
public virtual object AddEntity(T entity)
{
try
{
IList<T> entityList = new List<T>();
entityList.Add(entity); return base.AddEntity(entityList);
}
catch
{
throw;
} } /// <summary>根据主键修改实体
/// </summary>
/// <param name="entity">要修改的内容和包含主键值的实体</param>
/// <returns></returns>
public virtual int ModEntity(T entity)
{
try
{
IList<T> entityList = new List<T>();
entityList.Add(entity); return base.ModEntity(entityList);
}
catch
{
throw;
}
}
/// <summary>修改实体
/// </summary>
/// <param name="sourceEntity">要修改的内容</param>
/// <param name="conditionEntity">修改的条件</param>
/// <returns></returns>
public virtual int ModEntity(T sourceEntity, T conditionEntity)
{
try
{
IList<T> SourceEntityList = new List<T>();
SourceEntityList.Add(sourceEntity); IList<T> conditionEntityList = new List<T>();
conditionEntityList.Add(conditionEntity); return base.ModEntity(SourceEntityList, conditionEntityList);
}
catch
{
throw;
}
} /// <summary>删除实体
/// </summary>
/// <param name="entity">删除的条件</param>
/// <returns></returns>
public virtual int DelEntity(T entity)
{
try
{
IList<T> SourceEntityList = new List<T>();
SourceEntityList.Add(entity); return base.DelEntity(SourceEntityList);
}
catch
{
throw;
}
}
}
}

DAL层基类BaseDAL

使用:

   public partial class Test : System.Web.UI.Page
{
public string connetionString = "server=.;initial catalog=test;UID=sa;PWD=sa123";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData(); }
} protected void BindData()
{
DbCmd dbCmd = new DbCmdFactory(connetionString).CreateDbCmd();
IList<EmpInfoModel> list = new EmpInfoDAL(dbCmd).GetEntity(null);
GridView1.DataSource = list;
GridView1.DataBind();
} protected void Button1_Click(object sender, EventArgs e)
{
DbCmd dbCmd = new DbCmdFactory(connetionString).CreateDbCmd(); EmpInfoModel model = new EmpInfoModel();
model.Id = (GridView1.Rows.Count + ).ToString();
model.Name = "name" + model.Id+@"@!#$%^&*()_+-=<>:""{}|\';/.,";
model.isAllMoneyCheck = ;
model.MyGuid = Guid.NewGuid();
model.MySmallint = ;
model.MyBool = true;
model.Myntext = "Myntext " + model.Id;
new EmpInfoDAL(dbCmd).AddEntity(model);
BindData();
} protected void Button2_Click(object sender, EventArgs e)
{
DbCmd dbCmd = new DbCmdFactory(connetionString).CreateDbCmd();
EmpInfoModel model = new EmpInfoModel();
model.Id = ("").ToString(); model.isAllMoneyCheck = ;
model.MyGuid = Guid.NewGuid();
model.MySmallint = ;
model.MyBool = true;
new EmpInfoDAL(dbCmd).DelEntity(model);
BindData();
} protected void Button3_Click(object sender, EventArgs e)
{
DbCmd dbCmd = new DbCmdFactory(connetionString).CreateDbCmd();
IList<EmpInfoModel> list = new EmpInfoDAL(dbCmd).getTopOneList();
GridView1.DataSource = list;
GridView1.DataBind();
}
}

test.aspx.cs

通过对Model类属性做自定义特性来映射,数据库字段和程序类型的对应关系,是否为主键,是否外键,是否为虚拟字段等。

在下一篇中将开始研究如何一步一步的构建一个ORM框架。

自己开发轻量级ORM(二)

自己开发轻量级ORM(一)的更多相关文章

  1. 自己开发轻量级ORM(二)

    上一篇简单的对轻量级ORM开发开了个头.这篇主要聊下ORM框架的设计思路. ORM本质上是对数据库操作的抽象.大体上我将其分为对数据结构的抽象和对执行方法的抽象. 我的ORM设计图: ORM框架需要完 ...

  2. 自己开发轻量级ORM(三)

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

  3. 轻量级ORM框架 QX_Frame.Bantina(一、框架简介)

    轻量级ORM框架QX_Frame.Bantina系列讲解(开源) 一.框架简介 http://www.cnblogs.com/qixiaoyizhan/p/7417467.html 二.框架使用方式介 ...

  4. .NET轻量级ORM框架Dapper入门精通

    一.课程介绍 本次分享课程包含两个部分<.NET轻量级ORM框架Dapper修炼手册>和<.NET轻量级ORM框架Dapper葵花宝典>,阿笨将带领大家一起领略轻量级ORM框架 ...

  5. 轻量级ORM——PetaPoco

    近乎产品的数据访问是基于轻量级ORM——PetaPoco,哪怕是最新的5.2版本也是基于PetaPoco. 产品源码下载地址:http://www.jinhusns.com/Products/Down ...

  6. 轻量级ORM工具Simple.Data

    今天推举的这篇文章,本意不是要推举文章的内容,而是据此介绍一下Simple.Data这个很有意思的类ORM工具. 现在大家在.NET开发中如果需要进行数据访问,那么基本都会使用一些ORM工具,比如微软 ...

  7. .NET轻量级ORM组件Dapper葵花宝典

    一.摘要 为什么取名叫<葵花宝典>? 从行走江湖的世界角度来讲您可以理解为一本"武功秘籍",站在我们IT编程的世界角度应该叫"开发宝典". 如果您在 ...

  8. 连表查询都用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%我不知道在 ...

  9. 分享自己写的基于Dapper的轻量级ORM框架~

    1.说明 本项目是一个使用.NET Standard 2.0开发的,基于 Dapper 的轻量级 ORM 框架,包含基本的CRUD以及根据表达式进行一些操作的方法,目前只针对单表,不包含多表连接操作. ...

随机推荐

  1. MulticastSocket绑定端口的问题

    该文章由 Binkery 发布于 Binkery技术博客 http://www.binkery.com 如转载请注明出处,该文章的链接地址为 http://www.binkery.com/archiv ...

  2. openstack controller ha测试环境搭建记录(十)——配置neutron(控制节点)

    创建neutron用户:mysql -u root -p CREATE DATABASE neutron;GRANT ALL PRIVILEGES ON neutron.* TO 'neutron'@ ...

  3. Linux 分区和目录

    [1. 分区与目录概念理解]  Linux的分区是物理上的概念,就像我们把一块硬盘分成C:,D:,E:三个区一样,物理上将存储空间分开 Linux的目录是逻辑上的概念,Linux的目录树实际上是一个分 ...

  4. 502 Bad Gateway(Nginx) 查看nginx日志有如下内容

    2016/09/01 09:49:41 [error] 79464#79464: *3 user "nagios" was not found in "/usr/loca ...

  5. 2017-01-11小程序form表单提交

    小程序form表单提交 1.小程序相对于之前的WEB+PHP建站来说,个人理解为只是将web放到了微信端,用小程序固定的格式前前端进行布局.事件触发和数据的输送和读取,服务器端可以用任何后端语言写,但 ...

  6. IOS开发-ObjC-对象、封装

    C语言是基于过程的一种编程语言,而OC语言是基于对象的一种语言. C是和其他的面向对象的语言的区别在于C语言更注重地层操作,思维方式相比面向对象的语言而言更接近机器的思维方式,而面向对象的语言更接近于 ...

  7. oracle系列--基础理论

    一.数据库系统架构: 外层(External Level)外层是提供给用户直接操作使用的 概念层(Conceptual Level)用来描述数据库中存放数据的类型.表之间的关系.高级的数据模型.用户的 ...

  8. delphi的ArrayList

    本文转载自Top.hand<delphi的ArrayList>   delphi可以用Classes.TList类来实现ArrayList功能.注意:add()方法存入的类型是TPoint ...

  9. Selenium2(java)页面对象模型(Page Object) 八

    在开发一个 Selenium WebDriver 测试,我们可以使用页面对象模型.这个模型可以使测 试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来.对象模型也提供了一个注释,帮助缓存远程,避 ...

  10. Bootstrap3网上api文档地址

    http://v3.bootcss.com/css/#forms http://www.ziqiangxuetang.com/bootstrap/bootstrap-forms.html 另附加fa字 ...