IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> ASP.NET Core 3.1中整体项目搭建轻量级ORM二(异步存储) -> 正文阅读

[开发工具]ASP.NET Core 3.1中整体项目搭建轻量级ORM二(异步存储)

一、异步存储

1.1、基础准备

ASP.NET Core 3.1中整体项目搭建(仓储+服务+抽象接口)https://coffeemilk.blog.csdn.net/article/details/121750796

ASP.NET Core 3.1中整体项目搭建轻量级ORM(SqlSugar ORM)icon-default.png?t=M1L8https://coffeemilk.blog.csdn.net/article/details/121810501?

1.2、异步泛型存储流程

①定义异步泛型IRepository接口;

②接着在Repository中实现;

③然后在IServices层引用这些接口,同时也可以自定义扩展业务逻辑接口;

④最后在Services中实现

二、异步泛型存储基类接口和实现

2.1、设计异步泛型仓储基类接口_IBaseRepository<T>

? ? 这里设计泛型仓储基类接口的目的是在于:方便项目中不同的开发人员能够使用自身熟悉的(仓储操作方法【比如:数据库访问通用类 (ADO.NET)_SqlHelper常见的数据库操作(EF、Dapper、NHibernate等ORM框架)】)实现该接口功能,能够更加的灵活,设计该接口的目的只是规范必须要实现的功能,具体的实体可以使用不同的方法。

该接口添加在【类库(IRepository)下的Base文件夹下的:IBaseRepository.cs】:

?

/***
*	Title:"WebApi" 项目
*		主题:泛型仓储基类接口
*	Description:
*		功能:
*		    1、查询数据
*		    2、添加数据
*		    3、更新数据
*		    4、删除数据
*	Date:2022
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace IRepository.Base
{
    public interface IBaseRepository<TEntity> where TEntity:class
    {
        // 根据ID查询一条实体数据
        Task<TEntity> QueryById(object objId);
        // 根据ID查询一条实体数据(是否缓存)
        Task<TEntity> QueryById(object objId,bool IsUseCache=false);
        //根据ID列表查询对应的所有数据
        Task<List<TEntity>> QueryByIds(object[] listIds);


        //添加实体数据
        Task<int> Add(TEntity model);


        //更新一条实体数据
        Task<bool> Update(TEntity model);
        //根据条件更新一条实体数据
        Task<bool> Update(TEntity entity, string strWhere);
        //更新实体列表数据(根据显示列和忽略列)
        Task<bool> Update(TEntity entity, List<string> listColumns = null,
            List<string> listIgnoreColumns = null, string strWhere = null);


        //根据ID删除一条数据
        Task<bool> DeleteById(object id);
        //根据实体删除一条数据
        Task<bool> Delete(TEntity model);
        // 删除指定ID集合的数据(批量删除)
        Task<bool> DeleteByIds(object[] ids);


        //查询所有数据
        Task<List<TEntity>> Query();
        //根据条件查询数据
        Task<List<TEntity>> Query(string strWhere);
        //根据条件表达式查询数据
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);
        //根据条件表达式查询数据,且指定字段排序
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds);
        //根据条件表达式查询数据,且指定字段排序
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
        //根据条件查询数据,且指定字段排序
        Task<List<TEntity>> Query(string strWhere, string strOrderByFileds);
        //查询前N条数据
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds);
        //查询前N条数据
        Task<List<TEntity>> Query(string strWhere, int intTop, string strOrderByFileds);
        //分页查询
        Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds);
        //分页查询
        Task<List<TEntity>> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds);
        //分页查询
        Task<List<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 0, int intPageSize = 20, string strOrderByFileds = null);

    }//Interface_end
}

2.2、实现异步泛型仓储基类接口_BaseRepository<T>

在类库【Repository】下新建【Base】文件夹创建【BaseRepository.cs】:

?

/***
*	Title:"WebApi" 项目
*		主题:实现异步泛型仓储基类接口
*	Description:
*		功能:
*		    1、查询数据
*		    2、添加数据
*		    3、更新数据
*		    4、删除数据
*	Date:2022
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using IRepository.Base;
using Repository.Sugar;
using SqlSugar;

namespace Repository.Base
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        private DbContext context;
        private SqlSugarClient db;
        private SimpleClient<TEntity> entityDB;

        public DbContext Context
        {
            get { return context; }
            set { context = value; }
        }
        internal SqlSugarClient Db
        {
            get { return db; }
            private set { db = value; }
        }
        internal SimpleClient<TEntity> EntityDB
        {
            get { return entityDB; }
            private set { entityDB = value; }
        }
        public BaseRepository()
        {
            DbContext.Init(BaseDBConfig.connectionString);
            context = DbContext.GetDbContext();
            db = context.Db;
            entityDB = context.GetEntityDB<TEntity>(db);
        }


        /// <summary>
        /// 根据ID查询一条实体数据
        /// </summary>
        /// <param name="objId">Id内容</param>
        /// <returns>返回数据实体</returns>
        public async Task<TEntity> QueryById(object objId)
        {
            return await Task.Run(() => db.Queryable<TEntity>().InSingle(objId));
        }

        /// <summary>
        /// 根据ID查询一条数据
        /// </summary>
        /// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
        /// <param name="IsUseCache">是否使用缓存</param>
        /// <returns>返回数据实体</returns>
        public async Task<TEntity> QueryById(object objId, bool IsUseCache = false)
        {
            return await Task.Run(() => db.Queryable<TEntity>().WithCacheIF(IsUseCache).InSingle(objId));
        }

        /// <summary>
        /// 根据ID查询数据
        /// </summary>
        /// <param name="listIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
        /// <returns>返回数据实体列表</returns>
        public async Task<List<TEntity>> QueryByIds(object[] listIds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().In(listIds).ToList());
        }

        /// <summary>
        /// 添加实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>返回数据库受影响的行数(1表示添加成功)</returns>
        public async Task<int> Add(TEntity entity)
        {
            var i = await Task.Run(() => db.Insertable(entity).ExecuteCommand());
            //返回的i是long类型,这里你可以根据你的业务需要进行处理(比如我这里转为Int)
            return (int)i;
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>返回结果(true:表示更新成功)</returns>
        public async Task<bool> Update(TEntity entity)
        {
            //这种方式会以主键为条件
            var i = await Task.Run(() => db.Updateable(entity).ExecuteCommand());
            return i > 0;
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="strWhere">条件</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Update(TEntity entity, string strWhere)
        {
            return await Task.Run(() => db.Updateable(entity).Where(strWhere).ExecuteCommand() > 0);
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="strSql">更新的sql语句</param>
        /// <param name="parameters">更新使用的参数</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Update(string strSql, SugarParameter[] parameters = null)
        {
            return await Task.Run(() => db.Ado.ExecuteCommand(strSql, parameters) > 0);
        }

        /// <summary>
        /// 更新实体列表数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="listColumns">查询的列表列</param>
        /// <param name="listIgnoreColumns">需要忽略的列表列</param>
        /// <param name="strWhere">条件</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Update(
          TEntity entity,
          List<string> listColumns = null,
          List<string> listIgnoreColumns = null,
          string strWhere = ""
            )
        {
            IUpdateable<TEntity> up = db.Updateable(entity);
            if (listIgnoreColumns != null && listIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(listIgnoreColumns.ToArray());
            }
            if (listColumns != null && listColumns.Count > 0)
            {
                up = up.UpdateColumns(listColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }


        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> DeleteById(object id)
        {
            var i = await Task.Run(() => db.Deleteable<TEntity>(id).ExecuteCommand());
            return i > 0;
        }

        /// <summary>
        /// 根据实体删除一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Delete(TEntity entity)
        {
            var i = await Task.Run(() => db.Deleteable(entity).ExecuteCommand());
            return i > 0;
        }

        /// <summary>
        /// 删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> DeleteByIds(object[] ids)
        {
            var i = await Task.Run(() => db.Deleteable<TEntity>().In(ids).ExecuteCommand());
            return i > 0;
        }



        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>返回所有数据列表</returns>
        public async Task<List<TEntity>> Query()
        {
            return await Task.Run(() => entityDB.GetList());
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>返回条件对应的数据列表</returns>
        public async Task<List<TEntity>> Query(string strWhere)
        {
            return await Task.Run(() => db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
        }

        /// <summary>
        /// 根据条件表达式查询数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>返回条件表达式对应的数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await Task.Run(() => entityDB.GetList(whereExpression));
        }

        /// <summary>
        /// 根据条件表达式查询数据,且指定字段排序
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList());
        }


        /// <summary>
        /// 根据条件表达式查询数据,且指定字段排序
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="isAsc">是否升序(true表示升序)</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList());
        }

        /// <summary>
        /// 根据条件查询数据,且指定字段排序
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(string strWhere, string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList());
        }


        /// <summary>
        /// 查询前N条数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression,
            int intTop,
            string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList());
        }

        /// <summary>
        /// 查询前N条数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
            string strWhere,
            int intTop,
            string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList());
        }



        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression,
            int intPageIndex,
            int intPageSize,
            string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize));
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
          string strWhere,
          int intPageIndex,
          int intPageSize,
          string strOrderByFileds)
        {
            return await Task.Run(() => db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize));
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小(默认20)</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc </param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression,
        int intPageIndex = 0, int intPageSize = 20, string strOrderByFileds = null)
        {
            return await Task.Run(() => db.Queryable<TEntity>()
            .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
            .WhereIF(whereExpression != null, whereExpression)
            .ToPageList(intPageIndex, intPageSize));
        }



    }//Class_end
}

2.3、创建具体的仓储接口和实现

这里以广告仓储接口和实现为例说明:

2.3.1、创建具体的广告接口_IAdvertisementRepositoryAsyn

该接口添加在【类库(IRepository)下的:IAdvertisementRepositoryAsyn.cs】:

?

该广告接口需要继承【异步泛型仓储基类接口(IBaseRepository<Advertisement>)】?

/***
*	Title:"WebApi" 项目
*		主题:异步广告仓储接口
*	Description:
*		功能:
*		    1、求和
*		    2、插入
*		    3、查询
*		    4、更新
*		    5、删除
*	Date:2021
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using IRepository.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Test_NETCoreModel.Models;

namespace IRepository
{
    public interface IAdvertisementRepositoryAsyn:IBaseRepository<Advertisement>
    {
        Task<int> Sum(int i, int j);


    }//Class_end
}

2.3.2、实现具体的广告接口_AdvertisementRepositoryAsyn

在类库【Repository】下新建【AdvertisementRepositoryAsyn.cs】:

该广告接口需要继承【基础仓库类(?BaseRepository<Advertisement>)、广告接口(IAdvertisementRepositoryAsyn)】

/***
*	Title:"WebApi" 项目
*		主题:异步广告仓储接口实现
*	Description:
*		功能:
*           1、求和
*		    2、插入
*		    3、查询
*		    4、更新
*		    5、删除
*	Date:2021
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using IRepository;
using Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Test_NETCoreModel.Models;

namespace Repository
{
    public class AdvertisementRepositoryAsyn : BaseRepository<Advertisement>, IAdvertisementRepositoryAsyn
    {

        async Task<int> IAdvertisementRepositoryAsyn.Sum(int i, int j)
        {
            int result = await Task.Run(() =>
                {
                    System.Threading.Thread.Sleep(3000);
                    return i + j;
                });

            return result;
        }


    }//Class_end
}

?三、异步泛型服务接口和实现

3.1、设计异步泛型服务基类接口_IBaseServices<T>

该接口添加在【类库(IServices)下的Base文件夹下的:IBaseServices.cs】:

?

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace IServices.Base
{
    public interface IBaseServices<TEntity> where TEntity:class
    {
        Task<TEntity> QueryById(object objId);
        Task<TEntity> QueryById(object objId, bool IsUseCache = false);
        Task<List<TEntity>> QueryByIds(object[] listIds);

        Task<int> Add(TEntity model);

        Task<bool> DeleteById(object id);
        Task<bool> Delete(TEntity model);
        Task<bool> DeleteByIds(object[] ids);

        Task<bool> Update(TEntity model);
        Task<bool> Update(TEntity entity, string strWhere);
        Task<bool> Update(TEntity entity, List<string> listColumns = null,
            List<string> listIgnoreColumns = null, string strWhere = null);

        Task<List<TEntity>> Query();
        Task<List<TEntity>> Query(string strWhere);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
        Task<List<TEntity>> Query(string strWhere, string strOrderByFileds);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds);
        Task<List<TEntity>> Query(string strWhere, int intTop, string strOrderByFileds);
        Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds);
        Task<List<TEntity>> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds);
        Task<List<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 0, int intPageSize = 20, string strOrderByFileds = null);


    }//Interface_end
}

3.2、实现异步泛型服务基类接口_BaseServices<T>

该实现添加在【类库(Services)下的Base文件夹下的:IBaseServices.cs】:

/***
*	Title:"WebApi" 项目
*		主题:异步泛型存储服务实现
*	Description:
*		功能:XXX
*	Date:2022
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using IRepository.Base;
using IServices.Base;
using Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Services.Base
{
    public class BaseServices<TEntity> :IBaseServices<TEntity> where TEntity:class,new()
    {
        public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();


        /// <summary>
        /// 根据Id查询一条数据
        /// </summary>
        /// <param name="objId">查询的Id</param>
        /// <returns></returns>
        public async Task<TEntity> QueryById(object objId)
        {
            return await baseDal.QueryById(objId);
        }


        /// <summary>
        /// 根据ID查询一条数据
        /// </summary>
        /// <param name="objId">id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
        /// <param name="IsUseCache">是否使用缓存</param>
        /// <returns>数据实体</returns>
        public async Task<TEntity> QueryById(object objId, bool IsUseCache = false)
        {
            return await baseDal.QueryById(objId, IsUseCache);
        }

        /// <summary>
        /// 根据ID查询数据
        /// </summary>
        /// <param name="lstIds">id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件</param>
        /// <returns>数据实体列表</returns>
        public async Task<List<TEntity>> QueryByIds(object[] listIds)
        {
            return await baseDal.QueryByIds(listIds);
        }

        /// <summary>
        /// 添加实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>返回数据库受影响的行数(1表示添加成功)</returns>
        public async Task<int> Add(TEntity entity)
        {
            return await baseDal.Add(entity);
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<bool> Update(TEntity entity)
        {
            return await baseDal.Update(entity);
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="strWhere">条件</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Update(TEntity entity, string strWhere)
        {
            return await baseDal.Update(entity, strWhere);
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="listColumns">查询的列表列</param>
        /// <param name="listIgnoreColumns">需要忽略的列表列</param>
        /// <param name="strWhere">条件</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Update(
         TEntity entity,
         List<string> listColumns = null,
         List<string> listIgnoreColumns = null,
         string strWhere = ""
            )
        {
            return await baseDal.Update(entity, listColumns, listIgnoreColumns, strWhere);
        }


        /// <summary>
        /// 根据实体删除一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> Delete(TEntity entity)
        {
            return await baseDal.Delete(entity);
        }

        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> DeleteById(object id)
        {
            return await baseDal.DeleteById(id);
        }

        /// <summary>
        /// 删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns>返回结果(true:表示成功)</returns>
        public async Task<bool> DeleteByIds(object[] ids)
        {
            return await baseDal.DeleteByIds(ids);
        }



        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>返回所有数据列表</returns>
        public async Task<List<TEntity>> Query()
        {
            return await baseDal.Query();
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>返回条件对应的数据列表</returns>
        public async Task<List<TEntity>> Query(string strWhere)
        {
            return await baseDal.Query(strWhere);
        }

        /// <summary>
        /// 根据条件查询数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <returns>返回条件表达式对应的数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await baseDal.Query(whereExpression);
        }

        /// <summary>
        /// 根据条件表达式查询数据,且指定字段排序
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await baseDal.Query(whereExpression, strOrderByFileds);
        }

        /// <summary>
        /// 根据条件表达式查询数据,且指定字段排序
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderByExpression">排序表达式</param>
        /// <param name="isAsc">是否升序(true表示升序)</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await baseDal.Query(whereExpression, orderByExpression, isAsc);
        }



        /// <summary>
        /// 根据条件查询数据,且指定字段排序
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(string strWhere, string strOrderByFileds)
        {
            return await baseDal.Query(strWhere, strOrderByFileds);
        }

        /// <summary>
        /// 查询前N条数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return await baseDal.Query(whereExpression, intTop, strOrderByFileds);
        }

        /// <summary>
        /// 查询前N条数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
            string strWhere,
            int intTop,
            string strOrderByFileds)
        {
            return await baseDal.Query(strWhere, intTop, strOrderByFileds);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression,
            int intPageIndex,
            int intPageSize,
            string strOrderByFileds)
        {
            return await baseDal.Query(
              whereExpression,
              intPageIndex,
              intPageSize,
              strOrderByFileds);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc</param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> Query(
          string strWhere,
          int intPageIndex,
          int intPageSize,
          string strOrderByFileds)
        {
            return await baseDal.Query(
            strWhere,
            intPageIndex,
            intPageSize,
            strOrderByFileds);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码(下标0)</param>
        /// <param name="intPageSize">页大小(默认20)</param>
        /// <param name="strOrderByFileds">排序字段和关键字【排序字段 排序关键字】,如:age desc </param>
        /// <returns>返回对应的实体数据列表</returns>
        public async Task<List<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression,
        int intPageIndex = 0, int intPageSize = 20, string strOrderByFileds = null)
        {
            return await baseDal.QueryPage(whereExpression,
         intPageIndex = 0, intPageSize, strOrderByFileds);
        }


    }//Class_end

}

3.3、创建具体的服务接口和实现

这里以广告服务接口和实现为例说明:

3.3.1、创建具体的服务接口_IAdvertisementAsynServices

该广告接口服务添加在【类库(IServices)下是:IAdvertisementAsynServices.cs】:

该广告接口服务需要继承【异步泛型服务基类接口(IBaseServices<Advertisement>)】?

using IServices.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Test_NETCoreModel.Models;

namespace IServices
{
    public interface IAdvertisementAsynServices: IBaseServices<Advertisement>
    {

        Task<int> Sum(int i, int j);
        //其他的业务方法内容

    }//Class
}

3.3.2、实现具体的服务接口_AdvertisementAsynServices

该广告实现服务添加在【类库(Services)下是:AdvertisementAsynServices.cs】:

?该广告实现服务需要继承【异步泛型服务实现(BaseServices<Advertisement>)、广告服务接口IAdvertisementAsynServices】?

/***
*	Title:"XXX" 项目
*		主题:
*	Description:
*		功能:
*	Date:2021
*	Version:0.1版本
*	Author:Coffee
*	Modify Recoder:
*/

using IRepository;
using IServices;
using Repository;
using Services.Base;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Test_NETCoreModel.Models;

namespace Services
{ 
    public class AdvertisementAsynServices : BaseServices<Advertisement>, IAdvertisementAsynServices
    {
        IAdvertisementRepositoryAsyn dal = new AdvertisementRepositoryAsyn();


        Task<int> IAdvertisementAsynServices.Sum(int i, int j)
        {
            //处理业务逻辑内容
            return dal.Sum(i, j);
        }

        //其他业务方法内容


    }//Class_end
}

四、编写广告接口

在Api项目工程的Controllers文件夹下创建广告的Api接口(AdvertisementAsynController.cs):?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IServices;
using Microsoft.AspNetCore.Mvc;
using Services;
using Test_NETCoreModel.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Test_NETCoreWebAPI.Controllers
{
    /// <summary>
    /// 异步广告接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AdvertisementAsynController : ControllerBase
    {
        IAdvertisementAsynServices advertisementServices = new AdvertisementAsynServices();

        /// <summary>
        /// 异步广告的Sum方法
        /// </summary>
        /// <param name="i">参数i</param>
        /// <param name="j">参数j</param>
        /// <returns></returns>
        [HttpGet("{i},{j}")]
        public async Task<int> GetSum(int i, int j)
        {
            return await advertisementServices.Sum(i, j);
        }


        /// <summary>
        /// 异步添加广告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddInfo(Advertisement model)
        {
            return await advertisementServices.Add(model);
        }


        /// <summary>
        /// 异步根据ID查询信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Advertisement>> GetInfoById(int id)
        {
            return await advertisementServices.Query(d => d.Id == id);
        }

        ///<summary>
        /// 异步更新广告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateInfo(Advertisement model)
        {
            return await advertisementServices.Update(model);
        }

        /// <summary>
        /// 异步删除指定广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<bool> DeleteInfo(int id)
        {
            return await advertisementServices.DeleteById(id);
        }

    }
}

五、运行项目进行测试Api接口

这里以【异步添加广告】为例进行测试,如下表示添加广告成功:

登陆数据库进行验证:可以看到已经将我们输入的信息成功添加到数据库中

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-03-10 22:48:06  更:2022-03-10 22:49:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 6:23:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码