(一)前言
Asp.Net Core 项目中的“服务”,指的是用于扩展应用程序功能的一系列类型。在程序初始化期间,会把需要的服务类型实例添加到ServiceCollection集合中,这些服务实例将通过依赖注入提供给其他代码使用。
Service结构设计:其实服务和仓储的结构是一致的,主要是为了比较好区分。我之前见过又一个奇葩写的,仓储层写用文件夹弄的挺工整的,服务就一个文件夹都没有,找基类都找不到他的。
各文件夹介绍:这个就免了吧,和仓储的结构是一致的。我见给的大部分的项目都是一致的,就又一些特殊的不一致而已。(我这是指的是结构上哈)
(二)Service服务创建
1、创建仓储的项目,创建文件夹这些我就不截图了。
2、引用Entity类库,Repository服务层就行了。
3、基类(BaseService、IBaseService)代码。
BaseService类
public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
{
//从子类(仓储中)的构造函授中传入
protected IBaseRepository<TEntity> _IbaseRepository;
/// <summary>
/// 添加
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<bool> CreateAsync(TEntity entity)
{
return await _IbaseRepository.CreateAsync(entity);
}
/// <summary>
/// 根据ID删除
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public async Task<bool> DeleteAsync(int ID)
{
return await _IbaseRepository.DeleteAsync(ID);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<bool> EditAsync(TEntity entity)
{
return await _IbaseRepository.EditAsync(entity);
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public async Task<TEntity> FindAsync(int Id)
{
//用户数据,不可能返回到前端去的;(我的总结是查询都用上virtual;virtual代表的是虚方法)
return await _IbaseRepository.FindAsync(Id);
}
/// <summary>
/// 根据条件查询接口
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func)
{
return await _IbaseRepository.FindAsync(func);
}
/// <summary>
/// 查询全部数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public async Task<List<TEntity>> QueryAsync()
{
return await _IbaseRepository.QueryAsync();
}
/// <summary>
/// 自定义条件查询
/// </summary>
/// <returns></returns>
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func)
{
return await _IbaseRepository.QueryAsync(func);
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="page"></param>
/// <param name="size"></param>
/// <param name="total"></param>
/// <returns></returns>
public async Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total)
{
return await _IbaseRepository.QueryAsync(page, size, total);
}
/// <summary>
/// 自定义条件分页查询
/// </summary>
/// <param name="func"></param>
/// <param name="page"></param>
/// <param name="size"></param>
/// <param name="total"></param>
/// <returns></returns>
public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total)
{
return await _IbaseRepository.QueryAsync(func, page, size, total);
}
}
IBaseService类
public interface IBaseService<TEntity> where TEntity:class,new()
{
/// <summary>
/// 添加
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
Task<bool> CreateAsync(TEntity entity);
/// <summary>
/// 根据ID删除
/// </summary>
/// <param name="ID"></param>
/// <returns></returns>
Task<bool> DeleteAsync(int ID);
/// <summary>
/// 修改
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
Task<bool> EditAsync(TEntity entity);
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
Task<TEntity> FindAsync(int Id);
/// <summary>
/// 根据条件查询接口
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func);
/// <summary>
/// 查询全部数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync();
/// <summary>
/// 自定义条件查询
/// </summary>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func);
/// <summary>
/// 分页查询
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total);
/// <summary>
/// 自定义条件分页查询
/// </summary>
/// <param name="func"></param>
/// <param name="page"></param>
/// <param name="size"></param>
/// <param name="total"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total);
}
4、BusinessService:
BlogNewsService类
public class BlogNewsService : BaseService<Entity.BaseEntity.BlogNews>, IBlogNewsService
{
private readonly IBlogNewsRepository _iblogNewsRepository;
public BlogNewsService(IBlogNewsRepository iblogNewsRepository)
{
base._IbaseRepository = iblogNewsRepository;
_iblogNewsRepository = iblogNewsRepository;
}
}
IBlogNewsService类
public interface IBlogNewsService:IBaseService<Entity.BaseEntity.BlogNews>
{
}
TypeInfoService,WriterInfoService也是一样的,修改一继承的类就行了。
ITypeInfoService,IWriterInfoService:都是继承基类的接口。