欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)

程序员文章站 2022-04-09 21:33:13
常规的中小型项目搭建方式一般是三层架构加上mvc与webapi作为一个主要框架,再加上一些第三方库,例如orm框架(EF、SqlSugar、Dapper等),API文档工具(Swagger)这些的应用。 接下来我们以一个数据管理平台做为实战项目来演示三层架构与仓储模式。 项目Github连接: 一、 ......

  常规的中小型项目搭建方式一般是三层架构加上mvc与webapi作为一个主要框架,再加上一些第三方库,例如orm框架(ef、sqlsugar、dapper等),api文档工具(swagger)这些的应用。

  接下来我们以一个数据管理平台做为实战项目来演示三层架构与仓储模式。

  项目github连接:

https://github.com/faithguo/datamanager

 

  一、项目创建

  首先搭建以下目录结构

asp.net core 一个中小型项目实战的起手式——项目搭建与仓储模式下的持久层创建(1)

 

1. 数据持久接口:idal

2.数据持久实现:dal

3.业务逻辑接口:ibll

4.业务逻辑实现:bll

5.业务规则接口:ibussness

6.业务规则实现:bussness

7.数据实体:entity

8.模型:model

9.公共工具:common

10.页面ui:web

其中我将常规三层的业务逻辑层分为了业务逻辑与业务规则两部分,这样分层的优点是,便于后期维护;缺点是工作量的增加。

二、第三方库的加入

项目搭建完了自然就是加入一些第三方框架。

这里我选择的orm框架为sqlsugar:

    分别在common、entity、idal与dal项目依赖项中右键-管理nuget程序包-搜索sqlsugarcore-安装(此处为5.0.0.9版本)

    这个项目中我将sqlsugar数据库链接类放入到common中,以便未来其他项目中可以直接复用common项目,所以才在common中引入sqlsugarcore。

    首先在common中新建basedbcontext类:

using system;
using system.collections.generic;
using system.configuration;
using system.text;
using microsoft.extensions.configuration;
using sqlsugar;

namespace datamanager.common.db
{
    /// <summary>
    /// 数据库操作基类
    /// </summary>
    public class basedbcontext
    {
        public sqlsugarclient db;

        /// <summary>
        /// 构造函数
        /// </summary>
        public basedbcontext(iconfiguration configuration)
        {
            try
            {
                //主库
                var connmain = configurationmanager.appsettings["connmain"];
                //从库
                var connfrom = configurationmanager.appsettings["connfrom"];
                initdatabase(connfrom == null
                    ? new list<string> {connmain.tostring()}
                    : new list<string> {connmain.tostring(), connfrom.tostring()});
            }
            catch (exception ex)
            {
                throw new exception("未配置数据库连接字符串");
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="listconnsettings">
        /// 连接字符串配置key集合,配置多个连接则是读写分离 
        /// </param>
        public basedbcontext(list<string> listconnsettings)
        {
            try
            {
                var listconn = new list<string>();
                foreach (var t in listconnsettings)
                {
                    listconn.add(configurationmanager.connectionstrings[t].tostring());
                }

                initdatabase(listconn);
            }
            catch
            {
                throw new exception("未配置数据库连接字符串");
            }

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serverip">服务器ip</param>
        /// <param name="user">用户名</param>
        /// <param name="pass">密码</param>
        /// <param name="database">数据库</param>
        public basedbcontext(string serverip, string user, string pass, string database)
        {
            initdatabase(new list<string>
                {$"server={serverip};user id={user};password={pass};persistsecurityinfo=true;database={database}"});
        }

        /// <summary>
        /// 初始化数据库连接
        /// </summary>
        /// <param name="listconn">连接字符串</param>
        private void initdatabase(list<string> listconn)
        {
            var connstr = ""; //主库
            var slaveconnectionconfigs = new list<slaveconnectionconfig>(); //从库集合
            for (var i = 0; i < listconn.count; i++)
            {
                if (i == 0)
                {
                    connstr = listconn[i]; //主数据库连接
                }
                else
                {
                    slaveconnectionconfigs.add(new slaveconnectionconfig()
                    {
                        hitrate = i * 2,
                        connectionstring = listconn[i]
                    });
                }
            }

            //如果配置了 slaveconnectionconfigs那就是主从模式,所有的写入删除更新都走主库,查询走从库,
            //事务内都走主库,hitrate表示权重 值越大执行的次数越高,如果想停掉哪个连接可以把hitrate设为0
            var ctx = new configureexternalservices();

            db = new sqlsugarclient(new connectionconfig()
            {
                connectionstring = connstr,
                dbtype = dbtype.sqlserver,
                isautocloseconnection = true,
                slaveconnectionconfigs = slaveconnectionconfigs
            });
            db.ado.commandtimeout = 30000; //设置超时时间
            db.aop.onlogexecuted = (sql, pars) => //sql执行完事件
            {
            };
            db.aop.onlogexecuting = (sql, pars) => //sql执行前事件
            {
            };
            db.aop.onerror = (exp) => //执行sql 错误事件
            {
                throw new exception("出错sql:" + exp.sql + "\r\n" + exp.message);
            };
            db.aop.onexecutingchangesql = (sql, pars) => //sql执行前 可以修改sql
            {
                return new keyvaluepair<string, sugarparameter[]>(sql, pars);
            };
        }

        public sqlsugarclient getclient() => db;
    }
}

    查询排序条件类:

using system;
using system.collections.generic;
using system.text;
using sqlsugar;

namespace datamanager.common
{
    /// <summary>
    /// 排序类型
    /// </summary>
    public enum ordersequence
    {
        asc,
        desc
    }

    /// <summary>
    /// 排序枚举
    /// </summary>
    public class orderbyclause
    {
        public string sort { get; set; }
        public ordersequence order { get; set; }
    }

    /// <summary>
    /// 查询条件
    /// </summary>
    public class querydescriptor
    {
        /// <summary>
        /// 行数
        /// </summary>
        public int pagesize { get; set; }

        /// <summary>
        /// 页码
        /// </summary>
        public int pageindex { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public list<orderbyclause> orderbys { get; set; }

        /// <summary>
        /// 条件
        /// </summary>
        public list<conditionalmodel> conditions { get; set; }
    }
}

    在idal中新建仓储基类接口ibaserepository:

using system;
using system.collections.generic;
using system.data;
using system.linq.expressions;
using datamanager.common;
using sqlsugar;

namespace datamanager.idal
{
    public interface ibaserepository<t> : idisposable where t : class, new()
    {
        #region 事务
        /// <summary>
        /// 初始化事务
        /// </summary>
        /// <param name="level"></param>
        void begintran(isolationlevel level = isolationlevel.readcommitted);

        /// <summary>
        /// 完成事务
        /// </summary>
        void committran();

        /// <summary>
        /// 完成事务
        /// </summary>
        void rollbacktran();
        #endregion

        #region 新增 
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>操作影响的行数</returns>
        int add<t>(t entity, bool islock = false) where t : class, new();

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="islock">是否加锁</param>
        /// <returns>操作影响的行数</returns>
        int add<t>(list<t> entitys, bool islock = false) where t : class, new();

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>返回实体</returns>
        t addreturnentity<t>(t entity, bool islock = false) where t : class, new();
        /// <summary>
        /// 新增
        /// </summary> 
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>返回bool, 并将identity赋值到实体</returns>
        bool addreturnbool<t>(t entity, bool islock = false) where t : class, new();

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="islock">是否加锁</param>
        /// <returns>返回bool, 并将identity赋值到实体</returns>
        bool addreturnbool<t>(list<t> entitys, bool islock = false) where t : class, new();
        #endregion

        #region 修改 

        /// <summary>
        /// 修改数据源
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <returns>数据源</returns>
        iupdateable<t> updateable<t>() where t : class, new();

        /// <summary>
        /// 修改(主键是更新条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int update<t>(t entity, bool islock = false) where t : class, new();

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="update"> 字段集合 </param> 
        /// <param name="where"> 条件 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int update<t>(expression<func<t, t>> update, expression<func<t, bool>> where, bool islock = false) where t : class, new();

        /// <summary>
        /// 修改(主键是更新条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int update<t>(list<t> entitys, bool islock = false) where t : class, new();
        #endregion

        #region 删除


        /// <summary>
        /// 删除(主键是条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int delete<t>(t entity, bool islock = false) where t : class, new();

        /// <summary>
        /// 删除(主键是条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="where"> 条件 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        int delete<t>(expression<func<t, bool>> where, bool islock = false) where t : class, new();
        #endregion

        #region 查询 

        /// <summary>
        /// 查询数据源
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <returns>数据源</returns>
        isugarqueryable<t> queryable<t>() where t : class, new();

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns></returns>
        t query<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>实体</returns>
        list<t> querylist<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="sql">sql</param>
        /// <returns>实体</returns>
        list<t> querylist<t>(string sql) where t : class, new();

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>实体</returns>
        datatable querydatatable<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="sql">sql</param>
        /// <returns>实体</returns>
        datatable querydatatable<t>(string sql) where t : class, new();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="totalcount">总行数</param>
        /// <returns>实体</returns>
        list<t> querypagelist<t>(querydescriptor query, out int totalcount) where t : class, new();

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="totalcount">总行数</param>
        /// <returns>实体</returns>
        datatable querydatatablepagelist<t>(querydescriptor query, out int totalcount) where t : class, new();

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>json</returns>
        string queryjson<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 查询存储过程
        /// </summary>
        /// <param name="procedurename">存储过程名称</param>
        /// <param name="parameters">参数</param>
        datatable queryprocedure(string procedurename, list<sugarparameter> parameters);

        /// <summary>
        /// 查询前多少条数据
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <param name="num">数量</param>
        /// <returns></returns>
        list<t> take<t>(expression<func<t, bool>> wherelambda, int num) where t : class, new();

        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param> 
        /// <returns></returns>
        t first<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param> 
        /// <returns></returns>
        bool isexist<t>(expression<func<t, bool>> wherelambda) where t : class, new();

        /// <summary>
        /// 合计
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        int sum<t>(string field) where t : class, new();

        /// <summary>
        /// 最大值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        object max<t>(string field) where t : class, new();

        /// <summary>
        /// 最小值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        object min<t>(string field) where t : class, new();

        /// <summary>
        /// 平均值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        int avg<t>(string field) where t : class, new();

        #endregion
    }
}

    在dal中新建仓储基类的实现,在sqlsugar框架中,作者直接将sqlsugarclient默认单例,所以此处不用再做处理可以直接在仓储基类中继承basedbcontext方便直接操作数据库:

using system;
using system.collections.generic;
using system.data;
using system.linq.expressions;
using system.text;
using datamanager.common;
using datamanager.common.db;
using datamanager.idal;
using microsoft.extensions.configuration;
using sqlsugar;

namespace datamanager.dal
{
    public class baseservice<t> : basedbcontext, ibaserepository<t> where t : class, new()
    {
        public sqlsugarclient dbcontext;

        public baseservice(iconfiguration configuration) : base(configuration)
        {
            dbcontext = getclient();
        }


        #region 事务

        /// <summary>
        /// 初始化事务
        /// </summary>
        /// <param name="level"></param>
        public void begintran(isolationlevel level = isolationlevel.readcommitted)
        {
            dbcontext.ado.begintran(isolationlevel.unspecified);
        }

        /// <summary>
        /// 完成事务
        /// </summary>
        public void committran()
        {
            dbcontext.ado.committran();
        }

        /// <summary>
        /// 完成事务
        /// </summary>
        public void rollbacktran()
        {
            dbcontext.ado.rollbacktran();
        }

        #endregion

        #region 新增 

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>操作影响的行数</returns>
        public int add<t>(t entity, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.insertable(entity).with(sqlwith.updlock).executecommand()
                    : dbcontext.insertable(entity).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="islock">是否加锁</param>
        /// <returns>操作影响的行数</returns>
        public int add<t>(list<t> entitys, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.insertable(entitys).with(sqlwith.updlock).executecommand()
                    : dbcontext.insertable(entitys).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>返回实体</returns>
        public t addreturnentity<t>(t entity, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.insertable(entity).with(sqlwith.updlock).executereturnentity()
                    : dbcontext.insertable(entity).executereturnentity();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        /// <summary>
        /// 新增
        /// </summary> 
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock">是否加锁</param>
        /// <returns>返回bool, 并将identity赋值到实体</returns>
        public bool addreturnbool<t>(t entity, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.insertable(entity).with(sqlwith.updlock).executecommandidentityintoentity()
                    : dbcontext.insertable(entity).executecommandidentityintoentity();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys">泛型集合</param>
        /// <param name="islock">是否加锁</param>
        /// <returns>返回bool, 并将identity赋值到实体</returns>
        public bool addreturnbool<t>(list<t> entitys, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.insertable(entitys).with(sqlwith.updlock).executecommandidentityintoentity()
                    : dbcontext.insertable(entitys).executecommandidentityintoentity();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        #endregion

        #region 修改 

        /// <summary>
        /// 修改数据源
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <returns>数据源</returns>
        public iupdateable<t> updateable<t>() where t : class, new()
        {
            return dbcontext.updateable<t>();
        }

        /// <summary>
        /// 修改(主键是更新条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        public int update<t>(t entity, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.updateable(entity).with(sqlwith.updlock).executecommand()
                    : dbcontext.updateable(entity).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="update"> 实体对象 </param> 
        /// <param name="where"> 条件 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        public int update<t>(expression<func<t, t>> update, expression<func<t, bool>> where, bool islock = false)
            where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.updateable<t>().setcolumns(update).where(where).with(sqlwith.updlock)
                        .executecommand()
                    : dbcontext.updateable<t>().setcolumns(update).where(where).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        /// <summary>
        /// 修改(主键是更新条件)
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entitys"> 实体对象集合 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        public int update<t>(list<t> entitys, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.updateable(entitys).with(sqlwith.updlock).executecommand()
                    : dbcontext.updateable(entitys).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        #endregion

        #region 删除


        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="entity"> 实体对象 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        public int delete<t>(t entity, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.deleteable(entity).with(sqlwith.updlock).executecommand()
                    : dbcontext.deleteable(entity).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="where"> 条件 </param> 
        /// <param name="islock"> 是否加锁 </param> 
        /// <returns>操作影响的行数</returns>
        public int delete<t>(expression<func<t, bool>> where, bool islock = false) where t : class, new()
        {
            try
            {
                var result = islock
                    ? dbcontext.deleteable<t>().where(where).with(sqlwith.updlock).executecommand()
                    : dbcontext.deleteable<t>().where(where).executecommand();
                return result;
            }
            catch (exception ex)
            {
                rollbacktran();
                throw new exception(ex.message);
            }

        }

        #endregion

        #region 查询

        /// <summary>
        /// 查询数据源
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <returns>数据源</returns>
        public isugarqueryable<t> queryable<t>() where t : class, new()
        {
            return dbcontext.queryable<t>();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns></returns>
        public t query<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            return dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).first();
            var str = dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).tosql();
            console.writeline(str);
            return null;
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>实体</returns>
        public list<t> querylist<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            return dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).tolist();
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="sql">sql</param>
        /// <returns>实体</returns>
        public list<t> querylist<t>(string sql) where t : class, new()
        {
            return dbcontext.sqlqueryable<t>(sql).with(sqlwith.nolock).tolist();
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>实体</returns>
        public datatable querydatatable<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            return dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).todatatable();
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="sql">sql</param>
        /// <returns>实体</returns>
        public datatable querydatatable<t>(string sql) where t : class, new()
        {
            return dbcontext.sqlqueryable<t>(sql).with(sqlwith.nolock).todatatable();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="totalcount">总行数</param>
        /// <returns>实体</returns>
        public list<t> querypagelist<t>(querydescriptor query, out int totalcount) where t : class, new()
        {
            if (query == null)
            {
                throw new argumentnullexception(nameof(query));
            }

            var listdatas = dbcontext.queryable<t>();
            if (query.conditions != null)
            {
                var conds = parsecondition(query.conditions);
                listdatas = listdatas.where(conds);
            }

            if (query.orderbys != null)
            {
                var orderbys = parseorderby(query.orderbys);
                listdatas = listdatas.orderby(orderbys);
            }

            totalcount = 0;
            var datas = listdatas.topagelist(query.pageindex, query.pagesize, ref totalcount);
            return datas;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="query">查询条件</param>
        /// <param name="totalcount">总行数</param>
        /// <returns>datatable</returns>
        public datatable querydatatablepagelist<t>(querydescriptor query, out int totalcount) where t : class, new()
        {
            if (query == null)
            {
                throw new argumentnullexception(nameof(query));
            }

            var listdatas = dbcontext.queryable<t>();
            if (query.conditions != null)
            {
                var conds = parsecondition(query.conditions);
                listdatas = listdatas.where(conds);
            }

            if (query.orderbys != null)
            {
                var orderbys = parseorderby(query.orderbys);
                listdatas = listdatas.orderby(orderbys);
            }

            totalcount = 0;
            var datas = listdatas.todatatablepage(query.pageindex, query.pagesize, ref totalcount);
            return datas;
        }

        /// <summary>
        /// 查询集合
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型)</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <returns>json</returns>
        public string queryjson<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            return dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).tojson();
        }

        /// <summary>
        /// 查询存储过程
        /// </summary> 
        /// <param name="procedurename">存储过程名称</param>
        /// <param name="parameters">参数</param>re
        public datatable queryprocedure(string procedurename, list<sugarparameter> parameters)
        {
            var datas = dbcontext.ado.usestoredprocedure().getdatatable(procedurename, parameters);
            return datas;
        }

        /// <summary>
        /// 查询前多少条数据
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param>
        /// <param name="num">数量</param>
        /// <returns></returns>
        public list<t> take<t>(expression<func<t, bool>> wherelambda, int num) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).take(num).tolist();
            return datas;
        }

        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param> 
        /// <returns></returns>
        public t first<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().with(sqlwith.nolock).where(wherelambda).first();
            return datas;
        }

        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="wherelambda">查询表达式</param> 
        /// <returns></returns>
        public bool isexist<t>(expression<func<t, bool>> wherelambda) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().any(wherelambda);
            return datas;
        }

        /// <summary>
        /// 合计
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        public int sum<t>(string field) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().sum<int>(field);
            return datas;
        }

        /// <summary>
        /// 最大值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        public object max<t>(string field) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().max<object>(field);
            return datas;
        }

        /// <summary>
        /// 最小值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        public object min<t>(string field) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().min<object>(field);
            return datas;
        }

        /// <summary>
        /// 平均值
        /// </summary>
        /// <typeparam name="t">泛型参数(集合成员的类型</typeparam>
        /// <param name="field">字段</param> 
        /// <returns></returns>
        public int avg<t>(string field) where t : class, new()
        {
            var datas = dbcontext.queryable<t>().avg<int>(field);
            return datas;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 查询条件转换
        /// </summary>
        /// <param name="contitons">查询条件</param>
        /// <returns></returns>
        private list<iconditionalmodel> parsecondition(list<conditionalmodel> contitons)
        {
            var conds = new list<iconditionalmodel>();
            foreach (var con in contitons)
            {
                if (con.fieldname.contains(","))
                {
                    conds.add(parsekeyor(con));
                }
                else
                {
                    conds.add(new conditionalmodel()
                    {
                        fieldname = con.fieldname,
                        conditionaltype = con.conditionaltype,
                        fieldvalue = con.fieldvalue
                    });
                }
            }

            return conds;
        }

        /// <summary>
        /// 转换or条件
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private conditionalcollections parsekeyor(conditionalmodel condition)
        {
            var objectkeys = condition.fieldname.split(',');
            var conditionallist = new list<keyvaluepair<wheretype, conditionalmodel>>();
            foreach (var objkey in objectkeys)
            {
                var cond = new keyvaluepair<wheretype, conditionalmodel>
                (wheretype.or, new conditionalmodel()
                {
                    fieldname = objkey,
                    conditionaltype = condition.conditionaltype,
                    fieldvalue = condition.fieldvalue
                });
                conditionallist.add(cond);
            }

            return new conditionalcollections { conditionallist = conditionallist };
        }


        /// <summary>
        /// 排序转换
        /// </summary>
        /// <param name="orderbys">排序</param>
        /// <returns></returns>
        private string parseorderby(list<orderbyclause> orderbys)
        {
            var conds = "";
            foreach (var con in orderbys)
            {
                if (con.order == ordersequence.asc)
                {
                    conds += $"{con.sort} asc,";
                }
                else if (con.order == ordersequence.desc)
                {
                    conds += $"{con.sort} desc,";
                }
            }

            return conds.trimend(',');
        }

        #endregion

        /// <summary>
        /// 处理
        /// </summary>
        public void dispose()
        {
            rollbacktran();
            dbcontext.close();

        }
    }
}

    至此sqlsugar前期工作完成,后期新增的实体新建对应的仓库,直接继承baserepository。下一篇我会将swagger加入到项目中。