Search code examples
c#repositoryabp-framework

abp.io how to get 2 or more WithDetails in Repository?


I have entity:

class Contract
{
   public TenantProfile TenantProfile { get; set; }
   public ContractStatus Status { get; set; }
}

Service (override GetAsync(Id)):

 var contractWithDetails = (await Repository.WithDetailsAsync(x => x.Status)).FirstOrDefault(x => x.Id == id);

But property TenantProfile - null, because I can't execute WithDetailsAsync for IQueryable. How to solve my problem and execute more then 2 WithDetailsAsync?


Solution

  • It is suggested to create an extension method for each aggregate root with sub collections:

    public static IQueryable<Contract> IncludeDetails(
        this IQueryable<Contract> queryable,
        bool include = true)
    {
        if (!include)
        {
            return queryable;
        }
    
        return queryable
            .Include(x => x.TenantProfile)
            .Include(x => x.ContractStatus);
    }
    

    Now you can override WithDetailsAsync:

    public override async Task<IQueryable<Contract>> WithDetailsAsync()
    {
        // Uses the extension method defined above
        return (await GetQueryableAsync()).IncludeDetails();
    }
    

    Now your WithDetailsAsync method includes both of them.

    See more on ABP Entity Framework Core Integration Best Practices docs.