I'm using ASP.NET MVC 3 and my repositories are using ADO.NET with stored procedures to fill my domain objects. I'm not 100% sure where certain objects should be filled. For example I need to fill the User property based on the UserId retrieved. What is the best approach to do this? For each domain object I have a repository and a unit of work object that manages the transaction and connection.
public class Comment
{
public string Post { get; set; }
public int UserId { get; set; }
public int ParentCommentId { get; set; }
List<Comment> Comments { get; set; }
public User User { get; set; }
}开发者_开发百科
Any insight would be greatly appreciated.
Without knowing more about your domain it is difficult to answer the quesiton. This is more related to Domain Driven Design than to the MVC framework. The concept you need to investigate is called Aggregates in DDD. An aggregate is a collection of model objects that logically fit together. One of the models in the aggregate would be a root object that handles the other model objects in the aggregate including persistence.
You would have a repository for each aggregate root and not for each model object. I would recommend going through the free downloadable minibook from InfoQ called Domain Driven Design Quickly. The author has summarized the excellent Domain Driven Design book by Eric Evans in this minibook.
精彩评论