I have a question about how to solve the following scenario using DDD.
I have 2 entities Person and Email with one to many relationship. A person can have zero or more email address(es).
Person is an aggregate root of Email which is a component.
class Person{
    Set<Email> emails = new HashSet<Email>
}
Class Email {
    String value;
    Email(String value){
    }
}
I have 2 requirements in the system:
- User can send a request to add a new Email to person 
- User can create a list of emails temporarily and may or may not add them to person. 
Does having 3 methods make sense in DDD? Or is there a better way to do meet my requirements above.
- To create Email from party but not add it to the emails list (see - createEmail()below).
- Having a seperat开发者_StackOverflow社区e method just to add email to the list (see - setEmail()below).
- A method to create email for a person and add it to the emails list (see - addEmail()below).
public Class Person{
    Set<Email> emails = new HashSet<Email>
    public void addEmail(String value){
        Email email = createEmail(value);
        emails.add(email);
    }
    public Email createEmail(String value){
        return new Email(value);
    }
    public void setEmail(Email email){
        emails.add(email);
    }   
}
Personally, I would see it different:
I would only see 'Person' as being an entity. Email is a value type (component) and not an entity, since it has no identity.
Then, I would expose the eMail adresses of a person as a read-only collection of the Person entity.
public class Person
{
    public void AddEmail( Email mail ) {}
    public void AddEmail( string mailAddress) {}
    public void RemoveEmail( Email mail ){}
    public void RemoveEmail( string mailAddress ){}
    Email[]  GetEmailAdresses() {};
}
public struct Email
{
     public Email( string address ){}
     public string Address
     {
         get {return _address; }
     }
}
Personally I would think that a method called setEmail() would likely add a special email to the person. For example a person has a primary email address. I think it is better to overload addEmail to clarify this point.
Furthermore I would use a factory to create a temporary email. It is not the responsibility of a person to create email addresses in the real world. A good DDD in my imagination is a image of reality so that it is easier to use and in some way self documenting. My code would look something like this:
public Class Person {  
  Set<Email> emails = new HashSet<Email>
  public void addEmail(String value){
        Email email = EmailFactory.createEmail(value);
        emails.add(email);
  }
  public void addEmail(Email email){
        emails.add(email);
  }
}
public class Email {...}
public class EmailFactory {
  public static Email createEmail(String value) {
    return new Email(value);
  }
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论