比较好的Dapper封装的仓储实现类 来源:https://www.cnblogs.com/liuchang/articles/4220671.html
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Cache;
using System.Text;
using Dapper;
using DapperExtensions;
using TestData.Entity;
using TestData.Business; namespace TestData.Business
{
/// <summary>
/// 业务逻辑的基类 包含了一些简单的操作
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseManager<T> where T : class, IDataEntity
{
/// <summary>
/// 查询所有
/// </summary>
/// <returns></returns>
public IEnumerable<T> GetAll()
{
using (var conn = ConnectionFactory.Connection)
{
return conn.GetList<T>();
}
} /// <summary>
/// 根绝sql查询
/// </summary>
/// <param name="sql">sql</param>
/// <param name="parameters">参数列表</param>
/// <returns></returns>
public IEnumerable<T> SelectCommond(string sql, object parameters = null)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<T>(sql, parameters);
}
}
/// <summary>
/// 根据表明查询
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public IEnumerable<T> GetAll(string name)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<T>(string.Format("select * from {0}", name));
}
}
/// <summary>
/// 根据编号查询
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T GetById(int? id)
{
if (id == null)
return default(T);
using (var conn = ConnectionFactory.Connection)
{ return conn.Get<T>(id.Value);
}
}
/// <summary>
/// 修改实体
/// </summary>
/// <param name="t">实体对象</param>
/// <returns></returns>
public bool Update(T t)
{
using (var conn = ConnectionFactory.Connection)
{
t.EditorDate = DateTime.Now;
return conn.Update(t);
}
} /// <summary>
/// 得到数量
/// </summary>
/// <returns></returns>
public int GetCount()
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Count<T>(null);
}
} /// <summary>
/// 分页
/// </summary>
/// <param name="predicate"></param>
/// <param name="pageindex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public Tuple<int, IEnumerable<T>> GetPaged(object predicate, int pageindex, int pageSize)
{
using (var conn = ConnectionFactory.Connection)
{
var sort = new List<ISort>
{
Predicates.Sort<T>(p=>p.EditorDate)
}; var total = conn.Count<T>(predicate);
return new Tuple<int, IEnumerable<T>>(total, conn.GetPage<T>(predicate, sort, pageindex, pageSize).ToList());
}
}
/// <summary>
/// 添加
/// </summary>
/// <param name="t">实体对象</param>
/// <returns></returns>
public bool Insert(T t)
{
t.EditorDate = DateTime.Now;
return this.Add(t, false) == t.Id;
}
/// <summary>
/// 添加实体集合
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public bool Insert(List<T> list)
{
using (var conn = ConnectionFactory.Connection)
{
list.ForEach(p => p.EditorDate = DateTime.Now);
return conn.Insert(list);
}
} /// <summary>
/// 添加实体
/// </summary>
/// <param name="t">实体对象</param>
/// <param name="isAutoGenId"></param>
/// <returns></returns>
public int Add(T t, bool isAutoGenId = true)
{
using (var conn = ConnectionFactory.Connection)
{
//var maxindex = conn.Query<int?>(string.Format("select max(indexs) from {0}", typeof(T).Name)).FirstOrDefault() ?? 0;
//t.Indexs = maxindex + 1;
return conn.Insert(t, isGenId: isAutoGenId);
}
}
/// <summary>
/// 根据编号删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool Delete(int? id)
{
var obj = this.GetById(id);
if (obj == null) return false;
return this.Update(obj);
}
/// <summary>
/// 根据编号修改
/// </summary>
/// <param name="id"></param>
/// <param name="mark"></param>
/// <returns></returns>
public bool UpdataStatus(int? id)
{
var obj = this.GetById(id);
if (obj == null) return false;
return this.Update(obj);
}
/// <summary>
/// 根据外键得到数据
/// </summary>
/// <param name="foreignKeyName">外键名称</param>
/// <param name="foreignKeyValue">外键的值</param>
/// <returns></returns>
public IEnumerable<T> GetByForeignKey(string foreignKeyName, Guid foreignKeyValue)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<T>(string.Format("select * from {0} where {1}=@value", typeof(T).Name, foreignKeyName), new { value = foreignKeyValue });
}
}
/// <summary>
/// 根据列查询
/// </summary>
/// <param name="fieldName">列名称</param>
/// <param name="fieldValue">列的值</param>
/// <returns></returns>
public IEnumerable<T> GetByField(string fieldName, dynamic fieldValue)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<T>(string.Format("select * from {0} where {1}=@value", typeof(T).Name, fieldName), new { value = fieldValue });
}
}
/// <summary>
/// lxh 根据某列查询的方法--带排序
/// </summary>
/// <param name="fieldName">查询列名</param>
/// <param name="fieldValue">条件内容</param>
/// <param name="sortFieldName">排序列名</param>
/// <returns></returns>
public IEnumerable<T> GetByField(string fieldName, dynamic fieldValue, string sortFieldName)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<T>(string.Format("select * from {0} where {1}=@value order by {2}", typeof(T).Name, fieldName, sortFieldName), new { value = fieldValue });
}
} /// <summary>
/// lxh 获取排序号的方法
/// </summary>
/// <returns></returns>
public int GetNextSequence(T t)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query<int>(string.Format("select isnull(max(Sequence),0)+1 from {0}", typeof(T).Name)).FirstOrDefault();
}
}
/// <summary>
/// 存储过程
/// </summary>
/// <param name="procName"></param>
/// <param name="obj"></param>
/// <returns></returns>
public List<dynamic> SelectProc(string procName, object obj = null)
{
using (var conn = ConnectionFactory.Connection)
{
return conn.Query(procName, obj, commandType: CommandType.StoredProcedure).ToList();
}
}
}
}
using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Net.Cache;using System.Text;using Dapper;using DapperExtensions;using TestData.Entity;using TestData.Business;
namespace TestData.Business{ /// <summary> /// 业务逻辑的基类 包含了一些简单的操作 /// </summary> /// <typeparam name="T"></typeparam> public abstract class BaseManager<T> where T : class, IDataEntity { /// <summary> /// 查询所有 /// </summary> /// <returns></returns> public IEnumerable<T> GetAll() { using (var conn = ConnectionFactory.Connection) { return conn.GetList<T>(); } }
/// <summary> /// 根绝sql查询 /// </summary> /// <param name="sql">sql</param> /// <param name="parameters">参数列表</param> /// <returns></returns> public IEnumerable<T> SelectCommond(string sql, object parameters = null) { using (var conn = ConnectionFactory.Connection) { return conn.Query<T>(sql, parameters); } } /// <summary> /// 根据表明查询 /// </summary> /// <param name="name"></param> /// <returns></returns> public IEnumerable<T> GetAll(string name) { using (var conn = ConnectionFactory.Connection) { return conn.Query<T>(string.Format("select * from {0}", name)); } } /// <summary> /// 根据编号查询 /// </summary> /// <param name="id"></param> /// <returns></returns> public T GetById(int? id) { if (id == null) return default(T); using (var conn = ConnectionFactory.Connection) {
return conn.Get<T>(id.Value); } } /// <summary> /// 修改实体 /// </summary> /// <param name="t">实体对象</param> /// <returns></returns> public bool Update(T t) { using (var conn = ConnectionFactory.Connection) { t.EditorDate = DateTime.Now; return conn.Update(t); } }
/// <summary> /// 得到数量 /// </summary> /// <returns></returns> public int GetCount() { using (var conn = ConnectionFactory.Connection) { return conn.Count<T>(null); } }
/// <summary> /// 分页 /// </summary> /// <param name="predicate"></param> /// <param name="pageindex"></param> /// <param name="pageSize"></param> /// <returns></returns> public Tuple<int, IEnumerable<T>> GetPaged(object predicate, int pageindex, int pageSize) { using (var conn = ConnectionFactory.Connection) { var sort = new List<ISort> { Predicates.Sort<T>(p=>p.EditorDate) };
var total = conn.Count<T>(predicate); return new Tuple<int, IEnumerable<T>>(total, conn.GetPage<T>(predicate, sort, pageindex, pageSize).ToList()); } } /// <summary> /// 添加 /// </summary> /// <param name="t">实体对象</param> /// <returns></returns> public bool Insert(T t) { t.EditorDate = DateTime.Now; return this.Add(t, false) == t.Id; } /// <summary> /// 添加实体集合 /// </summary> /// <param name="list"></param> /// <returns></returns> public bool Insert(List<T> list) { using (var conn = ConnectionFactory.Connection) { list.ForEach(p => p.EditorDate = DateTime.Now); return conn.Insert(list); } }
/// <summary> /// 添加实体 /// </summary> /// <param name="t">实体对象</param> /// <param name="isAutoGenId"></param> /// <returns></returns> public int Add(T t, bool isAutoGenId = true) { using (var conn = ConnectionFactory.Connection) { //var maxindex = conn.Query<int?>(string.Format("select max(indexs) from {0}", typeof(T).Name)).FirstOrDefault() ?? 0; //t.Indexs = maxindex + 1; return conn.Insert(t, isGenId: isAutoGenId); } } /// <summary> /// 根据编号删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public bool Delete(int? id) { var obj = this.GetById(id); if (obj == null) return false; return this.Update(obj); } /// <summary> /// 根据编号修改 /// </summary> /// <param name="id"></param> /// <param name="mark"></param> /// <returns></returns> public bool UpdataStatus(int? id) { var obj = this.GetById(id); if (obj == null) return false; return this.Update(obj); } /// <summary> /// 根据外键得到数据 /// </summary> /// <param name="foreignKeyName">外键名称</param> /// <param name="foreignKeyValue">外键的值</param> /// <returns></returns> public IEnumerable<T> GetByForeignKey(string foreignKeyName, Guid foreignKeyValue) { using (var conn = ConnectionFactory.Connection) { return conn.Query<T>(string.Format("select * from {0} where {1}=@value", typeof(T).Name, foreignKeyName), new { value = foreignKeyValue }); } } /// <summary> /// 根据列查询 /// </summary> /// <param name="fieldName">列名称</param> /// <param name="fieldValue">列的值</param> /// <returns></returns> public IEnumerable<T> GetByField(string fieldName, dynamic fieldValue) { using (var conn = ConnectionFactory.Connection) { return conn.Query<T>(string.Format("select * from {0} where {1}=@value", typeof(T).Name, fieldName), new { value = fieldValue }); } } /// <summary> /// lxh 根据某列查询的方法--带排序 /// </summary> /// <param name="fieldName">查询列名</param> /// <param name="fieldValue">条件内容</param> /// <param name="sortFieldName">排序列名</param> /// <returns></returns> public IEnumerable<T> GetByField(string fieldName, dynamic fieldValue, string sortFieldName) { using (var conn = ConnectionFactory.Connection) { return conn.Query<T>(string.Format("select * from {0} where {1}=@value order by {2}", typeof(T).Name, fieldName, sortFieldName), new { value = fieldValue }); } }
/// <summary> /// lxh 获取排序号的方法 /// </summary> /// <returns></returns> public int GetNextSequence(T t) { using (var conn = ConnectionFactory.Connection) { return conn.Query<int>(string.Format("select isnull(max(Sequence),0)+1 from {0}", typeof(T).Name)).FirstOrDefault(); } } /// <summary> /// 存储过程 /// </summary> /// <param name="procName"></param> /// <param name="obj"></param> /// <returns></returns> public List<dynamic> SelectProc(string procName, object obj = null) { using (var conn = ConnectionFactory.Connection) { return conn.Query(procName, obj, commandType: CommandType.StoredProcedure).ToList(); } } }}
比较好的Dapper封装的仓储实现类 来源:https://www.cnblogs.com/liuchang/articles/4220671.html的更多相关文章
- OSS.Core基于Dapper封装(表达式解析+Emit)仓储层的构思及实现
最近趁着不忙,在构思一个搭建一个开源的完整项目,至于原因以及整个项目框架后边文章我再说明.既然要起一个完整的项目,那么数据仓储访问就必不可少,这篇文章我主要介绍这个新项目(OSS.Core)中我对仓储 ...
- .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
本篇我将带着大家一起来对Dapper进行下封装并实现基本的增删改查.分页操作的同步异步方法的实现(已实现MSSQL,MySql,PgSQL).同时我们再实现一下仓储层的代码生成器,这样的话,我们只需要 ...
- 同时支持EF+Dapper的混合仓储,助你快速搭建数据访问层
背景 17年开始,公司开始向DotNet Core转型,面对ORM工具的选型,当时围绕Dapper和EF发生了激烈的讨论.项目团队更加关注快速交付,他们主张使用EF这种能快速开发的ORM工具:而在线业 ...
- .Net Core中使用Dapper构建泛型仓储
前言:Dapper是.NET的简单对象映射器,在速度方面拥有ORM之王的称号,与使用原始ADO.NET读取数据一样快.ORM是对象关系映射器,它负责数据库和编程语言之间的映射. 仓储主要是用来解耦业务 ...
- 基于EFCore3.0+Dapper 封装Repository
Wei.Repository 基于EFCore3.0+Dapper 封装Repository,实现UnitOfWork,提供基本的CURD操作,可直接注入泛型Repository,也可以继承Repos ...
- EsayUI + MVC + ADO.NET(仓储基类)
该篇主要讲解工作单元的运用 和DbHlper抽象封装 工作单元仓储基类:BaseRepository (DbHlper抽象封装) 仓储接口(CRUD):IRepository (CRUD接口) ...
- Android RecyclerView单击、长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类
Android RecyclerView单击.长按事件:基于OnItemTouchListener +GestureDetector标准实现(二),封装抽取成通用工具类 我写的附录文章2,介绍了 ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
随机推荐
- iOS11访问相册权限变更问题
手机升到iOS 11后 发现之前正常的图片保存功能无法正常使用 会闪退 经测试发现应该是权限没有开启的原因 但是NSPhotoLibraryUsageDescription已经写入plist ...
- Mac Technology Overview
[Mac Technology Overview]https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual ...
- Centos7下安装Mongodb
Mongodb网盘路径:链接:https://pan.baidu.com/s/1rWJCPZ59EAW25ha1UF5czw 密码:u3zq 1.把安装包上传到linux服务器上,解压,然后我们把mo ...
- HDU 1575 Tr A( 简单矩阵快速幂 )
链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /********************************************************** ...
- PHP下的异步尝试四:PHP版的Promise
PHP下的异步尝试系列 如果你还不太了解PHP下的生成器和协程,你可以根据下面目录翻阅 PHP下的异步尝试一:初识生成器 PHP下的异步尝试二:初识协程 PHP下的异步尝试三:协程的PHP版thunk ...
- 基于Quick_Thought Vectors的Sentence2Vec神经网络实现
一.前言 1.Skip-Thought-Vector论文 https://github.com/ryankiros/skip-thoughts 2.本文假设读者已了解Skip-Gram-Vector和 ...
- python学习笔记:第三天
day03: 1.python中所有的变量都可以改变 2.Print(name) 打印 3.Del name 删除 4.python中python2与python3不能兼容,需要分别安装pyth ...
- python--(常用模块-1)
python--(常用模块-1) 一.模块的简单认识: 什么是模块,模块就是我们把装有特有功能的代码进行归类的结果,从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块 ...
- COGS——T 1175. [顾研NOIP] 旅游电车
http://www.cogs.pro/cogs/problem/problem.php?pid=1175 ★★☆ 输入文件:buss.in 输出文件:buss.out 简单对比时间限制: ...
- 极路由4pro(HC5962)安装python
基本配置 其实极路由4.极路由4 pro.极路由B70都是一个型号的(HC5962) CPU:MT7621AT + MT7612EN + 7603EN 内存:256MB DDR3 RAM 闪存:128 ...