对于Organization Units,我们使用以下代码获取组织单位中的实体,包括其子组织单位。是否合理以及如何扩展IRepository
以将此功能添加到所有实体?
public virtual List<Product> GetProductsInOuIncludingChildren(long organizationUnitId)
{
var code = _organizationUnitRepository.Get(organizationUnitId).Code;
var query =
from product in _productRepository.GetAll()
join organizationUnit in _organizationUnitRepository.GetAll() on product.OrganizationUnitId equals organizationUnit.Id
where organizationUnit.Code.StartsWith(code)
select product;
return query.ToList();
}
答案 0 :(得分:1)
首先,继承IMustHaveOrganizationUnit
:
public class Product : Entity, IMustHaveOrganizationUnit
{
public long OrganizationUnitId { get; set; }
}
然后定义扩展方法:
public static class RepositoryExtensions
{
public static List<TEntity> GetAllInOuIncludingChildren<TEntity, TPrimaryKey>(
this IRepository<TEntity, TPrimaryKey> repository,
long organizationUnitId
)
where TEntity : class, IEntity<TPrimaryKey>, IMustHaveOrganizationUnit
{
using (var organizationUnitRepository = repository.GetIocResolver().ResolveAsDisposable<IRepository<OrganizationUnit, long>>())
{
var code = organizationUnitRepository.Object.Get(organizationUnitId).Code;
var query =
from entity in repository.GetAll()
join organizationUnit in organizationUnitRepository.Object.GetAll() on entity.OrganizationUnitId equals organizationUnit.Id
where organizationUnit.Code.StartsWith(code)
select entity;
return query.ToList();
}
}
}
用法:
var products = _productRepository.GetAllInOuIncludingChildren(organizationUnitId);
答案 1 :(得分:0)
您可以使用以下代码。
<强>接口强>
public interface ITestRepository : IRepository<Test, int>
{
}
<强>类别:强>
public class TestRepository : YourProjectNameRepositoryBase<Test, int>, ITestRepository
{
public TestRepository(IDbContextProvider<YourProjectNameDbContext> dbContextProvider, IObjectMapper objectMapper)
: base(dbContextProvider, objectMapper)
{
}
}
<强>用法:强>
public class TestAppService : YouProjectNameAppServiceBase, ITestAppService
{
private readonly ITestRepository _testRepository;
public TestAppService(ITestRepository testRepository,
}