Jordan Walker
Jordan Walker

Reputation: 630

Can infrastructure services return domain entities in Uncle Bob's The Clean Architecture?

Consider:

The Use Case layer defines an interface

public interface IGeocoder
{
    Coordinate GetCoordinate(Address address);
}

Coordinate and Address are value objects defined in the domain layer (i.e. entities layer in Uncle Bob jargon). Obviously the use case (interactor) constructs the Address and passes it to the IGeocoder, expecting a Coordinate to be returned.

Would this break any Clean Architecture rules? Should I be instead passing DTOs through the interface, so that the actual implementation of the service is not responsible for generating the domain entities? Or is this fine?

Note that I don't see any difference between this and a repository (entity gateway), whose interface would also be defined in the Use Case layer but implemented in the surrounding Interface Adapters layer:

public interface IRestaurantRepository
{
   // ...
}

Upvotes: 4

Views: 2005

Answers (2)

Bob
Bob

Reputation: 1625

This all depends on what your "entity" is, and what the "DTO" is. If the "entity" and the "DTO" are basically the same, then you aren't violating anything by passing in the entity.

However, if your entity had a function like address.getISOAddress() and your getCoordinates function was expected to call that function, to get the correct address/coordinates, then that would violate the architecture, and instead you should pass in a ISOAddress into your getCoordinates usecase.

The important thing to remember is that you are dealing with interfaces, and contracts or abstractions. If that interface is dealing with an abstract class, a concrete implementation of an exact instance should not and does not matter. What matters is that you have a clear interface at the right level of abstraction, and that your system is flexible so that the implementation details can change and your system still works without major modifications.

Upvotes: 1

plainionist
plainionist

Reputation: 3573

Who is owning the implementation of the infrastructure service? Is it the same team? Then it should be fine to handle it similar to repositories; return domain entities directly. Is it owned by different team? Then creating a boundary and passing dedicated DTOs might be beneficial as it creates less coupling.

Upvotes: 2

Related Questions