在DDD领域驱动设计中,仓储实现了对数据进行增删改查操作的代码。
(1)ABP里面已经为我们定义了默认仓储,里面已经包含基本的仓储方法(增删改查)。我们可以直接调用。
(2)当ABP的默认仓储不能满足业务时,我们可以在默认仓储上进行扩展。继承默认仓储接口IRepository,和继承默认仓储实现类PDRepositoryBase来进行扩展。
(3)如果在默认仓储上进行扩展,我要把仓储接口和仓储实现分离。仓储接口应放置于领域层(Core),仓储实现应该放置于基础设施层(EntityFrameworkCore)
在命名namespace空间下 Abp.Domain.Repositories,默认仓储接口IRepository
#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 : IRepository, ITransientDependency where TEntity : class, IEntity
{
IQueryable GetAll();
IQueryable GetAllIncluding(params Expression>[] propertySelectors);
List GetAllList();
Task> GetAllListAsync();
List GetAllList(Expression> predicate);
Task> GetAllListAsync(Expression> predicate);
T Query(Func, T> queryMethod);
TEntity Get(TPrimaryKey id);
Task GetAsync(TPrimaryKey id);
TEntity Single(Expression> predicate);
Task SingleAsync(Expression> predicate);
TEntity FirstOrDefault(TPrimaryKey id);
Task FirstOrDefaultAsync(TPrimaryKey id);
TEntity FirstOrDefault(Expression> predicate);
Task FirstOrDefaultAsync(Expression> predicate);
TEntity Load(TPrimaryKey id);
TEntity Insert(TEntity entity);
Task InsertAsync(TEntity entity);
TPrimaryKey InsertAndGetId(TEntity entity);
Task InsertAndGetIdAsync(TEntity entity);
TEntity InsertOrUpdate(TEntity entity);
Task InsertOrUpdateAsync(TEntity entity);
TPrimaryKey InsertOrUpdateAndGetId(TEntity entity);
Task InsertOrUpdateAndGetIdAsync(TEntity entity);
TEntity Update(TEntity entity);
Task UpdateAsync(TEntity entity);
TEntity Update(TPrimaryKey id, Action updateAction);
Task UpdateAsync(TPrimaryKey id, Func updateAction);
void Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
void Delete(TPrimaryKey id);
Task DeleteAsync(TPrimaryKey id);
void Delete(Expression> predicate);
Task DeleteAsync(Expression> predicate);
int Count();
Task CountAsync();
int Count(Expression> predicate);
Task CountAsync(Expression> predicate);
long LongCount();
Task LongCountAsync();
long LongCount(Expression> predicate);
Task LongCountAsync(Expression> predicate);
}
}
在命名空间namespace下 EntityFrameworkCore.Repositories,默认仓储实现PDRepositoryBase
using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore;
using Abp.EntityFrameworkCore.Repositories;
namespace PD.EntityFrameworkCore.Repositories
{
public abstract class PDRepositoryBase : EfCoreRepositoryBase
where TEntity : class, IEntity
{
protected PDRepositoryBase(IDbContextProvider dbContextProvider)
: base(dbContextProvider)
{
}
}
public abstract class PDRepositoryBase : PDRepositoryBase, IRepository
where TEntity : class, IEntity
{
protected PDRepositoryBase(IDbContextProvider dbContextProvider)
: base(dbContextProvider)
{
}
}
}
对于如何调用默认仓储,后续文章会有说明
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
{
Task> GetSys_MenuList();
}
}
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, IMenuRepository
{
public MenuRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider)
{
}
public async Task> GetSys_MenuList()
{
//GetAllListAsync()为默认仓储中的方法,可直接使用
var query = await GetAllListAsync();
//TODO.......
return query;
}
}
}
说明:ABP中使用了依赖注入容器Castle.Windsor,因此构造函数中的注入对象由容器自动注入
dbContextProvider对象由容器Castle.Windsor自动注入
public MenuRepository(IDbContextProvider
{
}
容器Castle.Windsor配置位于Startup启动类的ConfigureServices方法中
services.AddAbp(
// Configure Log4Net logging
options => options.IocManager.IocContainer.AddFacility(
f => f.UseAbpLog4Net().WithConfig("log4net.config")
)
);
,依赖注入后续学习中会继续讲解