Search code examples
repository-patterneager-loading

Eager loading and repository pattern


I'm wondering how to properly handle eager-loading problem for complex object graphs when using Repository pattern. This isn't ORM specific problem i guess.

First try:

public interface IProductRepository : IRepository<Product>
{
  Product GetById(int id);
  IProductRepository WithCustomers();
}

This would work fine, but that would involve repeating myself all the time (writing custom 'With' methods in repository implementations everywhere).

Next approach:

public interface IRepository<T> where T : IAggregateRoot
{
  ...
  void With(Expression<Func<T, object>> propToExpand);
}

With method will add an item to private collection which will be used later to find out what props should be eager loaded when retrieving necessary entity/ies.

This kind a works and is fine. But i dislike usage:

productRepository.With(x=>x.Customer);
productRepository.With(x=>x.Price);
productRepository.With(x=>x.Manufacturer);
var product = productRepository.GetById(id);

Basically - problem is that there isn't chaining. I would like it to be like this:

var product = productRepository
  .With(x=>x.Customer)
  .With(x=>x.Price)
  .With(x=>x.Manufacturer)
  .GetById(id);

I couldn't achieve this. Even if i could - i'm not sure if that solution would be elegant.

This leads to thoughts that i'm missing something fundamental (lack of examples anywhere). Are there different ways how to handle this? What are best practices?


Solution

  • Interesting problem and I am sure you are not the first one having trouble with this (I absolutelty have).

    For me, the real question is: where do you want to put your eager loading logic?

    Outside of the repository in the client code

    var product = productRepository
    .With(x=>x.Customer)
    .With(x=>x.Price)
    .With(x=>x.Manufacturer)
    .GetById(id);
    

    I dont think that is good software design: it looks like this could cause "death by a thousand cuts" if such constructs are scattered through your whole app.

    Or within the repository. Example:

    interface IProductRepository {
        Product GetById(int id);
        Product GetByIdWithCustomers(int i);
    }
    

    So your client code would look like this:

    var product = productRepository.GetByIdWithCustomers(id);
    

    Normally I make one BaseRepository which has just the basic CRUD operations defined:

    public class BaseRepository<TEntity, TPrimaryKey> {
        public void Save(TEntity entity) { ... }
        public void Delete(TEntity entity) { ... }
        public TEntity Load(TPrimaryKey id) { ... } // just gets the entity by primary key
    }
    

    Then I extend this base Class / Interface in order to provide specific methods for fetching domain objects. Your approach seems to go in a somewhat similiar direction.

    public class MediaRepository : BaseRepository<Media, int> {
        public long CountMediaWithCategories() { ... }
        public IList<Media> MediaInCategories(IList<Category> categories) { .... }
    }
    

    The good thing: all ORM stuff (eager loading config, fetch depth etc) is encapsulated in the Repository class, the client code just gets the result set.

    I tried working with very generic repositories like you are trying to do, but I mostly ended up writing specific queries and repositories for my domain objects.