一个通用的LinQ操作类(将开发人员从单表的增删改查中解放出来)
来源:互联网 发布:淘宝买被子 编辑:程序博客网 时间:2024/04/29 16:01
在写数据库操作类时,需要对每张数据库表写基本的增删改查操作,及其繁琐且无聊。
故思考如何能简化,然后就有了下面的通用操作类。
使用要求:
1. 主键字段必须是 'id',不可为其它的。
2. 如需要假删,请对应修改假删的标识字段名(这里假设为'deleteFlag')
优点:
开发人员无须编写单表的增删改查方法。
缺点:
反射,以及大量的遍历;性能低下。
操作类源码如下:
使用方法如下:
万事OK!
故思考如何能简化,然后就有了下面的通用操作类。
使用要求:
1. 主键字段必须是 'id',不可为其它的。
2. 如需要假删,请对应修改假删的标识字段名(这里假设为'deleteFlag')
优点:
开发人员无须编写单表的增删改查方法。
缺点:
反射,以及大量的遍历;性能低下。
操作类源码如下:
/// <summary>
/// 数据库单表操作接口(无实际意义,规范 数据库单表操作 的接口)
/// </summary>
/// <typeparam name="Context">LinQ to SQL类</typeparam>
/// <typeparam name="T">映射表LinQ类</typeparam>
public abstract class ITableDAL<Context, T>
where Context : DataContext, new()
where T : class, INotifyPropertyChanging, INotifyPropertyChanged
{
protected const string GET_ENTITY_BY_ID_SQL = "SELECT * FROM {0} WHERE id = '{1}'";
/// <summary>
///
/// </summary>
protected Context db = new Context();
/// <summary>
/// 根据ID获取数据
/// </summary>
/// <param name="id">表ID</param>
/// <returns></returns>
public virtual T GetEntityById(int id)
{
try
{
return db.ExecuteQuery<T>((new StringBuilder()).AppendFormat(GET_ENTITY_BY_ID_SQL, typeof(T).Name, id).ToString()).Single();
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("数据库表'{0}'中不存在'{1}'为'{2}'的数据", typeof(T).Name, "id", id.ToString());
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 获取所有数据
/// </summary>
/// <returns></returns>
public virtual List<T> GetEntities()
{
try
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
return table.ToList();
}
catch
{
return new List<T>();
}
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="data">需添加的数据</param>
/// <returns></returns>
public virtual int AddEntity(T data)
{
try
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
table.InsertOnSubmit(data);
db.SubmitChanges();
return Convert.ToInt32(typeof(T).GetProperty("id").GetValue(data, null));
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'插入数据失败", typeof(T).Name);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="data">需修改的数据</param>
/// <returns></returns>
public virtual bool EditEntity(T data)
{
try
{
Type t = typeof(T);
T editData = GetEntityById(Convert.ToInt32(t.GetProperty("id").GetValue(data, null)));
foreach (PropertyInfo property in t.GetProperties())
{
object newValue = property.GetValue(data, null);
bool isColumn = false;
foreach (object attr in property.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(ColumnAttribute))
{
isColumn = true;
break;
}
}
if ((isColumn == true) && (newValue != null))
{
property.SetValue(editData, newValue, null);
}
}
db.SubmitChanges();
return true;
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'更新数据失败", typeof(T).Name);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="id">需删除数据的ID</param>
/// <param name="isRealDelete">是否真删</param>
/// <returns></returns>
public virtual bool DeleteEntity(int id, bool isRealDelete)
{
try
{
T deleteData = GetEntityById(id);
if (isRealDelete)
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
table.DeleteOnSubmit(deleteData);
}
else
{
typeof(T).GetProperty("deleteFlag").SetValue(deleteData, 1, null);
}
db.SubmitChanges();
return true;
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'删除ID为'{1}'的数据失败", typeof(T).Name, id);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 删除数据(默认真删)
/// </summary>
/// <param name="id">需删除数据的ID</param>
/// <returns></returns>
public bool DeleteEntity(int id)
{
return DeleteEntity(id, true);
}
}
/// 数据库单表操作接口(无实际意义,规范 数据库单表操作 的接口)
/// </summary>
/// <typeparam name="Context">LinQ to SQL类</typeparam>
/// <typeparam name="T">映射表LinQ类</typeparam>
public abstract class ITableDAL<Context, T>
where Context : DataContext, new()
where T : class, INotifyPropertyChanging, INotifyPropertyChanged
{
protected const string GET_ENTITY_BY_ID_SQL = "SELECT * FROM {0} WHERE id = '{1}'";
/// <summary>
///
/// </summary>
protected Context db = new Context();
/// <summary>
/// 根据ID获取数据
/// </summary>
/// <param name="id">表ID</param>
/// <returns></returns>
public virtual T GetEntityById(int id)
{
try
{
return db.ExecuteQuery<T>((new StringBuilder()).AppendFormat(GET_ENTITY_BY_ID_SQL, typeof(T).Name, id).ToString()).Single();
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("数据库表'{0}'中不存在'{1}'为'{2}'的数据", typeof(T).Name, "id", id.ToString());
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 获取所有数据
/// </summary>
/// <returns></returns>
public virtual List<T> GetEntities()
{
try
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
return table.ToList();
}
catch
{
return new List<T>();
}
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="data">需添加的数据</param>
/// <returns></returns>
public virtual int AddEntity(T data)
{
try
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
table.InsertOnSubmit(data);
db.SubmitChanges();
return Convert.ToInt32(typeof(T).GetProperty("id").GetValue(data, null));
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'插入数据失败", typeof(T).Name);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="data">需修改的数据</param>
/// <returns></returns>
public virtual bool EditEntity(T data)
{
try
{
Type t = typeof(T);
T editData = GetEntityById(Convert.ToInt32(t.GetProperty("id").GetValue(data, null)));
foreach (PropertyInfo property in t.GetProperties())
{
object newValue = property.GetValue(data, null);
bool isColumn = false;
foreach (object attr in property.GetCustomAttributes(false))
{
if (attr.GetType() == typeof(ColumnAttribute))
{
isColumn = true;
break;
}
}
if ((isColumn == true) && (newValue != null))
{
property.SetValue(editData, newValue, null);
}
}
db.SubmitChanges();
return true;
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'更新数据失败", typeof(T).Name);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="id">需删除数据的ID</param>
/// <param name="isRealDelete">是否真删</param>
/// <returns></returns>
public virtual bool DeleteEntity(int id, bool isRealDelete)
{
try
{
T deleteData = GetEntityById(id);
if (isRealDelete)
{
Table<T> table = typeof(Context).GetProperty(typeof(T).Name).GetValue(db, null) as Table<T>;
table.DeleteOnSubmit(deleteData);
}
else
{
typeof(T).GetProperty("deleteFlag").SetValue(deleteData, 1, null);
}
db.SubmitChanges();
return true;
}
catch (System.Exception ex)
{
StringBuilder errorMsg = new StringBuilder();
errorMsg.AppendFormat("对数据库表'{0}'删除ID为'{1}'的数据失败", typeof(T).Name, id);
throw new ApplicationException(errorMsg.ToString(), ex);
}
}
/// <summary>
/// 删除数据(默认真删)
/// </summary>
/// <param name="id">需删除数据的ID</param>
/// <returns></returns>
public bool DeleteEntity(int id)
{
return DeleteEntity(id, true);
}
}
使用方法如下:
//假设:
//LinQ映射类为 LinQDataContext
//数据库表名为 tbData
public class DataDAL : ITableDAL<LinQDataContext, tbData>
{
}
//LinQ映射类为 LinQDataContext
//数据库表名为 tbData
public class DataDAL : ITableDAL<LinQDataContext, tbData>
{
}
万事OK!
- 一个通用的LinQ操作类(将开发人员从单表的增删改查中解放出来)
- Funny Linq Part1: 建一个 Linq 数据库通用的操作类 (上)
- Funny Linq Part2: 建一个 Linq 数据库通用的操作类 (下)
- 声控技术竞赛的战斗已然打响,聊天机器人如何将我们从屏幕上解放出来?
- 通用的数据库增删改操作类
- 通用的增删改操作
- 封装JDBC的一个通用增删改查工具类,将数据集rs转为list
- Linq对GridView 增删改操作(单表操作)
- 将使用者从实现细节中解放出来
- springboot中通用mapper结合mybatis generator的使用 通用mapper是为了方便开发人员对单表进行crud操作而产生的一套通用方法。 通用mapper只支持单表操作,可以
- 开发人员眼中的LINQ
- 一个文员级的报表工具Wreport终于开发完成,用word设计报表,最终把程序员完全从报表中解放出来
- mybatis入门--单表的增删改操作
- MySQL基础操作之单表的增删改
- JAVA操作数据库的一个通用类
- Linq to SQL 简单的增删改操作
- 用JDBCTemplate实现的单表通用DAO,实现增删改查和统计
- 分享一个自己抽象出来的Manager管理类,封装增删改,查的方法
- C# 使用ICallbackEventHandler实现客户端服务器端异步通信
- IE8去掉地址栏,同时关闭父页面,打开新页面
- SQL递归游戏
- Android的之我见
- 如何组建测试团队
- 一个通用的LinQ操作类(将开发人员从单表的增删改查中解放出来)
- QCA Installation
- openssl error: unrecognized command line option "-m486"
- SQLite Optimization FAQ
- 跨域访问sessiono丢失问题说明及解决方法
- Android+Eclipse,创建项目总是出现 invalid project description. overlaps the workspace location的解决办法
- 目录无限循环递归是怎么回事?
- Mplayer的秘密功能
- WM6的WIN32程序创建菜单,利用资源文件