闲暇之余,整理了一下EF底层的一些基础方法,供查看,只有接口,具体实现需要你们自己写了。
建议:接口的实现定义为虚方法,当父类的方法不满住子类需求时,可以重写此方法
此接口都为公用方法,基本上满足小系统的实际开发需求,如果你觉得满足不了你,可以扩展此接口。
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.SqlClient; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace Basic.IRepository { public interface ICommonRepository<T> where T : class, new() { #region Other /// <summary> /// 确定序列中的任何元素是否都满足条件 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> bool Any(Expression<Func<T, bool>> whereLambda); /// <summary> /// 返回序列中元素的数量 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> int Count(Expression<Func<T, bool>> whereLambda); /// <summary> /// 返回最大值 /// </summary> /// <typeparam name="Tkey"></typeparam> /// <param name="predicate"></param> /// <param name="selector"></param> /// <returns></returns> Tkey Max<Tkey>(Expression<Func<T, bool>> whereLambda, Func<T, Tkey> selector); /// <summary> /// 返回分组查询的数量 /// </summary> /// <typeparam name="TKey"></typeparam> /// <param name="whereLambda"></param> /// <param name="groupBy"></param> /// <returns></returns> IDictionary<TKey, int> Count<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> groupBy); /// <summary> /// 分组求和 /// </summary> /// <typeparam name="TKey"></typeparam> /// <param name="whereLambda"></param> /// <param name="groupBy"></param> /// <param name="selector"></param> /// <returns></returns> IDictionary<TKey, decimal> Sum<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> groupBy, Func<T, decimal> selector); /// <summary> /// 获取表名 /// </summary> /// <param name="type"></param> /// <returns></returns> string GetTableName(Type type); /// <summary> /// 得到SqlServer 表相关信息(字段名,类型.....) /// </summary> /// <param name="tableName"></param> /// <returns></returns> IEnumerable<SqlServerColumnModel> GetSqlServerTableOrViewColumns(string tableName); /// <summary> /// 得到MySql 表相关信息(字段名,类型.....) /// </summary> /// <param name="tableName"></param> /// <returns></returns> IEnumerable<MySqlColumnModel> GetMySqlTableOrViewColumns(string tableName); #endregion #region Insert /// <summary> /// 添加 /// </summary> /// <param name="model"></param> /// <returns></returns> bool Add(T model); /// <summary> /// 批量添加 /// </summary> /// <param name="model"></param> /// <returns></returns> bool AddRange(IEnumerable<T> model); /// <summary> /// 批量添加 生成Sql执行 /// </summary> /// <param name="model"></param> /// <param name="clearHistoryData"></param> void BulkInsertToSql(IEnumerable<T> model, bool clearHistoryData = false); /// <summary> /// 批量添加 SqlServer /// 通过 SqlBulkCopy 实现 /// </summary> /// <param name="model"></param> /// <param name="clearHistoryData"></param> void BulkInsertSqlServer(IEnumerable<T> model, bool clearHistoryData = false); /// <summary> /// 批量添加 MySql /// 通过 MySqlBulkLoader 实现 /// </summary> /// <param name="model"></param> /// <param name="clearHistoryData"></param> int BulkInsertMySql(IEnumerable<T> model, string csvPath, bool clearHistoryData = false); #endregion #region Delete /// <summary> /// 按主键删除 /// </summary> /// <param name="id"></param> bool DeleteById(dynamic id); /// <summary> /// 删除 /// </summary> /// <param name="model"></param> /// <returns></returns> bool Delete(T model); /// <summary> /// 按条件删除 /// </summary> /// <param name="id"></param> bool DeleteByWhere(Expression<Func<T, bool>> whereLambda); /// <summary> /// 批量删除 /// </summary> /// <param name="model"></param> /// <returns></returns> bool DeleteRange(IEnumerable<T> model); /// <summary> /// 批量删除 /// </summary> /// <param name="where">条件:{Name="张三",性别="男"}</param> /// <returns></returns> bool BulkDelete(object where); /// <summary> /// 批量删除 /// </summary> /// <param name="where">条件:{Name="张三",性别="男"}</param> /// <param name="notWhere">过滤的条件:{Name="张三",年龄="18"}</param> /// <returns></returns> bool BulkDelete(object where,object notWhere); #endregion #region Update /// <summary> /// 更新 /// </summary> /// <param name="model"></param> /// <returns></returns> bool Update(T model); /// <summary> /// 批量更新 /// </summary> /// <param name="model"></param> /// <returns></returns> bool UpdateRange(IEnumerable<T> models); /// <summary> /// 批量更新,不带条件 /// </summary> /// <param name="set">修改的数据:{Name="张三",年龄=22}</param> /// <returns></returns> bool BulkUpdate(object set); /// <summary> /// 批量更新,带条件 /// </summary> /// <param name="set">修改的数据:{Name="张三",年龄=22}</param> /// <param name="where">条件:{Name="张三",性别="男"}</param> /// <returns></returns> bool BulkUpdate(object set, object where); #endregion #region Select /// <summary> /// 按主键查询 /// </summary> /// <param name="id">主键</param> /// <returns></returns> T Find(dynamic id); /// <summary> /// 通过id查看是否存在 /// </summary> /// <param name="id">主键</param> /// <returns></returns> bool IsExist(dynamic id); /// <summary> /// 返回序列中的第一个元素 /// </summary> /// <param name="whereLambda">条件</param> /// <returns></returns> T FirstOrDefault(Expression<Func<T, bool>> whereLambda); /// <summary> /// 获取数据 /// </summary> /// <typeparam name="TObj">排序类型</typeparam> /// <param name="whereLambda">条件</param> /// <param name="orderBy">排序字段</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> Find<TObj>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TObj>> orderBy, bool isASC = true); /// <summary> /// 获取数据 /// </summary> /// <typeparam name="TKey">排序类型 1</typeparam> /// <typeparam name="TObj">排序类型 2</typeparam> /// <param name="whereLambda">条件</param> /// <param name="orderBy">排序字段 1</param> /// <param name="thenBy">排序字段 2</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> Find<TKey,TObj>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, Expression<Func<T, TObj>> thenBy, bool isASC = true); /// <summary> /// 分页查询 生成SQL执行 SQLSERVER /// </summary> /// <param name="name">表名/视图</param> /// <param name="where">条件 name='Jamel' and age = 18</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">数量</param> /// <param name="orderBy">排序字段</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> PagerToSqlServer(string name, string where, int pageIndex, int pageSize, object orderBy, bool isASC = true); /// <summary> /// 分页查询 生成SQL执行 MYSQL /// </summary> /// <param name="name">表名/视图</param> /// <param name="where">条件 name='Jamel' and age = 18</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">数量</param> /// <param name="orderBy">排序字段</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> PagerToMySql(string name, string where, int pageIndex, int pageSize, object orderBy, bool isASC = true); /// <summary> /// 分页查询 /// </summary> /// <typeparam name="TObj">排序类型</typeparam> /// <param name="whereLambda">条件</param> /// <param name="orderBy">排序字段</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">数量</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> Pager<TObj>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TObj>> orderBy, int pageIndex,int pageSize, bool isASC = true); /// <summary> /// 分页查询 /// </summary> /// <typeparam name="TKey">排序类型 1</typeparam> /// <typeparam name="TObj">排序类型 2</typeparam> /// <param name="whereLambda">条件</param> /// <param name="orderBy">排序字段 1</param> /// <param name="thenBy">排序字段 2</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">数量</param> /// <param name="isASC">是否升序</param> /// <returns></returns> IEnumerable<T> Pager<TKey, TObj>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, Expression<Func<T, TObj>> thenBy, int pageIndex, int pageSize, bool isASC = true); #endregion #region Proc /// <summary> /// 执行存储过程或自定义sql语句--返回集合(自定义返回类型) /// SqlServer: exec getActionUrlId @name,@ID /// MySql: call getActionUrlId(@name,@ID) /// </summary> /// <param name="sql">sql语句或存储过程</param> /// <param name="parms">参数 new SqlParameter("@name","Jamel")</param> /// <returns></returns> IEnumerable<T> SqlQuery(string sql, IEnumerable<SqlParameter> parms); /// <summary> /// 自定义语句和存储过程的增删改--返回影响的行数 /// SqlServer: exec getActionUrlId @name,@ID /// MySql: call getActionUrlId(@name,@ID) /// </summary> /// <param name="sql">sql语句或存储过程</param> /// <param name="parms">参数 new SqlParameter("@name","Jamel")</param> /// <returns></returns> int ExecuteSqlCommand(string sql, IEnumerable<SqlParameter> parms); #endregion } }
作者:JamelAr
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。