xmolecules / jmolecules-integrations

Technology integration for jMolecules
Apache License 2.0
79 stars 21 forks source link

ArchUnit rules should allow nested entities #222

Closed haisi closed 6 months ago

haisi commented 7 months ago

given: Aggregate Root (Menu) 1 → N Entity (Category) 1 → N Entity (CategoryItem)

This causes following test failure when running JMoleculesDddRules.all()

Field l.s.j.m.Category.items is of type List and declared to be used from aggregate Menu!

as JMoleculesDddRules#IsDeclaredToUseTheSameAggregate fails.

I've created a minimal project to reproduce the issue here: https://github.com/haisi/jmolecules-nested-entity

haisi commented 7 months ago

As far as I understood the Javadoc of JMoleculesDddRules#entitiesShouldBeDeclaredForUseInSameAggregate:

An {@link ArchRule} that verifies that fields that implement {@link Entity} within a type implementing {@link AggregateRoot} declare the aggregate type as the owning aggregate.

It describes disallowing references between Aggregates. However, how can I model a nested entity structure belonging to the same Aggregate?

The jmolecules @Entity annotation expects an Aggregate as its generic T

public interface Entity<T extends AggregateRoot<T, ?>, ID> extends Identifiable<ID> {}

seekM commented 7 months ago

I also tried to model a nested entity structure, but - please correct me if I am wrong - unfortunately it doesn't seem to be possible.

I also asked about this here and gave an example for which I feel like this restrictions limits the way I want to use DDD to properly model my domain. Maybe there is also a misunderstanding on my side though as I don't have a lot of experience regarding DDD? Unfortunately I haven't recieved a reply yet - I would love to discuss this more!

haisi commented 6 months ago

@seekM I've created a PoC PR that does what you want.

odrotbohm commented 6 months ago

This has been polished and merged. Thanks, @haisi! 🙇