Search code examples
hexagonal-architecture

Hexagonal Architecture, do I violate its principles by reusing code between usecases?


Hello and thank you for taking the time to read my question :)

I have several usecases that need to download and upload files from and to s3. For that, I've created a port and an S3 adapter that I share between those usecases. Somehow I feel that this is a terrible idea but I don't want to duplicate code.

Does that mean that I've baldy designed my usecases?

What is the correct approach here?


Solution

  • If you have multiple use cases, then you SHOULD have multiple root aggregates, one per use case. If you have multiple root aggregates, you SHOULD have multiple ports, each one handling persistance for a single use case. Since all use cases use the same S3 persistence backend, you MAY implement a single adapter, that implements all ports, and handles persistence for all use cases.

    This allows you to move any use case to another persistence backend later, if needed, without breaking your existing code. You can do that by implementing a new persistence adapter for the new backend, and configuring your dependency injection to use this new adapter instead of the previous one for that port.