一、数据库提供者接口
/// <summary> /// 数据库提供者 /// </summary> public interface IDbProvider : IDisposable { /// <summary> /// 初始化数据库连接信息 /// </summary> /// <param name="dbConnInfo"></param> /// <returns></returns> IDbProvider Init(DbConnInfo dbConnInfo); /// <summary> /// 获取Lambda语法的数据操作对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> IReqository<T> GetReqository<T>() where T : class, new(); ///<summary> /// 获取Linq语法的数据查询对象 ///</summary> ///<typeparam name="T"></typeparam> ///<returns></returns> IQuery<T> GetQuery<T>() where T : class, new(); ///<summary> /// 扩展操作 ///</summary> ///<returns></returns> IDbExtension GetDbExtension(); /// <summary> /// 事务开启 /// </summary> void Begin(); /// <summary> /// 事务提交 /// </summary> void Commit(); /// <summary> /// 事务回滚 /// </summary> void Rollback(); } ///<summary> /// 扩展数据库提供者 ///</summary> internal interface IDbProviderExtend : IDbProvider { IDbExecutor DbExecutor { get; } }
二、单表操作
/// <summary> /// 标准的数据层接口 /// </summary> /// <typeparam name="T"></typeparam> public interface IReqository<T> where T : class { /// <summary> /// 新增实体对象 /// </summary> /// <param name="entity">实体对象</param> void Insert(T entity); /// <summary> /// 批量新增实体对象 /// </summary> /// <param name="entitys"></param> void Insert(IList<T> entitys); /// <summary> /// 根据条件表达式自动删除对应数据 /// </summary> /// <param name="where"></param> /// <returns></returns> int Delete(Expression<Func<T, bool>> where); /// <summary> /// 根据条件表达式将映射对象的数据进行局部更新 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="update"></param> /// <param name="where"></param> /// <returns></returns> int Update<TResult>(Expression<Func<T, TResult>> update, Expression<Func<T, bool>> where) where TResult : class; /// <summary> /// 根据条件表达式获得[Top 1]实体对象 /// </summary> /// <param name="where"></param> /// <param name="nolock">是否允许脏读</param> /// <returns></returns> T Single(Expression<Func<T, bool>> where, bool nolock = false); /// <summary> /// 根据条件表达式获得实体对象列表 /// </summary> /// <param name="where">条件表达式</param> /// <param name="nolock">是否允许脏读</param> /// <returns></returns> IList<T> Select(Expression<Func<T, bool>> where, bool nolock = false); /// <summary> /// 根据条件表达式获得指定记录的条数 /// </summary> /// <param name="where"></param> /// <param name="nolock">是否允许脏读</param> /// <returns></returns> int Count(Expression<Func<T, bool>> where, bool nolock = false); /// <summary> /// 根据条件表达式获取某列的和 /// </summary> /// <param name="select"></param> /// <param name="where"></param> /// <param name="noLock"></param> /// <returns></returns> object Sum<TResult>(Expression<Func<T, TResult>> select, Expression<Func<T, bool>> where, bool noLock = false); }
三、多表操作
///<summary> /// 多表查询对象接口 ///</summary> ///<typeparam name="T">数据实体</typeparam> public interface IQuery<out T> : IOrderedQueryable<T> { } ///<summary> /// IQuery的扩展方法 ///</summary> public static class Query { ///<summary> /// 分页功能 ///</summary> ///<param name="source"></param> ///<param name="rowCount">每页的行数</param> ///<param name="pageNumber">第几页</param> ///<typeparam name="TSource"></typeparam> ///<returns></returns> public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int rowCount, int pageNumber) { if (source == null) { throw new ArgumentNullException("source"); } return source.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof (TSource) }), new Expression[] { source.Expression, Expression.Constant(rowCount), Expression.Constant(pageNumber) })); } ///<summary> /// 扩展join功能 ///</summary> ///<param name="source"></param> ///<param name="joins"></param> ///<typeparam name="TSource"></typeparam> ///<returns></returns> public static IQueryable<TSource> SetJoinMode<TSource>(this IQueryable<TSource> source, params JoinMode[] joins) { if (source == null) { throw new ArgumentNullException("source"); } return source.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()). MakeGenericMethod(new Type[] {typeof (TSource)}), new Expression[] { source.Expression, Expression.Constant(joins) })); } } ///<summary> ///</summary> public enum JoinMode { ///<summary> /// 内连接 ///</summary> Inner, ///<summary> /// 左连接 ///</summary> Left, ///<summary> /// 右连接 ///</summary> Right, ///<summary> /// 全连接 ///</summary> Full }
四、扩展操作
/// <summary> /// 扩展操作 /// </summary> public interface IDbExtension { /// <summary> /// 执行存储过程返回影响行数 /// </summary> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> int ExcuteProcNonQuery(string name, DbParameterCollection parameters = null); /// <summary> /// 执行存储过程返回DataSet /// </summary> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> DataSet ExcuteProcQuery(string name, DbParameterCollection parameters = null); /// <summary> /// 执行存储过程返回泛型集合 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="name"></param> /// <param name="parameters"></param> /// <returns></returns> IList<TResult> ExcuteProcReader<TResult>(string name, DbParameterCollection parameters = null); /// <summary> /// 执行sql语句返回影响行数 /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> int ExcuteNonQuery(string sql, DbParameterCollection parameters = null); /// <summary> /// 执行sql语句返回DataSet /// </summary> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> DataSet ExcuteQuery(string sql, DbParameterCollection parameters = null); /// <summary> /// 执行sql语句返回泛型集合 /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> IList<TResult> ExcuteQuery<TResult>(string sql, DbParameterCollection parameters = null); }
时间: 2024-10-10 17:32:52