1. 程式人生 > >.netCore+Vue 搭建的簡捷開發框架 (3)-- Services層實現

.netCore+Vue 搭建的簡捷開發框架 (3)-- Services層實現

繼續交作業:

上一篇作業中我們實現了 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 = 1, int pageSize = 20)
 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 = 1, int pageSize = 20);
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 }
View Code

接下來是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 }
View Code

這裡需要說明的是,因為我在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 }
View Code

最後就是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 }
View Code

這裡有些地方需要進行一下說明。首先就是通過依賴注入的方式,將IBaseRepository 注入進來。

 

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

 

到此為止,各個層中的實現就都完成了。但是我們在Controller 中該怎麼引用呢?

這個地方涉及的東西比較多,比如NetCore 的依賴注入、利用反射機制進行注入、NetCore 的Startup.cs 類等內容。準備用下一節的內容來進行整體說明。謝謝。

&n