I am currently implementing the repository pattern which allows me to pass my class and context into a general repository. This is working perfectly. The methods I am interested in are Get and Find. The find allows me to specify and "order", some "where" statements and some "include" statements. This is ideal.
public class GeneralRepository<TEntity> : IGeneralRepository<TEntity> where TEntity : class
{
readonly GamesContext context;
readonly DbSet<TEntity> db;
public GeneralRepository(GamesContext existingContext)
{
context = existingContext;
db = context.Set<TEntity>();
}
public TEntity Get(object id)
{
return db.Find(id);
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
IQueryable<TEntity> query = db;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
}
My question however, when I use the Get method I would rather it return the full object graph. Lazy loading is turned off at the moment but to eager load I need to add the relevant includes, only this would defeat the object of doing a generic repository.
I know I could just use the find method however it would be much quicker to by default return all related data using the get method.
Any ideas how I can get all (eager load all) by default?
thanks.