class Feed : Entity {
public Guid Id {get;set;}
public string FeedText {get;set;}
public ISet<FeedUser> FeedUsers {get;set;}
public void AddFeedUser(User user) {
FeedUsers.Add(user.Id);
}
public void RemoveFeedUser(User user) {
FeedUsers.Remove(user.Id);
}
public void MarkUserAsReadFeed(User user)
{
var feedUser = FeedUsers.Find(u=>u.Id == user.Id);
feedUser.Read();
}
}
class FeedUser : ValueObject {
public Guid UserId {get;private set;}
public bool IsRead {get;private set;}
public DateTime? ReadDate=null {get;private set;}
public void Read(){
IsRead = true;
}
public void UnRead(){
IsRead = false;
}
}
在这种情况下,FeedUser是值对象还是实体?我很困惑,因为FeedUser不需要身份但不是100%不可变(IsRead属性可变)
Feed有很多FeedUser。在NHibernate,我应该加载所有FeedUsers属性(Lazy Load或Eager loading)以添加新的FeedUser或从列表中删除吗?
答案 0 :(得分:0)
问题1:FeedUser不是值对象,因为它是可变的并且具有生命周期(但与Feed的生命周期绑定)。在此设计中,它是一个本地实体(userId作为本地标识符)。
问题2:我担心你必须先加载所有的FeedUsers。间接答案:如果你认为这很尴尬,你可能会问自己,保护的不变量是什么?怎么样使FeedUser成为一个聚合体?
class Feed : Entity {
public Guid Id {get;set;}
public string FeedText {get;set;}
public FeedUser AddFeedUser(User user) {
return new FeedUser(id, user); //sorry, I have to confess I'm not a .net guy.
}
}
class FeedUser : Entity {
public Guid id;
public Guid feedId;
public Guid UserId {get;private set;}
public bool IsRead {get;private set;}
public DateTime? ReadDate=null {get;private set;}
public void Read(){
IsRead = true;
}
public void UnRead(){
IsRead = false;
}
}
//FeedUserRepository is responsible for removeing FeedUser