Entity Framework Generic repository including properties through parameter

后端 未结 3 1768
日久生厌
日久生厌 2020-12-20 06:59

I have an implementation of a Generic Repository in Entity Framework which I am trying to improve to use the .Include(..) function provided by EF instead of including the na

相关标签:
3条回答
  • 2020-12-20 07:06

    I suggest you to keep two methods, one accepting string params and one expression params. Some of the clients of your repository can work better with the string signature and some of them can work better with expression signature which brings IntelliSense for them.

    public IQueryable<T> GetAll(params string[] including)
    {
        var query = dbSet.AsQueryable();
        if (including != null)
            including.ToList().ForEach(include =>
            {
                if (!string.IsNullOrEmpty(include))
                    query = query.Include(include);
            });
        return query;
    }
    
    public IQueryable<T> GetAll(params Expression<Func<T, object>>[] including)
    {
        var query = dbSet.AsQueryable();
        if (including != null)
            including.ToList().ForEach(include =>
            {
                if (include != null)
                    query = query.Include(include);
            });
        return query;
    }
    

    Make sure you have added using System.Data.Entity;.

    You can implement the logic for filter and sort the same way. For the string params signature for filter and sort, you can use System.Linq.Dynamic package.

    Example:

    var result1 = schoolRepository.GetAll("Students", "Teachers");
    var result2 = schoolRepository.GetAll(x=>x.Students, x=>x.Teachers);
    
    0 讨论(0)
  • 2020-12-20 07:20

    You can use the params Expression<Func<T, object>>[] includeProperties instead of string parameter

    public IQueryable<T> GetAll(
        Expression<Func<T, bool>> filter = null,
        Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
        params Expression<Func<T, object>>[] includeProperties)
    {
        IQueryable<TEntity> query = dbSet;
    
            if (filter != null)
            {
                query = query.Where(filter);
            }
    
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
    
            if (orderBy != null)
            {
                return orderBy(query);
            }
            else
            {
                return query;
            }
    }
    
    0 讨论(0)
  • 2020-12-20 07:25
    protected internal IQueryable<TEntity> Filter(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] includeProperties)
    {
        var query = RetrieveQuery();
    
        if (predicate != null)
        {
            query = query.Where(predicate).AsQueryable();
        }
    
        if (includeProperties != null)
        {
            query = _queryableUnitOfWork.ApplyIncludesOnQuery(query, includeProperties);
        }
    
        return (query);
    }
    

    And this method called there

    public IQueryable<TEntity> ApplyIncludesOnQuery<TEntity>(IQueryable<TEntity> query, params Expression<Func<TEntity, object>>[] includeProperties) where TEntity : class, IEntity
    {
        // Return Applied Includes query
        return (includeProperties.Aggregate(query, (current, include) => current.Include(include)));
    }
    

    Filter Method Call

     public IEnumerable<ShowStockProductDto> GetActiveShowStockProductListByProduct(int productId)
                {
                    var foundedPStockroducts = Filter(
                        ent => ent.ProductId == productId && ent.IsActive,
                        ent => ent.StockProductPrices,
                        ent => ent.StockProductDepots,
                        ent => ent.StockProductSpecificationValues,
                        ent => ent.StockProductSpecificationValues.Select(spsv => spsv.SpecificationValue)
                        );
    
                    // Map foundedPStockroducts to showStockProductList
                    var showStockProductList = TypeAdapterFactory.Adapter.Adapt<IEnumerable<ShowStockProductDto>>(foundedPStockroducts).ToList();
    
                    return (showStockProductList);
                }
    
    0 讨论(0)
提交回复
热议问题