继续交作业:

上一篇作业中我们实现了 Repository仓储层的应用。并为我们的框架引入了EFCore 详见:

.netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用

接下来我们继续来实现Services 层,同样,我们在Services 和IServices 中增加 Base 文件夹。并创建BaseServices.cs 和IBaseServices.cs

如图:

  

其中BaseServices.cs 和 IBaseServices.cs 代码如下:

  1. using Sincere.Core.IRepository.Base;
  2. using Sincere.Core.IServices.Base;
  3. using Sincere.Core.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Linq.Expressions;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace Sincere.Core.Services.Base
  13. {
  14. public class BaseServices<TEntity> : IBaseServices<TEntity> where TEntity : class, new()
  15. {
  16. //public IBaseRepository<TEntity> baseDal = new BaseRepository<TEntity>();
  17. public IBaseRepository<TEntity> BaseDal;//通过在子类的构造函数中注入,这里是基类,不用构造函数
  18.  
  19. public async Task<int> Del(TEntity model)
  20. {
  21. return await BaseDal.Del(model);
  22. }
  23.  
  24. public async Task<int> DelBy(Expression<Func<TEntity, bool>> delWhere)
  25. {
  26. return await BaseDal.DelBy(delWhere);
  27. }
  28.  
  29. public async Task<int> Execute(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text)
  30. {
  31. return await BaseDal.Execute(sql,parms,cmdType);
  32. }
  33.  
  34. public async Task<List<TEntity>> GetList()
  35. {
  36. return await BaseDal.GetList();
  37. }
  38.  
  39. public async Task<List<TEntity>> GetListBy(Expression<Func<TEntity, bool>> whereLambda)
  40. {
  41. return await BaseDal.GetListBy(whereLambda);
  42. }
  43.  
  44. public async Task<List<TEntity>> GetListBy<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true)
  45. {
  46. return await BaseDal.GetListBy(whereLambda,orderLambda,isAsc);
  47. }
  48.  
  49. public async Task<List<TEntity>> GetListBy<TKey>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true)
  50. {
  51. return await BaseDal.GetListBy(top, whereLambda, orderLambda, isAsc);
  52. }
  53.  
  54. public async Task<List<TEntity>> GetListBy<TKey1, TKey2>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true)
  55. {
  56. return await BaseDal.GetListBy( whereLambda, orderLambda1,orderLambda2, isAsc1,isAsc2);
  57. }
  58.  
  59. public async Task<List<TEntity>> GetListBy<TKey1, TKey2>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true)
  60. {
  61. return await BaseDal.GetListBy(top, whereLambda, orderLambda1, orderLambda2, isAsc1, isAsc2);
  62. }
  63.  
  64. public async Task<TEntity> GetModelById(Expression<Func<TEntity, bool>> whereLambda)
  65. {
  66. return await BaseDal.GetModelById( whereLambda);
  67. }
  68.  
  69. public async Task<List<TEntity>> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true)
  70. {
  71. return await BaseDal.GetPagedList(pageIndex,pageSize, whereLambda,orderByLambda,isAsc);
  72. }
  73.  
  74. public async Task<PageModel<TEntity>> GetPagedList<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true, int pageIndex = , int pageSize = )
  75. {
  76. return await BaseDal.GetPagedList( whereLambda, orderByLambda, isAsc, pageIndex, pageSize);
  77. }
  78.  
  79. public async Task<bool> Insert(TEntity model)
  80. {
  81. return await BaseDal.Insert(model);
  82. }
  83.  
  84. public async Task<bool> InsertRange(List<TEntity> datas)
  85. {
  86. return await BaseDal.InsertRange(datas);
  87. }
  88.  
  89. public async Task<int> Modify(TEntity model)
  90. {
  91. return await BaseDal.Modify(model);
  92. }
  93.  
  94. public async Task<int> Modify(TEntity model, params string[] propertyNames)
  95. {
  96. return await BaseDal.Modify(model,propertyNames);
  97. }
  98.  
  99. public async Task<int> ModifyBy(TEntity model, Expression<Func<TEntity, bool>> whereLambda, params string[] modifiedPropertyNames)
  100. {
  101. return await BaseDal.ModifyBy(model, whereLambda,modifiedPropertyNames);
  102. }
  103.  
  104. public async Task<List<TEntity>> Query(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text)
  105. {
  106. return await BaseDal.Query(sql, parms, cmdType);
  107. }
  108.  
  109. public void RollBackChanges()
  110. {
  111. BaseDal.RollBackChanges();
  112. }
  113. }
  114.  
  115. }

BaseServices

  1. using Sincere.Core.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq.Expressions;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Sincere.Core.IServices.Base
  11. {
  12. public interface IBaseServices<TEntity> where TEntity : class
  13. {
  14. Task<int> Execute(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text);
  15. Task<List<TEntity>> Query(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text);
  16. Task<bool> Insert(TEntity model);
  17. Task<bool> InsertRange(List<TEntity> datas);
  18.  
  19. Task<int> Del(TEntity model);
  20.  
  21. Task<int> DelBy(Expression<Func<TEntity, bool>> delWhere);
  22.  
  23. Task<int> Modify(TEntity model);
  24.  
  25. Task<int> Modify(TEntity model, params string[] propertyNames);
  26.  
  27. Task<int> ModifyBy(TEntity model, Expression<Func<TEntity, bool>> whereLambda, params string[] modifiedPropertyNames);
  28.  
  29. Task<List<TEntity>> GetList();
  30.  
  31. Task<List<TEntity>> GetListBy(Expression<Func<TEntity, bool>> whereLambda);
  32.  
  33. Task<TEntity> GetModelById(Expression<Func<TEntity, bool>> whereLambda);
  34.  
  35. Task<List<TEntity>> GetListBy<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true);
  36.  
  37. Task<List<TEntity>> GetListBy<TKey>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true);
  38.  
  39. Task<List<TEntity>> GetListBy<TKey1, TKey2>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true);
  40.  
  41. Task<List<TEntity>> GetListBy<TKey1, TKey2>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true);
  42.  
  43. Task<List<TEntity>> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true);
  44.  
  45. Task<PageModel<TEntity>> GetPagedList<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true, int pageIndex = , int pageSize = );
  46.  
  47. void RollBackChanges();
  48. }
  49.  
  50. }

IBaseServices.cs

需要说明的是,在BaseServices.cs 中我们通过依赖注入的方式引入了IBaseRepository 。

具体的引用需要在子类的构造函数中注入。

结合上一篇中的内容,我们现在的框架就已经搭建好了最基础的框架模型中的Services 和 Repository 层。

为了验证各层之间的调用。接下来我们依旧参照 张老师的课程中使用的Advertisement 类,来进行测试。

我们需要依次建立 Advertisement.cs (Model,已经在上一节中建好) 、IAdvertisementRepository(IRepository 中),AdvertisementRepository(Repository 中)、IAdvertisementServices(IServices中)、AdvertisementServices(Services中)

建好后的代码结构如下:

以后我们在此框架上开发其他服务的时候,也参照这样的目录结构建立相应文件。

接下来依次介绍各个文件,以及其实现过程。

首先是IAdvertisementRepository.cs

这个没什么好说的,继承自 IBaseRepository<Advertisement>。这里面我没有写其他的业务逻辑,是一个空的接口。代码如下:

  1. using Sincere.Core.IRepository.Base;
  2. using Sincere.Core.Model.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace Sincere.Core.IRepository
  8. {
  9. public interface IAdvertisementRepository : IBaseRepository<Advertisement>
  10. {
  11.  
  12. }
  13. }

接下来是AdvertisementRepository.cs ,这里继承自BaseRepository<Advertisement>, 并实现 IAdvertisementRepository接口。代码如下:

  1. using Sincere.Core.IRepository;
  2. using Sincere.Core.Model.EFCore;
  3. using Sincere.Core.Model.Models;
  4. using Sincere.Core.Repository.Base;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8.  
  9. namespace Sincere.Core.Repository
  10. {
  11. public class AdvertisementRepository : BaseRepository<Advertisement>, IAdvertisementRepository
  12. {
  13. public AdvertisementRepository(IBaseContext mydbcontext) : base(mydbcontext)
  14. {
  15.  
  16. }
  17. }
  18. }

这里需要说明的是,因为我在BaseRepository 的构造函数中,使用了有参数的构造函数

public BaseRepository(IBaseContext mydbcontext)
{
this._db = mydbcontext as BaseCoreContext;
this._dbSet = _db.Set<TEntity>();
}

所以在AdvertisementRepository  中,将注入的mydbContext 同步注入到BaseRepository 中。

仓储层的逻辑基本就这样了,接下来写Services中的实现。

同样,首先是接口IAdvertisementServices,继承IBaseServices<Advertisement>。没什么好说的,这里为了假装有一个方法,叫做ReadAllAd(),代码如下:

  1. using Sincere.Core.IServices.Base;
  2. using Sincere.Core.Model.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace Sincere.Core.IServices
  8. {
  9. public interface IAdvertisementServices : IBaseServices<Advertisement>
  10. {
  11. void ReadAllAd();
  12. }
  13. }

最后就是AdvertisementServices了,未来的开发中,我们大部分的业务逻辑都将在这里实现,于仓储层的实现类似,也是继承: BaseServices<Advertisement>, 实现 IAdvertisementServices接口。

代码如下:

  1. using Sincere.Core.IRepository;
  2. using Sincere.Core.IRepository.Base;
  3. using Sincere.Core.IServices;
  4. using Sincere.Core.Model.Models;
  5. using Sincere.Core.Services.Base;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9.  
  10. namespace Sincere.Core.Services
  11. {
  12. public class AdvertisementServices : BaseServices<Advertisement>, IAdvertisementServices
  13. {
  14. IAdvertisementRepository _advertisementRepository;
  15. public AdvertisementServices(IBaseRepository<Advertisement> baseRepository) {
  16. base.BaseDal = baseRepository;
  17. _advertisementRepository = baseRepository as IAdvertisementRepository;
  18. }
  19. public void ReadAllAd() {
  20.  
  21. }
  22. }
  23. }

这里有些地方需要进行一下说明。首先就是通过依赖注入的方式,将IBaseRepository 注入进来。

在ReadAllAd 方法中的引用如上图所示。

到此为止,各个层中的实现就都完成了。但是我们在Controller 中该怎么引用呢?

这个地方涉及的东西比较多,比如NetCore 的依赖注入、利用反射机制进行注入、NetCore 的Startup.cs 类等内容。准备用下一节的内容来进行整体说明。谢谢。

谢谢。

.netCore+Vue 搭建的简捷开发框架 (3)-- Services层实现的更多相关文章

  1. .netCore+Vue 搭建的简捷开发框架 (5)

    文章目录:.netCore+Vue 搭建的简捷开发框架--目录 上两节的内容介绍了一些关于.netCore 相关的一些基础知识.介绍这些的目的,最主要的还是为了我们的架构搭建服务. 上一节中,我们介绍 ...

  2. .netCore+Vue 搭建的简捷开发框架--目录

    .netCore+Vue 搭建的简捷开发框架 .netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用 .netCore+Vue 搭建的简捷开发框架 (3)-- Ser ...

  3. .netCore+Vue 搭建的简捷开发框架

    话不多说,上图: 整体项目结构如图所示,我的设计初衷是基于.netCore + DI + Vue 打造一个适合初学者的简捷开发框架. 架构模型采用基于RESTful API风格的前后台分离框架,总体分 ...

  4. .netCore+Vue 搭建的简捷开发框架 (4)--NetCore 基础

    书接上文:上一节中,我们已经实现Services 层.(https://www.cnblogs.com/xuzhencheng/p/11424751.html) 但是具体要如何将服务依赖注入进来呢?继 ...

  5. .netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用

    书接上文,继续搭建我们基于.netCore 的开发框架.首先是我们的项目分层结构. 这个分层结构,是参考张老师的分层结构,但是实际项目中,我没有去实现仓储模型.因为我使用的是EFCore ,最近也一直 ...

  6. .netCore+Vue 搭建的简捷开发框架 (4)--NetCore 基础 -2

    上节中,我们初步的介绍了一下NetCore的一些基础知识,为了控制篇幅(其实也是因为偷懒),我将NetCore 基础分为两部分来写. 0.WebAPI 项目的建立 1..NetCore 项目执行(加载 ...

  7. .netcore+vue+elementUI 前后端分离---支持前端、后台业务代码扩展的快速开发框架

    框架采用.NetCore + Vue前后端分离,并且支持前端.后台代码业务动态扩展,框架内置了一套有着20多种属性配置的代码生成器,可灵活配置生成的代码,代码生成器界面配置完成即可生成单表(主表)的增 ...

  8. Linux+.NetCore+Nginx搭建集群

    本篇和大家分享的是Linux+NetCore+Nginx搭建负载集群,对于netcore2.0发布后,我一直在看官网的文档并学习,关注有哪些新增的东西,我,一个从1.0到2.0的跟随者这里只总结一句话 ...

  9. 01 基于umi搭建React快速开发框架

    介绍 基于umi搭建一个快速开发框架,react 应用框架.umi 以路由为基础的,支持类 next.js 的约定式路由,以及各种进阶的路由功能,并以此进行功能扩展,比如支持路由级的按需加载. 我们会 ...

随机推荐

  1. python3虚拟环境中解决 ModuleNotFoundError: No module named '_ssl'

    前提是已经安装了openssl 问题 当我在python3虚拟环境中导入ssl模块时报错,报错如下: (py3) [root@localhost Python-3.6.3]# python3 Pyth ...

  2. Selenium + python 测试环境搭建

    Windows平台(py3 已经自带了工具包) 准备工具: python.setuptools(python工具基础包).pip(python安装包管理工具) 安装步骤: 1.python安装,运行e ...

  3. Python程序包的构建和发布过程

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  4. Requests+正则表达式爬取猫眼电影(TOP100榜)

    猫眼电影网址:www.maoyan.com 前言:网上一些大神已经对猫眼电影进行过爬取,所用的方法也是各有其优,最终目的是把影片排名.图片.名称.主要演员.上映时间与评分提取出来并保存到文件或者数据库 ...

  5. python 34 多进程(二)

    目录 1. 互斥锁 2. 进程之间的通信 2.1 基于文件的通信 2.2 基于队列的通信 1. 互斥锁 ​ 当多个进程抢占同一数据时,将数据加锁,使进程按串行的方式去获取数据,先到先得,保证了公平.数 ...

  6. springboot之additional-spring-configuration-metadata.json自定义提示

    springboot之additional-spring-configuration-metadata.json自定义提示 简介 additional-spring-configuration-met ...

  7. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  8. XHTML 和 HTML 中的 iframe

    1. XHTML 有什么? XHTML是更严谨更纯净的HTML版本. 2.HTML和XHTML之间的差异 ①XHTML元素必须被正确的嵌套 /!--错误写法--/ <p><i> ...

  9. 2018宁夏邀请赛K题Vertex Covers(高维前缀和 状压 折半

    https://vjudge.net/problem/Gym-102222K 题意:给定N点M边的无向图,每个点有点权.  点覆盖表示某个点集S{}覆盖了所有的边,其贡献是S中点权之积. 现在让你求所 ...

  10. CodeForces 1082 F Speed Dial

    题目传送门 题意:现在有n个电话号码,每个电话号码为si,拨打次数为pi. 现在有k 个快捷键,每次拨打号码之前可以先按一次快捷键,然后再输入数字,现在问输入数字次数是多少.快捷键的号码可以不在电话簿 ...