dimanche 20 octobre 2019

Domain Driven Implementation - Updating a single property inside Aggregate Root

I am new to DDD and I would like to have some advice on a few challenges I am facing in the implementation of it.

I am using Typescript to develop the application. The data is persisted in a Relational DB. We are not following CQRS pattern and our reads and writes happen in the same database.

Let's assume I have an aggregate User roughly like below,

class User extends AggregateRoot {

id: number;
phone: Phone;
email: Email;
address: Address;

private constructor(id, phone, email, address){
    //setting the values
}

public static create(props) {

    return new User({...props});
}

public static update(props) {

    return new User({...props});
}

}

Here, Phone and Email are ValueObjects and Address is an Entity.

class Phone extends ValueObject {

phNumber: string;

private constructor( ph ) {

    phNumber = ph;
}

public static create(ph){

    //do validations
    return new Phone(ph);
}
}

The class Email is also similar to Phone.

Now, once the update phone request is received in the controller, the request is forwarded to the User Service layer and the service will look roughly like this,

public updatePhone( updatePhNoDto ) {

const userEntity = userRepository.getUser(updatePhNoDto.userId);

const userModel = User.update({
    id: updatePhNoDto.userId,
    phone: Phone.create(userEntity.phone),
    email: Email.create(userEntity.email),
    address: Address.create(userEntity.address)
});

userRepository.updateUser(userModel)
}

Here each time the user requests for updating the phone number, I am fetching the user data from the RDBMS and doing all the validations for all the fields which are already validated and then calling the method User.update(). So, here are my questions:

  1. Not sure if the above is the right way since I am validating the stuffs that I have already validated and possibly an unnecessary DB call. So, please suggest me on the best practices to deal with the situations like this where a single or only a few fields are being requested to be updated.
  2. A user can update his Address independent of his other information. So, should the Address entity be an Aggregate Root by itself? If yes, how should it be handled if both UserInfo and Address are requested to be updated in a single http-request?
  3. What is the role of the Aggregate Root in deletion? How should that be modeled inside it?

Please let me know if you find any other flaws in the design.

Thanks!

Aucun commentaire:

Enregistrer un commentaire