lundi 10 août 2020

Best practice to include() nested child entity in linq (Repository Pattern)

I'm developing a web application but i have a doubt about whats is the best practice to include nested child entity in linq but using Repository Pattern.

To get all records i'm using the next method in my interface IRepository:

 Task<IEnumerable<T>> GetAllAsync(
         Expression<Func<T, bool>> filter = null,
         Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
         string includeProperties = null 
 );

And my class where I'm implementing Repository:

    public async Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = null)
    {
        IQueryable<T> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        if (includeProperties != null)
        {
            foreach (var includeProp in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                query = query.Include(includeProp);
            }
        }

        if (orderBy != null)
        {
            return await orderBy(query).ToListAsync();
        }
        return await query.ToListAsync();
    }

To get the child entity i'm callig as show below:

IEnumerable<ApplicationUser> applicationUsers = await
_unitOfWork.ApplicationUser.GetAllAsync(d => d.Eliminado == false, includeProperties: "Empleado,Empleado.Departamento").ConfigureAwait(true);

Is this the best way to get Empleado and Empleado.Departamento (child entity)?

or Have i to modify mi method in my implementation to add code like this

.Include(a => a.Empleado)
.ThenInclude(d => d.Departamento)}

?

Thanks

Aucun commentaire:

Enregistrer un commentaire