abp(net core)+easyui+efcore實現倉儲管理系統——ABP整體介紹(一)html
abp(net core)+easyui+efcore實現倉儲管理系統——解決方案介紹(二)數據庫
abp(net core)+easyui+efcore實現倉儲管理系統——領域層建立實體(三)架構
在上一篇文章中咱們建立了Module實體並在數據庫中生成了數據表,在這一文章中咱們介紹如何來對數據庫進行操做。框架
1、先來介紹下倉儲異步
倉儲(Repository): 倉儲用來操做數據庫進行數據存取。倉儲接口在領域層定義,而倉儲的實現類應該寫在基礎設施層。ide
在ABP中,倉儲類要實現IRepository接口,接口定義了經常使用的增刪改查以及聚合方法,其中包括同步及異步方法。主要包括如下方法:函數
ABP針對不一樣的ORM框架對這個接口進行了默認的實現:post
1) 對於EntityFrameworkCore,提供了EfCoreRepositoryBase<TDbContext, TEntity, TPrimaryKey>的泛型版本的實現方式。
2) 對於NHibernate,提供了NhRepositoryBase<TEntity, TPrimaryKey>的泛型版本的實現方式。ui
這些使用泛型的實現類,在大多數的時候,這些實現類中的方法已經足夠應付通常增刪改查的須要。若是默認實現類中的方法對於實體類來講已夠用了,咱們便不須要再去建立這個實體所需的倉儲接口與實現類。直接經過在應用服務層定義倉儲引用,而後經過構造函數注入便可。spa
接下來,咱們來說講如何自定義倉儲實現類。
2、如何實現自定義倉儲
1. 在Visual Studio 2017的「解決方案資源管理器」中,右鍵單擊在領域層「ABP.TPLMS.Core」項目。 選擇「添加」 > 「新建文件夾」。
2.將文件夾命名爲「IRepositories」。
3. 右鍵單擊「IRepositories」文件夾,而後選擇「添加」 > 「類」。 在彈出對話框「添加新項- ABP.TPLMS.Core」中選擇接口,將接口命名爲 IModuleRepository。而後使用鼠標左鍵單擊「添加」按鈕。以下圖。
4. IModuleRepository接口的代碼以下。
using Abp.Domain.Repositories; using ABP.TPLMS.Entitys; using System; using System.Collections.Generic; using System.Text; namespace ABP.TPLMS.IRepositories { interface IModuleRepository: IRepository<Module> { /// <summary> /// 分頁查詢功能模塊 /// </summary> /// <param name="pageindex">頁索引</param> /// <param name="pagesize">每頁多少條</param> /// <returns>模塊列表</returns> IEnumerable<Module> LoadModules(int pageindex, int pagesize); /// <summary> /// 批量刪除 /// </summary> /// <param name="ids"></param> /// <returns></returns> bool Delete(string ids); } }
1. 在Visual Studio 2017的「解決方案資源管理器」中,打開「ABP.TPLMS.EntityFrameworkCore」項目,找到「Repositories」目錄,在這個目錄中有一個ABP生成的基類。以下圖。
2. 鼠標右鍵單擊「Repositories」文件夾,而後選擇「添加」 > 「類」。 在彈出對話框「添加新項- ABP.TPLMS.EntityFrameworkCore」中類命名爲 ModuleRepository。而後使用鼠標左鍵單擊「添加」按鈕。代碼以下。
using Abp.EntityFrameworkCore; using ABP.TPLMS.Entitys; using ABP.TPLMS.IRepositories; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ABP.TPLMS.EntityFrameworkCore.Repositories { public class ModuleRepository:TPLMSRepositoryBase<Module>, IModuleRepository { public ModuleRepository(IDbContextProvider<TPLMSDbContext> dbContextProvider) : base(dbContextProvider) { } public bool Delete(string ids) { var idList = ids.Split(','); Expression<Func<Module, bool>> exp = m => idList.Contains(m.Id.ToString()); bool result = true; Delete(exp); return result; } public IEnumerable<Module> LoadModules(int pageindex, int pagesize) { return Context.Modules.OrderBy(u => u.Id).Skip((pageindex - 1) * pagesize).Take(pagesize); } } }
該倉儲實現,繼承自模板生成的TPLMSRepositoryBase泛型抽象類,而後再實現IModuleRepository接口。這裏要顯示聲明實現類的有參構造函數,使用泛型的IDbContextProvider將數據庫上下文的子類ChargeStationContext傳給父類的構造函數。以下圖。
4、倉儲的注意事項