Are there many add / change methods and the constructor overloads the consequences of DDD?

I have a class:

public class Person { public string FirstName { get; private set; } public string LastName { get; private set; } public string Email { get; private set; } public string Telephone { get; private set; } public Address Address { get; private set; } public Person(string firstName, string lastName) { //do null-checks FirstName = firstName; LastName = lastName; Address = new Address(); } public void AddOrChangeEmail(string email) { //Check if e-mail is a valid e-mail here Email = email; } public void AddOrChangeTelephone(string telephone) { //Check if thelephone has correct format and valid symbols Telephone = telephone; } public void AddOrChangeAdress(Address address) { Address = address; } 

Properties that are not in the constructor are optional, that is, a person does not need an email address, address or phone. However, I want to give the user of the class the opportunity to create an object without first having to provide the required information, and then find out which methods to use to add the information.

Questions:

  • Is it right to create 3 extra overloads to give them this option?
  • Should I allow additional properties to public setters and do validation there?
  • If a person gets married and changes his last name, do I need an additional method for changing the last name or should I make this setter publicly available and just require them in the constructor?
+1
source share
3 answers
  • No.
  • Yes
  • Make it publicly available.

Assuming you have more code in AddOrChange methods, such as formatting logic or validation, I would do the following. Otherwise, I would completely get rid of the AddOrChange methods:

 public class Person { private string _email = string.empty; private string _telephone = string.empty; private Address _address = new Address(); public string FirstName { get; set; } public string LastName { get; set; } public string Email { get { return _email } set { AddOrChangeEmail(value); } } public string Telephone { get { return _telephone;} set { AddOrChangeTelephone(value); } } public Address Address { get { return _address; } set { AddOrChangeAddress(value); } } public Person(string firstName, string lastName) { //do null-checks FirstName = firstName; LastName = lastName; } private void AddOrChangeEmail(string email) { //Check if e-mail is a valid e-mail here _email = email; } private void AddOrChangeTelephone(string telephone) { //Check if thelephone has correct format and valid symbols _telephone = telephone; } private void AddOrChangeAddress(Address address) { _address = address; } } 

To work with this class, you can do any of the following:

 Person p = new Person("Tom", "Jones"); p.Telephone = "9995551111"; 

or

 Person p = new Person("Tom", "Jones") { Telephone = "9995551111", Email=" spamme@ms.com " } 
+3
source

AddOrChange equivalent to a simple property with an open network device, so you do not need these methods.

 public class Person { public string FirstName { get; private set; } public string LastName { get; private set; } public Email Email { get; set; } public Telephone Telephone { get; set; } public Address Address { get; set; } public Person(string firstName, string lastName) { //do null-checks FirstName = firstName; LastName = lastName; } } 
  • If the user during creation of the person wants to provide something in addition to the required data, she can use class initializers. You can also add some additional parameters to the constructor.

    var bob = new Person("Bob", "Uncle") { Address = someAddress };

  • If it’s normal for a person to move, then why not use a public setter to change the address? Of course, you have to check if the address is valid. Also, if moving is a business process (i.e. you are moving someone to a hotel), then it would be nice to have this operation in the domain service (which will check that the destination room is empty and ready).

  • Allow name change in order. Usually the name is not an identifier for such objects, so it can change.

In addition, I introduced value objects for email and phone. I believe that it is not the responsibility to verify the correctness of the email address. Move this to the Email class. Same thing with the phone and address.

+2
source

Many add / change methods and the constructor overloads the consequences of DDD?

No, many upgrade methods are not a consequence of DDD.

code

Your Person class can be rewritten to have only 2 update methods:

 class Person public function Rename(FirstName as Name, LastName as Name) as Person public function ChangeContacts( Address as Maybe(of Address), Phone as Maybe(of Phone), Mail as Maybe(of MailAddress)) as Person end class 

Rename method accepts two required parameters of the special type Name . Name verification checks occur when names are created, and not when they are passed to the Person class.

ChangeContacts method accepts three optional parameters, any of which may be absent. The special Maybe type indicates that they are optional. The special types Address , Phone and MailAddress indicate that these parameters are already valid and there is no need to check them again in the Person class.

Use case

A person marries and changes his last name

 Person = Person. Rename(Person.FirstName, LastNameAfterMarriage) 

User buys a new phone number

 Person = Person. ChangeContacts(Person.Address, NewPhoneNumber, Person.Mail) 

Personal telephone number

 Dim NewPhoneNumber = Maybe.Create(Nothing) Person = Person. ChangeContacts(Person.Address, NewPhoneNumber, Person.Mail) 

The template should call the update method with the old values ​​+ some new values.

+1
source

Source: https://habr.com/ru/post/1441679/


All Articles