我想知道在下面的情况下使用引用进行建模的最佳实践是什么。我正在使用MongoRepository库。
public class User : Entity
{
publis string Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class Post : Entity
{
public string Id { get; set; }
public string Title { get; set; }
public string Summary { get; set; }
public DateTime Added { get; set; }
public User Owner { get; set; }
}
在存储Post时,我只想引用Owner(User)对象而不是整个基础对象。
目前我是这样做的,不知道有更好的方法...
var post = new Post
{
Title = "Example title",
Summary = "asd asd",
Added = DateTime.Now,
Owner = new User { Id = "someExistingUserId" }
};
postRepository.Update(post); //Save
..
//Then to get the post
var post = postRepository.GetById("previouslySavedPostId");
post.Owner = userRepository.GetById(post.Owner.Id);
return post;
userRepository和postRepository都是MongoRepository类型。
这是否是使用C#/MVC(4)与MongoDB解决问题的正确方法?