huyrua / efprs

Entity Framework POCO, Repository and Specification Pattern
MIT License
40 stars 18 forks source link

Eager-Loading nav properties within the generic repo implementation? #4

Open diegohb opened 11 years ago

diegohb commented 11 years ago

Any particular reason to not include a way specify properties to eager load? Just wondering if there was something wrong with that.. I'm thinking I rather isolate all EF-specifics including the .Includes() calls to the same assembly as my generic repo implementation assembly.

I added params to GetQuery<>() and its overloads:

Queryable<TEntity> GetQuery<TEntity>(params Expression<Func<TEntity, object>>[] pExpandPropertySelectors) 

and before return the objectQuery:

if (pExpandPropertySelectors != null)
            {
                objectQuery = pExpandPropertySelectors.Aggregate
                    (objectQuery, (pCurrent, pInclude) => pCurrent.Include(pInclude) as ObjectQuery<TEntity>);
            }

also modified the GetByKey method to make a call out to DbContext.Entry(entityObject).Reference/Collection.Load() ..

huyrua commented 11 years ago

1) In net40. package, there is IQueryableExtension that implement IQueryable.Include([...]) under Infrastructure.Data.EntityFramework\EntityFrameworkExtension.cs 2) In net45 package, since we are using EF 5, the Include method has already implemented in System.Data.Entity, you can find the use at Infrastructure.Tests.Data\RepositoryTest.cs, the method name is FindCategoryWithInclude().