Considering the following "database" :
Post -> User
The entities :
class User
{
public virtual string Name { get; set; }
public virtual IList<Post> Posts { get; set; }
}
class Post
{
public virtual string Title { get; set; }
public virtual User Author { get; set; }
}
And the following service Layer:
class UserService
{
IRepositoryUsers repositoryUsers;
IList<User> GetUsers()
{
return this.repositoryUsers.GetAllUsers();
}
}
When I want to print all users, with associated post count, I get (no surprise here) a N+1 select problem, as for each line, it will create a select to get the posts for the users.
Now here is my question : what is the "best" way to handle this, as there are some cases when I don't want to eager load each user's posts.
Should I create as many methods in my repository (and service) to match those scenarios ?