本文主要是介绍ABP框架—后台:仓储Repository(8),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在DDD领域驱动设计中,仓储实现了对数据进行增删改查操作的代码。
(1)ABP里面已经为我们定义了默认仓储,里面已经包含基本的仓储方法(增删改查)。我们可以直接调用。
(2)当ABP的默认仓储不能满足业务时,我们可以在默认仓储上进行扩展。继承默认仓储接口IRepository,和继承默认仓储实现类PDRepositoryBase来进行扩展。
(3)如果在默认仓储上进行扩展,我要把仓储接口和仓储实现分离。仓储接口应放置于领域层(Core),仓储实现应该放置于基础设施层(EntityFrameworkCore)
一、默认仓储
1.默认仓储接口IRepository
在命名namespace空间下 Abp.Domain.Repositories,默认仓储接口IRepository<TEntity, TPrimaryKey>
#region 程序集 Abp, Version=4.3.0.0, Culture=neutral, PublicKeyToken=null
// C:\Users\Administrator\.nuget\packages\abp\4.3.0\lib\netstandard2.0\Abp.dll
// Decompiled with ICSharpCode.Decompiler 3.1.0.3652
#endregion
using Abp.Dependency;
using Abp.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;namespace Abp.Domain.Repositories
{public interface IRepository<TEntity, TPrimaryKey> : IRepository, ITransientDependency where TEntity : class, IEntity<TPrimaryKey>{IQueryable<TEntity> GetAll();IQueryable<TEntity> GetAllIncluding(params Expression<Func<TEntity, object>>[] propertySelectors);List<TEntity> GetAllList();Task<List<TEntity>> GetAllListAsync();List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);T Query<T>(Func<IQueryable<TEntity>, T> queryMethod);TEntity Get(TPrimaryKey id);Task<TEntity> GetAsync(TPrimaryKey id);TEntity Single(Expression<Func<TEntity, bool>> predicate);Task<TEntity> SingleAsync(Expression<Func<TEntity, bool>> predicate);TEntity FirstOrDefault(TPrimaryKey id);Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id);TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);TEntity Load(TPrimaryKey id);TEntity Insert(TEntity entity);Task<TEntity> InsertAsync(TEntity entity);TPrimaryKey InsertAndGetId(TEntity entity);Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity);TEntity InsertOrUpdate(TEntity entity);Task<TEntity> InsertOrUpdateAsync(TEntity entity);TPrimaryKey InsertOrUpdateAndGetId(TEntity entity);Task<TPrimaryKey> InsertOrUpdateAndGetIdAsync(TEntity entity);TEntity Update(TEntity entity);Task<TEntity> UpdateAsync(TEntity entity);TEntity Update(TPrimaryKey id, Action<TEntity> updateAction);Task<TEntity> UpdateAsync(TPrimaryKey id, Func<TEntity, Task> updateAction);void Delete(TEntity entity);Task DeleteAsync(TEntity entity);void Delete(TPrimaryKey id);Task DeleteAsync(TPrimaryKey id);void Delete(Expression<Func<TEntity, bool>> predicate);Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);int Count();Task<int> CountAsync();int Count(Expression<Func<TEntity, bool>> predicate);Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);long LongCount();Task<long> LongCountAsync();long LongCount(Expression<Func<TEntity, bool>> predicate);Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate);}
}
2.默认仓储实现类PDRepositoryBase
在命名空间namespace下 EntityFrameworkCore.Repositories,默认仓储实现PDRepositoryBase<TEntity, TPrimaryKey>
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore;
using Abp.EntityFrameworkCore.Repositories;namespace PD.EntityFrameworkCore.Repositories
{public abstract class PDRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<PDDbContext, TEntity, TPrimaryKey>where TEntity : class, IEntity<TPrimaryKey>{protected PDRepositoryBase(IDbContextProvider<PDDbContext> dbContextProvider): base(dbContextProvider){}}public abstract class PDRepositoryBase<TEntity> : PDRepositoryBase<TEntity, int>, IRepository<TEntity>where TEntity : class, IEntity<int>{protected PDRepositoryBase(IDbContextProvider<PDDbContext> dbContextProvider): base(dbContextProvider){}}
}
对于如何调用默认仓储,后续文章会有说明
二、自定义仓储
1.定义仓储接口PDRepositoryBase
using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;namespace PD.Menu.Repository
{public interface IMenuRepository : IRepository<Sys_Menu, int>{Task<List<Sys_Menu>> GetSys_MenuList();}
}
2.仓储实现类MenuRepository
using Abp.EntityFrameworkCore;
using PD.EntityFrameworkCore.Repositories;
using PD.Menu;
using PD.Menu.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace PD.EntityFrameworkCore.MenuRepository
{public class MenuRepository : PDRepositoryBase<Sys_Menu, int>, IMenuRepository{public MenuRepository(IDbContextProvider<PDDbContext> dbContextProvider) : base(dbContextProvider){}public async Task<List<Sys_Menu>> GetSys_MenuList(){//GetAllListAsync()为默认仓储中的方法,可直接使用var query = await GetAllListAsync();//TODO.......return query;}}
}
说明:ABP中使用了依赖注入容器Castle.Windsor,因此构造函数中的注入对象由容器自动注入
dbContextProvider对象由容器Castle.Windsor自动注入
public MenuRepository(IDbContextProvider<PDDbContext> dbContextProvider) : base(dbContextProvider)
{
}
容器Castle.Windsor配置位于Startup启动类的ConfigureServices方法中
services.AddAbp<PDWebHostModule>(// Configure Log4Net loggingoptions => options.IocManager.IocContainer.AddFacility<LoggingFacility>(f => f.UseAbpLog4Net().WithConfig("log4net.config")));
,依赖注入后续学习中会继续讲解
这篇关于ABP框架—后台:仓储Repository(8)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!