简体   繁体   中英

entity framework core, generic repository method with inclusion navigation properties

I want to create a universal data access layer through a business logic project, The business logic layer must interact with the DAL layer in which I have a repository. I want to have a short-lived data context and all the repository methods returning IEnumerable and not IQueryable. Prompt on an example of method GetAll how I can extract the connected data through ThenInclude or Select to dependent tables.

public class GenericDataRepository<T> : IGenericDataRepository<T> where T : class
{
    public virtual IList<T> GetAll(params Expression<Func<T, object>>[] navigationProperties)
    {

        List<T> list;
        using (var context = new Context())
        {
            IQueryable<T> dbQuery = context.Set<T>();

            //Apply eager loading
            foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
                dbQuery = dbQuery.Include<T, object>(navigationProperty);

            list = dbQuery
                .AsNoTracking()
                .ToList<T>();
        }
        return list;
    }

}

ThenInclude(prop2=>prop2.i)?

dbQuery.Include(navigationProperty).ThenInclude(prop2=>prop2.i)

This is actually bad design. You don't want to do this. If you want to abstract your data access layer, then do so completely. Create a library that provides an API for your application, which returns exactly what you need, no more and no less. If you're going to still allow arbitrary querying, then just utilize your context directly, as you're not actually buying yourself anything other than an additional layer to maintain, with no real benefit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM