当前位置:   article > 正文

(六)Service(服务层)

服务层

(一)前言

Asp.Net Core 项目中的“服务”,指的是用于扩展应用程序功能的一系列类型。在程序初始化期间,会把需要的服务类型实例添加到ServiceCollection集合中,这些服务实例将通过依赖注入提供给其他代码使用。

Service结构设计:其实服务和仓储的结构是一致的,主要是为了比较好区分。我之前见过又一个奇葩写的,仓储层写用文件夹弄的挺工整的,服务就一个文件夹都没有,找基类都找不到他的。

各文件夹介绍:这个就免了吧,和仓储的结构是一致的。我见给的大部分的项目都是一致的,就又一些特殊的不一致而已。(我这是指的是结构上哈) 

(二)Service服务创建

1、创建仓储的项目,创建文件夹这些我就不截图了。

2、引用Entity类库,Repository服务层就行了。

3、基类(BaseService、IBaseService)代码。

BaseService类

  1. public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
  2. {
  3. //从子类(仓储中)的构造函授中传入
  4. protected IBaseRepository<TEntity> _IbaseRepository;
  5. /// <summary>
  6. /// 添加
  7. /// </summary>
  8. /// <param name="entity"></param>
  9. /// <returns></returns>
  10. public async Task<bool> CreateAsync(TEntity entity)
  11. {
  12. return await _IbaseRepository.CreateAsync(entity);
  13. }
  14. /// <summary>
  15. /// 根据ID删除
  16. /// </summary>
  17. /// <param name="Id"></param>
  18. /// <returns></returns>
  19. public async Task<bool> DeleteAsync(int ID)
  20. {
  21. return await _IbaseRepository.DeleteAsync(ID);
  22. }
  23. /// <summary>
  24. /// 修改
  25. /// </summary>
  26. /// <param name="entity"></param>
  27. /// <returns></returns>
  28. public async Task<bool> EditAsync(TEntity entity)
  29. {
  30. return await _IbaseRepository.EditAsync(entity);
  31. }
  32. /// <summary>
  33. /// 根据ID查询
  34. /// </summary>
  35. /// <param name="Id"></param>
  36. /// <returns></returns>
  37. public async Task<TEntity> FindAsync(int Id)
  38. {
  39. //用户数据,不可能返回到前端去的;(我的总结是查询都用上virtual;virtual代表的是虚方法)
  40. return await _IbaseRepository.FindAsync(Id);
  41. }
  42. /// <summary>
  43. /// 根据条件查询接口
  44. /// </summary>
  45. /// <param name="func"></param>
  46. /// <returns></returns>
  47. public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func)
  48. {
  49. return await _IbaseRepository.FindAsync(func);
  50. }
  51. /// <summary>
  52. /// 查询全部数据
  53. /// </summary>
  54. /// <param name="entity"></param>
  55. /// <returns></returns>
  56. public async Task<List<TEntity>> QueryAsync()
  57. {
  58. return await _IbaseRepository.QueryAsync();
  59. }
  60. /// <summary>
  61. /// 自定义条件查询
  62. /// </summary>
  63. /// <returns></returns>
  64. public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func)
  65. {
  66. return await _IbaseRepository.QueryAsync(func);
  67. }
  68. /// <summary>
  69. /// 分页查询
  70. /// </summary>
  71. /// <param name="page"></param>
  72. /// <param name="size"></param>
  73. /// <param name="total"></param>
  74. /// <returns></returns>
  75. public async Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total)
  76. {
  77. return await _IbaseRepository.QueryAsync(page, size, total);
  78. }
  79. /// <summary>
  80. /// 自定义条件分页查询
  81. /// </summary>
  82. /// <param name="func"></param>
  83. /// <param name="page"></param>
  84. /// <param name="size"></param>
  85. /// <param name="total"></param>
  86. /// <returns></returns>
  87. public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total)
  88. {
  89. return await _IbaseRepository.QueryAsync(func, page, size, total);
  90. }
  91. }

IBaseService类

  1. public interface IBaseService<TEntity> where TEntity:class,new()
  2. {
  3. /// <summary>
  4. /// 添加
  5. /// </summary>
  6. /// <param name="entity"></param>
  7. /// <returns></returns>
  8. Task<bool> CreateAsync(TEntity entity);
  9. /// <summary>
  10. /// 根据ID删除
  11. /// </summary>
  12. /// <param name="ID"></param>
  13. /// <returns></returns>
  14. Task<bool> DeleteAsync(int ID);
  15. /// <summary>
  16. /// 修改
  17. /// </summary>
  18. /// <param name="entity"></param>
  19. /// <returns></returns>
  20. Task<bool> EditAsync(TEntity entity);
  21. /// <summary>
  22. /// 根据ID查询
  23. /// </summary>
  24. /// <param name="Id"></param>
  25. /// <returns></returns>
  26. Task<TEntity> FindAsync(int Id);
  27. /// <summary>
  28. /// 根据条件查询接口
  29. /// </summary>
  30. /// <param name="entity"></param>
  31. /// <returns></returns>
  32. Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func);
  33. /// <summary>
  34. /// 查询全部数据
  35. /// </summary>
  36. /// <param name="entity"></param>
  37. /// <returns></returns>
  38. Task<List<TEntity>> QueryAsync();
  39. /// <summary>
  40. /// 自定义条件查询
  41. /// </summary>
  42. /// <returns></returns>
  43. Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func);
  44. /// <summary>
  45. /// 分页查询
  46. /// </summary>
  47. /// <param name="func"></param>
  48. /// <returns></returns>
  49. Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total);
  50. /// <summary>
  51. /// 自定义条件分页查询
  52. /// </summary>
  53. /// <param name="func"></param>
  54. /// <param name="page"></param>
  55. /// <param name="size"></param>
  56. /// <param name="total"></param>
  57. /// <returns></returns>
  58. Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total);
  59. }

4、BusinessService:

BlogNewsService类

  1. public class BlogNewsService : BaseService<Entity.BaseEntity.BlogNews>, IBlogNewsService
  2. {
  3. private readonly IBlogNewsRepository _iblogNewsRepository;
  4. public BlogNewsService(IBlogNewsRepository iblogNewsRepository)
  5. {
  6. base._IbaseRepository = iblogNewsRepository;
  7. _iblogNewsRepository = iblogNewsRepository;
  8. }
  9. }

IBlogNewsService类

  1. public interface IBlogNewsService:IBaseService<Entity.BaseEntity.BlogNews>
  2. {
  3. }

TypeInfoService,WriterInfoService也是一样的,修改一继承的类就行了。

ITypeInfoService,IWriterInfoService:都是继承基类的接口。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/392975
推荐阅读
相关标签
  

闽ICP备14008679号