How do you implement the "Last 20 something" property in your domain class?
Let's say I need to implement a domain model for StackOverflow.
If I am doing an ORM, how can I define a property (and a map) to get "recent comments" and other "recent" things? It seems to me that this should be reflected in the domain model.
Sometimes I may need "all comments" though ...
source to share
You would query your repository for the latest comments from a specific user. something like
IList<Comment> recentComments = repository.GetRecentComments(user,20);
YOU CAN do this in a model, but depending on how many comments you expect, I would avoid it. The model doesn't need to know how to populate itself, that is, how the repository works; however, if you use something like NHibernate it will be there.
public class User{
public IList<Comment> Comments { get;set;}
public IList<Comment> GetRecentComments()
{
// Logic
}
}
In this implementation, you will always download ALL comments to get the last 20. Not a big deal if there are only 50 comments, but if there are 5000 comments, you have quite a lot of overhead.
source to share