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 ?
One thing you could do is to have a fetching strategy for every scenario in which you need to retrieve the data. This blog post explains a possible solution for this type of problem, the author uses his own version of IRepository but you use the same ideas in your repository (or check out his NCommon project to get some inspiration)