Using repository pattern doesn't mean that you will not be able to use lazy loading. You can still return entity which will be able to lazy load its related entities. The only requirement is that DbContext
used for loading entity must be "alive".
But let's have a look on definition of repository by Martin Fowler:
A Repository mediates between the
domain and data mapping layers, acting
like an in-memory domain object
collection. Client objects construct
query specifications declaratively and
submit them to Repository for
satisfaction. Objects can be added to
and removed from the Repository, as
they can from a simple collection of
objects, and the mapping code
encapsulated by the Repository will
carry out the appropriate operations
behind the scenes. Conceptually, a
Repository encapsulates the set of
objects persisted in a data store and
the operations performed over them,
providing a more object-oriented view
of the persistence layer. Repository
also supports the objective of
achieving a clean separation and
one-way dependency between the domain
and data mapping layers.
I think the interesting part is: Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Also repository is usually used to provide aggregate roots. So you will either always provide whole root (not always possible) or you will satisfy the mentioned statement and you will define eager loading outside of the repository by Include
extension method on IQueryable
. Because of that you will never need specialized methods like GetUserByIdIncludeSomething
.
If you want to user repository start with this method for all queries:
public interface IRepository<T>
{
IQueryable<T> GetQuery();
}
Btw. I don't think that a user is aggregate root for posts. In such case the most of applications will have only single aggregate root - a user.
Edit:
Small clarification: IQueryable
by default doesn't provide Include
method. It is provided as extension method in CTP5 assembly but if you use it you will make your upper layer dependent on EntityFramework.dll. It is something you usually don't want (the reason why you are using repository). So the way to go is define your own extension method wrapping provided extension in assembly with your repository.