mtarld / apip-ddd

An example of hexagonal API Platform 3 implementation
MIT License
317 stars 55 forks source link

Questions on best practice example #23

Closed wazum closed 2 years ago

wazum commented 2 years ago

Thanks for the example and the presentation, but as this should be an example project for best practices(?), allow me to ask some questions:

  1. UpdateBookCommandHandler does not update

But remove the complete book and then add it again.

$this->bookRepository->remove($book);
$this->bookRepository->add($book);

Why would you do that?

  1. Properties on the domain model are public

And despite telling me in the Chat on the API platform conference that they are readonly, they are not. You even use this fact in the above-mentioned command handler:

$book->name = $command->name ?? $book->name;
$book->description = $command->description ?? $book->description;
$book->author = $command->author ?? $book->author;
$book->content = $command->content ?? $book->content;

which contradicts ideas DDD stands for. You don't want to modify single (public) properties and expose the object to the risk of entering an invalid state! The correct way to do this update is through a single public method that updates all (private/protected) properties at once (and checks some rules).

chalasr commented 2 years ago

Thanks for attending and for taking the time to open this discussion instead of just bashing live.

About 1, I agree it does not make sense in a real world project. A proper way to update an existing entity should be added.

Regarding 2, these properties should definitely be immutable. If it’s not the case now, then that should be fixed.

mtarld commented 2 years ago

Indeed, that repository is only an API Platform integration example, it isn't intended to be a DDD/Hexagonal best practice example!

But both of your observations are great IMHO. Therefore we must handle them, and feel free to contribute if you want to! 😉

chalasr commented 2 years ago

PR welcome!