天天看點

【Abp VNext】實戰入門(十三):【1】領域層 —— 添加自定義倉儲服務一、前言二、自定義倉儲步驟三、總結

文章目錄

  • 一、前言
  • 二、自定義倉儲步驟
    • 1、在Domain領域層添加自定義倉儲接口申明
    • 2、在EntityFrameworkCore基礎設施層實作接口服務
    • 3、在Application應用層 注入并使用服務
  • 三、總結

一、前言

項目中通常習慣性的針對每一個實體在應用層建立繼承自現有的CrudAppService服務,來獲得标配的增删改查接口功能,但有的時候需要用自定義倉儲服務來個性化的資料操作需求;

如:我們需要在商品Goods表中牛奶Index=2後面插入一條香蕉記錄Index=3,所有商品記錄通過Index字段由大到小排序,這時候需要先将比牛奶Index=2大的所有記錄Index+1,然後插入Index=2+1的香蕉記錄;

這時候就可以為Goods商品添加自定義倉儲事件來實作在把指定某件Id商品 後的所有Index+1,然後按正常操作新增記錄;

二、自定義倉儲步驟

1、在Domain領域層添加自定義倉儲接口申明

//IPresetCruise_PointsRepository.cs   
    public interface IPresetCruise_PointsRepository : IRepository<PresetCruise_Points, int>
    {       
        Task IndexIncreaseAfterIdAsync(int id);
    }
           
【Abp VNext】實戰入門(十三):【1】領域層 —— 添加自定義倉儲服務一、前言二、自定義倉儲步驟三、總結

2、在EntityFrameworkCore基礎設施層實作接口服務

/// <summary>
    /// 預置點自定義倉儲類
    /// </summary>
    public class PresetCruise_PointsRepository : EfCoreRepository<GasMonitoringDbContext, PresetCruise_Points, int>, IPresetCruise_PointsRepository
    {
        public PresetCruise_PointsRepository(IDbContextProvider<GasMonitoringDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }


        /// <summary>
        /// 自動批量+1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       public async Task IndexIncreaseAfterIdAsync(int id)
        {
            var tmpEntity = this.DbContext.PresetCruise_Points.FirstOrDefault(p => p.Id == id);
            if (tmpEntity == null)
            {
                return;
            }
			//把後面所有記錄Index批量+1 ,注意這地方使用了Z.EntityFramework.Plus開源元件 
            await this.DbContext.PresetCruise_Points.Where(p => p.Index > tmpEntity.Index)
                  .UpdateAsync(p => new PresetCruise_Points() { Index = p.Index + 1 });
        }
    }
           
【Abp VNext】實戰入門(十三):【1】領域層 —— 添加自定義倉儲服務一、前言二、自定義倉儲步驟三、總結

3、在Application應用層 注入并使用服務

public class PresetCruise_PointAppService : CrudAppService<PresetCruise_Points, PresetCruisePointDto, int, PagedResultRequestDto, CreatePresetCruisePointDto, PresetCruisePointDto>, IPresetCruise_PointAppService
    {

        /// <summary>
        /// 構造函數注入預置點位自定義倉儲
        /// </summary>
        private readonly IPresetCruise_PointsRepository _presetCruise_PointsRepository;

        public PresetCruise_PointAppService(IRepository<PresetCruise_Points, int> repository, IPresetCruise_PointsRepository presetCruise_PointsRepository) : base(repository)
        {
            _presetCruise_PointsRepository = presetCruise_PointsRepository;
        }

        /// <summary>
        /// 比自定記錄Id大的所有記錄 對應的排序Index +1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task IndexIncreaseAfterIdAsync(int id)
        {
            await _presetCruise_PointsRepository.IndexIncreaseAfterIdAsync(id);
        }
}
           
【Abp VNext】實戰入門(十三):【1】領域層 —— 添加自定義倉儲服務一、前言二、自定義倉儲步驟三、總結

三、總結

這裡隻是單純示範了自定義倉儲的實作過程,至于什麼内容應該放在倉儲服務中,什麼内容應該放在領域服務中,什麼内容應該放在應用層服務中 是個來回拉鋸的問題,有一定的标準,也看個人如何了解;

下一節講解如何批量操作表記錄;

繼續閱讀