在2.3中完成依賴註入後,這次主要實現欄目的添加功能。按照前面思路欄目有三種類型,常規欄目即可以添加子欄目也可以選擇是否添加內容,內容又可以分文章或其他類型,所以還要添加一個模塊功能。這次主要實現欄目的添加,附帶實現模塊列表功能,並將業務邏輯層的功能都實現了非同步方法。 先來個完成後的界面吧。 一、業... ...
在2.3中完成依賴註入後,這次主要實現欄目的添加功能。按照前面思路欄目有三種類型,常規欄目即可以添加子欄目也可以選擇是否添加內容,內容又可以分文章或其他類型,所以還要添加一個模塊功能。這次主要實現欄目的添加,附帶實現模塊列表功能,並將業務邏輯層的功能都實現了非同步方法。
先來個完成後的界面吧。
一、業務邏輯層非同步方法
.net Core中非同步方法很簡單,只需要Task、async、await三個關鍵字就行。比如要實現統計記錄數非同步方法,只要給方法添加關鍵字async,然後返回Task類型,併在方法中使用await調用非同步方法就可以。
public async Task<int> CountAsync(Expression<Func<T, bool>> predicate) { return await _dbContext.Set<T>().CountAsync(predicate); }
一般在非同步方法中也是調用非同步方法,如果調用同步方法,如果直接調用同步方法也沒問題,但是編譯器會顯示警告,看著警告煩可以Task.FromResult,像下麵這樣子。
public virtual async Task<IQueryable<T>> FindListAsync() { IQueryable<T> result = _dbContext.Set<T>(); return await Task.FromResult(result); }
基本內容都差不多,下麵直接貼代碼。
1、非同步介面
在Ninesky.InterfaceBase中打開InterfaceBaseService文件。添加介面非同步的方法,結果如下:
using Ninesky.Models; using System; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace Ninesky.InterfaceBase { /// <summary> /// 服務基礎介面 /// </summary> public interface InterfaceBaseService<T> where T:class { /// <summary> /// 添加 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> int Add(T entity, bool isSave = true); /// <summary> /// 添加 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> Task<int> AddAsync(T entity, bool isSave = true); /// <summary> /// 添加[批量] /// </summary> /// <param name="entities">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數</returns> int AddRange(T[] entities, bool isSave = true); /// <summary> /// 添加[批量] /// </summary> /// <param name="entities">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> Task<int> AddRangeAsync(T[] entities, bool isSave = true); /// <summary> /// 查詢記錄數 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>記錄數</returns> int Count(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢記錄數 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>記錄數</returns> Task<int> CountAsync(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢是否存在 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>是否存在</returns> bool Exists(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢是否存在 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>是否存在</returns> Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate); /// <summary> /// 查找 /// </summary> /// <param name="Id">主鍵</param> /// <returns></returns> T Find(int Id); /// <summary> /// 查找 /// </summary> /// <param name="Id">主鍵</param> /// <returns></returns> Task<T> FindAsync(int Id); /// <summary> /// 查找 /// </summary> /// <param name="keyValues">主鍵</param> /// <returns></returns> T Find(object[] keyValues); /// <summary> /// 查找 /// </summary> /// <param name="keyValues">主鍵</param> /// <returns></returns> Task<T> FindAsync(object[] keyValues); /// <summary> /// 查找 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns></returns> T Find(Expression<Func<T, bool>> predicate); /// <summary> /// 查找 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns></returns> Task<T> FindAsync(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢 /// </summary> /// <returns>實體列表</returns> IQueryable<T> FindList(); /// <summary> /// 查詢 /// </summary> /// <returns>實體列表</returns> Task<IQueryable<T>> FindListAsync(); /// <summary> /// 查詢 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>實體列表</returns> IQueryable<T> FindList(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>實體列表</returns> Task<IQueryable<T>> FindListAsync(Expression<Func<T, bool>> predicate); /// <summary> /// 查詢 /// </summary> /// <param name="number">返回記錄數</param> /// <param name="predicate">查詢條件</param> /// <returns>實體列表</returns> IQueryable<T> FindList(int number, Expression<Func<T, bool>> predicate); /// <summary> /// 查詢 /// </summary> /// <param name="number">返回記錄數</param> /// <param name="predicate">查詢條件</param> /// <returns>實體列表</returns> Task<IQueryable<T>> FindListAsync(int number, Expression<Func<T, bool>> predicate); /// <summary> /// 查詢 /// </summary> /// <param name="number">顯示數量[小於等於0-不啟用]</param> /// <typeparam name="TKey">排序欄位</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">正序</param> /// <returns></returns> IQueryable<T> FindList<TKey>(int number, Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc); /// <summary> /// 查詢 /// </summary> /// <param name="number">顯示數量[小於等於0-不啟用]</param> /// <typeparam name="TKey">排序欄位</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">正序</param> /// <returns></returns> Task<IQueryable<T>> FindListAsync<TKey>(int number, Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc); /// <summary> /// 查詢[分頁] /// </summary> /// <typeparam name="TKey">排序屬性</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">是否正序</param> /// <param name="paging">分頁數據</param> /// <returns></returns> Paging<T> FindList<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc, Paging<T> paging); /// <summary> /// 查詢[分頁] /// </summary> /// <typeparam name="TKey">排序屬性</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">是否正序</param> /// <param name="paging">分頁數據</param> /// <returns></returns> Task<Paging<T>> FindListAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc, Paging<T> paging); /// <summary> /// 查詢[分頁] /// </summary> /// <typeparam name="TKey">排序屬性</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">是否正序</param> /// <param name="pageIndex">當前頁</param> /// <param name="pageSize">每頁記錄數</param> /// <returns></returns> Paging<T> FindList<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc, int pageIndex, int pageSize); /// <summary> /// 查詢[分頁] /// </summary> /// <typeparam name="TKey">排序屬性</typeparam> /// <param name="predicate">查詢條件</param> /// <param name="keySelector">排序</param> /// <param name="isAsc">是否正序</param> /// <param name="pageIndex">當前頁</param> /// <param name="pageSize">每頁記錄數</param> /// <returns></returns> Task<Paging<T>> FindListAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector, bool isAsc, int pageIndex, int pageSize); /// <summary> /// 刪除 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>是否刪除成功[isSave=true時有效]</returns> bool Remove(T entity, bool isSave = true); /// <summary> /// 刪除 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>是否刪除成功[isSave=true時有效]</returns> Task<bool> RemoveAsync(T entity, bool isSave = true); /// <summary> /// 刪除[批量] /// </summary> /// <param name="entities">實體數組</param> /// <param name="isSave">是否立即保存</param> /// <returns>成功刪除的記錄數[isSave=true時有效]</returns> int RemoveRange(T[] entities, bool isSave = true); /// <summary> /// 刪除[批量] /// </summary> /// <param name="entities">實體數組</param> /// <param name="isSave">是否立即保存</param> /// <returns>成功刪除的記錄數[isSave=true時有效]</returns> Task<int> RemoveRangeAsync(T[] entities, bool isSave = true); /// <summary> /// 保存數據 /// </summary> /// <returns>更改的記錄數</returns> int SaveChanges(); /// <summary> /// 保存數據 /// </summary> /// <returns>更改的記錄數</returns> Task<int> SaveChangesAsync(); /// <summary> /// 更新 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>是否保存成功[isSave=true時有效]</returns> bool Update(T entity, bool isSave = true); /// <summary> /// 更新 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>是否保存成功[isSave=true時有效]</returns> Task<bool> UpdateAsync(T entity, bool isSave = true); /// <summary> /// 更新[批量] /// </summary> /// <param name="entities">實體數組</param> /// <param name="isSave">是否立即保存</param> /// <returns>更新成功的記錄數[isSave=true時有效]</returns> int UpdateRange(T[] entities, bool isSave = true); /// <summary> /// 更新[批量] /// </summary> /// <param name="entities">實體數組</param> /// <param name="isSave">是否立即保存</param> /// <returns>更新成功的記錄數[isSave=true時有效]</returns> Task<int> UpdateRangeAsync(T[] entities, bool isSave = true); } }
2、非同步介面的實現
在Ninesky.Base中打開BaseService 添加非同步介面的實現,代碼如下
using Microsoft.EntityFrameworkCore; using Ninesky.InterfaceBase; using Ninesky.Models; using System; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace Ninesky.Base { /// <summary> /// 服務基類 /// </summary> public class BaseService<T>:InterfaceBaseService<T> where T:class { protected DbContext _dbContext; public BaseService(DbContext dbContext) { _dbContext = dbContext; } /// <summary> /// 添加 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> public virtual int Add(T entity, bool isSave = true) { _dbContext.Set<T>().Add(entity); if (isSave) return _dbContext.SaveChanges(); else return 0; } /// <summary> /// 添加 /// </summary> /// <param name="entity">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> public virtual async Task<int> AddAsync(T entity, bool isSave = true) { await _dbContext.Set<T>().AddAsync(entity); if (isSave) return await _dbContext.SaveChangesAsync(); else return 0; } /// <summary> /// 添加[批量] /// </summary> /// <param name="entities">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> public virtual int AddRange(T[] entities, bool isSave = true) { _dbContext.Set<T>().AddRange(entities); if (isSave) return _dbContext.SaveChanges(); else return 0; } /// <summary> /// 添加[批量] /// </summary> /// <param name="entities">實體</param> /// <param name="isSave">是否立即保存</param> /// <returns>添加的記錄數[isSave=true時有效]</returns> public virtual async Task<int> AddRangeAsync(T[] entities, bool isSave = true) { await _dbContext.Set<T>().AddRangeAsync(entities); if (isSave) return await _dbContext.SaveChangesAsync(); else return 0; } /// <summary> /// 查詢記錄數 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>記錄數</returns> public virtual int Count(Expression<Func<T, bool>> predicate) { return _dbContext.Set<T>().Count(predicate); } /// <summary> /// 查詢記錄數 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>記錄數</returns> public virtual async Task<int> CountAsync(Expression<Func<T, bool>> predicate) { return await _dbContext.Set<T>().CountAsync(predicate); } /// <summary> /// 查詢是否存在 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>是否存在</returns> public virtual bool Exists(Expression<Func<T, bool>> predicate) { return _dbContext.Set<T>().Any(predicate); } /// <summary> /// 查詢是否存在 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns>是否存在</returns> public virtual async Task<bool> ExistsAsync(Expression<Func<T, bool>> predicate) { return await _dbContext.Set<T>().AnyAsync(predicate); } /// <summary> /// 查找 /// </summary> /// <param name="Id">主鍵</param> /// <returns></returns> public virtual T Find(int Id) { return _dbContext.Set<T>().Find(Id); } /// <summary> /// 查找 /// </summary> /// <param name="Id">主鍵</param> /// <returns></returns> public virtual async Task<T> FindAsync(int Id) { return await _dbContext.Set<T>().FindAsync(Id); } /// <summary> /// 查找 /// </summary> /// <param name="keyValues">主鍵</param> /// <returns></returns> public virtual T Find(object[] keyValues) { return _dbContext.Set<T>().Find(keyValues); } /// <summary> /// 查找 /// </summary> /// <param name="keyValues">主鍵</param> /// <returns></returns> public virtual async Task<T> FindAsync(object[] keyValues) { return await _dbContext.Set<T>().FindAsync(keyValues); } public virtual T Find(Expression<Func<T, bool>> predicate) { return _dbContext.Set<T>().SingleOrDefault(predicate); } /// <summary> /// 查找 /// </summary> /// <param name="predicate">查詢條件</param> /// <returns></returns> public virtual async Task<T> FindAsync(Expression<Func<T, bool>> predicate) { return await _dbContext.Set<T>().SingleOrDefaultAsync(predicate); } /// <summary> /// 查詢 /// </summary> /// <returns></returns> public virtual IQueryable<T> FindList() { return _dbContext.Set<T>(); } /// <summary> /// 查詢 /// </summary> /// <returns>實體列表</returns> public virtual async Task<IQueryable<T>> FindListAsync() { IQueryable<T> result = _dbContext.Set<