0

which approach should be taken when adding entities to an aggregate?

I could add an constructed entity to the aggregate

$book = new Book($title, $releaseDate, ...);
$library->addBook($book)

or I could pass the parameters to the addBook method and let the method to construct the instance:

$library->addBook($title, $releaseDate, ...);
2

I would take the:

Library.AddBook(Book book)

Approach. Essentially a Book can exist outside of a library and we would want to add a particular book instance to a library, not a duplicate instance.

I think this would be the general case for child objects, although you can imagine cases where the child object is an integral part of the parent and the second method might be preferable. Maybe...

Baboon.AddHeart(parameter list...)

Here you can't add just any heart, there will only ever be one etc. its more like a setup step in creating a Baboon than adding a child object.

var b = new Baboon(name: "Jimmy")
    .AddHeart(parameter list...)
    .AddLungs(parameter list...)
    .AddLiver(parameter list...)
    .AddBrain(parameter list...)
    .Vivify();
  • Thanks a lot, luckily the example I put fits in your first approach but what I need is the second one, so I can see the differences. (My aggregate root is a Trip which has TripRequests to different drivers, so a TripRequest can't exist by itself) – vivoconunxino Feb 1 at 11:19
  • 1
    I did give both examples, onyl you can know which bettter fits your domain – Ewan Feb 1 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.