0

For Organization Units, we used the code below to get entities in an Organization Unit including its child Organization Units. Is it reasonable and how to extend the IRepository to add this feature to all entities?

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();
}
aaron
  • 39,695
  • 6
  • 46
  • 102
Edward
  • 28,296
  • 11
  • 76
  • 121

2 Answers2

2

First, inherit IMustHaveOrganizationUnit:

public class Product : Entity, IMustHaveOrganizationUnit
{
    public long OrganizationUnitId { get; set; }
}

Then define the extension method:

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();
        }
    }
}

Usage:

var products = _productRepository.GetAllInOuIncludingChildren(organizationUnitId);
aaron
  • 39,695
  • 6
  • 46
  • 102
1

You can use the below code.

Interface:

public interface ITestRepository : IRepository<Test, int>
{

}

Class:

public class TestRepository : YourProjectNameRepositoryBase<Test, int>, ITestRepository
{
    public TestRepository(IDbContextProvider<YourProjectNameDbContext> dbContextProvider, IObjectMapper objectMapper)
        : base(dbContextProvider, objectMapper)
        {
        }
}

Usage:

public class TestAppService : YouProjectNameAppServiceBase, ITestAppService
{
    private readonly ITestRepository _testRepository;
    public TestAppService(ITestRepository testRepository,
    
}

Update

In the latest version, there's no need to pass objectmapper and you need to use the updated constructor below:

public class TestRepository : YourProjectNameRepositoryBase<Test, int>, ITestRepository
{
    public TestRepository(IDbContextProvider<YourProjectNameDbContext> dbContextProvider)
        : base(dbContextProvider)
        {
        }
}
Mahyar Mottaghi Zadeh
  • 1,178
  • 6
  • 18
  • 31
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197