oguzh4n
oguzh4n

Reputation: 682

ddd, collections of value object

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;
    }
}

In this case FeedUser is Value Object or Entity? I'm confuse because FeedUser no need identity but not 100% immutable (IsRead property changable)

A Feed have a lot of FeedUser. At NHibernate, should I load all FeedUsers property (Lazy Load or Eager loading) for add new FeedUser or remove from list?

Upvotes: 0

Views: 815

Answers (1)

Yugang Zhou
Yugang Zhou

Reputation: 7283

Question1: The FeedUser is not a value object since it's mutable and has it's life cycle(but binding with the Feed's lifecycle). In this design, it's a local entity(userId as local identifier).

Question2: I'm afraid you have to load all FeedUsers first.

Indirect answer: If you think it's awkward, you may ask yourself what's the invariant to be protected? what about make FeedUser an aggregate?

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

Upvotes: 1

Related Questions