上一篇简单的对轻量级ORM开发开了个头。这篇主要聊下ORM框架的设计思路。
ORM本质上是对数据库操作的抽象。大体上我将其分为对数据结构的抽象和对执行方法的抽象。
我的ORM设计图:
ORM框架需要完成.net数据和数据库中数据的相互转换,以及对SQL语句中经典的增删改查操作的抽象封装。
封装方法代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Model.Entities; namespace DAL.ErpSqlDAL.SqlFactory
{
/// <summary>CRUD方法抽象
/// 创建人:雷旭鹏(leo) 2014-1-13
/// 联系方式:leixupeng823@163.com
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class OperateEntity<T>:ResolveEntity<T> where T :BaseEntity
{
public OperateEntity(DbCmd dbCmd, string entityName, Type entityType)
: base(dbCmd, entityName, entityType)
{
} /// <summary>添加实体
/// </summary>
/// <param name="entityList">要添加的内容</param>
/// <returns></returns>
public virtual object AddEntity(IList<T> entityList)
{
try
{
base.SourceEntity = entityList;
base.ConditionEntity = null;
base.OperateType = OperateType.INSERT;
base.EntityToSql(); string strSql = base.CommandString; return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>修改实体,要修改的内容和条件一一对应
/// </summary>
/// <param name="entityList">要修改的内容</param>
/// <param name="conditionList">条件</param>
/// <returns></returns>
public virtual int ModEntity(IList<T> entityList, IList<T> conditionList)
{
try
{
base.SourceEntity = entityList;
base.ConditionEntity = conditionList;
base.OperateType = OperateType.UPDATE; base.EntityToSql();
string strSql = base.CommandString;
return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
}
catch
{
throw;
}
}
/// <summary>根据主键修改实体
/// </summary>
/// <param name="entityList">要修改的内容和包含主键值的实体</param>
/// <returns></returns>
public virtual int ModEntity(IList<T> entityList)
{
try
{
IList<T> cndEntity = new List<T>();
CreatePKConditionFromSourceEntity(entityList, cndEntity);
return ModEntity(entityList, cndEntity);
}
catch
{
throw;
}
}
/// <summary>删除实体
/// </summary>
/// <param name="conditionList">删除的条件</param>
/// <returns></returns>
public virtual int DelEntity(IList<T> conditionList)
{
try
{
base.SourceEntity = conditionList;
base.ConditionEntity = conditionList;
base.OperateType = OperateType.DELETE; base.EntityToSql();
string strSql = base.CommandString;
return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
}
catch
{
throw;
}
}
/// <summary>得到实体
/// </summary>
/// <param name="condition">查询条件</param>
/// <returns></returns>
public virtual IList<T> GetEntity(T condition)
{
try
{
if (condition != null)
{
IList<T> coditionLists = new List<T>();
coditionLists.Add(condition); base.ConditionEntity = coditionLists;
base.SourceEntity = coditionLists;
}
base.OperateType = OperateType.SELECT; base.EntityToSql();
string strSql = base.CommandString;
return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray()));
}
catch
{
throw;
}
}
/// <summary>得到一个值
/// </summary>
/// <param name="conditionList">查询条件</param>
/// <returns></returns>
public virtual Object GetValue(T conditionList)
{
try
{
IList<T> coditionLists = new List<T>();
coditionLists.Add(conditionList); base.SourceEntity = coditionLists;
base.ConditionEntity = coditionLists;
base.OperateType = OperateType.SELECT; base.EntityToSql();
string strSql = base.CommandString;
return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, strSql, base.Parameters.ToArray());
}
catch
{
throw;
}
} //exce by sql
/// <summary>执行存储过程得到一个值
/// </summary>
/// <param name="sqlStr">存储过程的名称</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual Object GetValueByProc(string sqlStr, params DbParameter[] Parameters)
{
try
{
return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters);
}
catch
{
throw;
}
}
/// <summary>执行SQL语句得到一个值
/// </summary>
/// <param name="sqlStr">SQL语句</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual Object GetValueBySql(string sqlStr, params DbParameter[] Parameters)
{
try
{
return DBHelper.ExecuteScalar(dbCmd.Cmd, CommandType.Text, sqlStr, Parameters);
}
catch
{
throw;
}
}
/// <summary>执行SQL,返回受影响的函数
/// </summary>
/// <param name="sqlStr">存储过程的名称</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual int ExecuteSql(string sqlStr, params DbParameter[] Parameters)
{
try
{
return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.Text, sqlStr, Parameters);
}
catch
{
throw;
}
}
/// <summary>执行存储过程,返回受影响的函数
/// </summary>
/// <param name="sqlStr">存储过程的名称</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual int ExecuteProc(string sqlStr, params DbParameter[] Parameters)
{
try
{
return DBHelper.ExecuteNonQuery(dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters);
}
catch
{
throw;
}
} /// <summary>通过SQL语句得到实体
/// </summary>
/// <param name="sqlStr">SQL</param>
/// <returns></returns>
protected virtual IList<T> GetEntityBySql(string sqlStr)
{
try
{
return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, null));
}
catch
{
throw;
}
}
/// <summary>通过SQL语句得到DataSet
/// </summary>
/// <param name="sqlStr">SQL</param>
/// <returns></returns>
protected virtual DataSet GetDataSetBySql(string sqlStr)
{
try
{
return DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, null);
}
catch
{
throw;
}
}
/// <summary>通过SQL语句得到实体
/// </summary>
/// <param name="sqlStr">SQL</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual IList<T> GetEntityBySql(string sqlStr, params DbParameter[] Parameters)
{
try
{
return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.Text, sqlStr, Parameters));
}
catch
{
throw;
}
}
/// <summary>通过存储过程,语句得到实体
/// </summary>
/// <param name="sqlStr">SQL</param>
/// <param name="Parameters">参数</param>
/// <returns></returns>
protected virtual IList<T> GetEntityByProc(string sqlStr, params DbParameter[] Parameters)
{
try
{
return base.DateSetToEntity(DBHelper.GetDataSet(dbCmd.Adapter, dbCmd.Cmd, CommandType.StoredProcedure, sqlStr, Parameters));
}
catch
{
throw;
}
}
}
}
下一篇将讲下从代码上如何实现上面的设计图。