在开发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框架。