In Clean Architecture, aren’t Entities another type of Boundary

clean-architecture

In the Clean Architecture, Uncle Bob defines Entities as enterprise-wide business rules and Interactors (Use Cases) as application-specific business rules. Also, he describes that Interactors are responsible for the "dance of the entities", and in the diagrams the Interactor -> Entity relation is shown as direct, without boundaries.

What I don't seem to understand is that in order to test an Interactor, I would inadvertently test code from the Entity, as they seem to be coupled together.

There are some projects that implement the Clean Architecture which do not hold logic inside the Entities, only data.

So, should Entities:

  • hold only data,
  • be another Boundary or
  • be tested with the Interactors or
  • something else?

Best Answer

If you're only holding data in your Entities your domain model will become anemic, because the encapsulation is broken and the behaviors are all defined outside the entity, far from where the data are declared.

The whole point of the Clean Architecture (and the Hexagonal Architecture, Onion Architecture, etc.) is to promote the Dependency Inversion Principle, the D in the SOLID acronyme. To do that, the outer layers need to depend on the the inner layers, not the opposite. That's why it's not only ok but necessary that your interactors depend on your entities. But your entities should not depend on your interactors.

Testing interactors means testing a whole use case, so that's totally intended to execute the code in your entities !