views:

61

answers:

1

Hi,

I am new to the DDD. 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 emails = new HashSet }

Class Email { String value; Email(String value){ } }

I have 2 requirements in the system 1) User can send a request to add a new Email to person 2) 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 meeth my requirements above.

1) To create Email from party but not add it to the emails list(see createEmail() below).

2) Having a seperate method just to add email to the list (see setEmail() below).

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

}

A: 

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