When I have been preparing the overview of Hexagonal Architecture I read multiple articles about the topic and some of them contain useful implementation recommendations for the pattern. So let's go trough them.
The starting implementation point is the core application logic, with port interfaces defined around it, at both driving and driven sides. New ports and adapters may appear during implementation.
Tips for ports implementation:
- Do not mention the particular technology in port naming
- Do not include word ‘Port’ in the name
- Define port as a contract (interface) with no default behavior
- Port is part of inside logic, so keep it close to application logic
- Use the "this port is for …ing something" trick to define the name. For example, "this port is for notifying about blog posts" will result
BlogPostNotifier interface name - Port should follow Single Responsibility principle
- Port should follow Interface Segregation Principle: narrow-focused ports are preferable than generic ones
Tips for adapters implementation:
- Do not use
Adapter word in the name - If possible specify technology in the name
- Adapter should be used for a single port only
- Use Adapter pattern for implementation
- Do not introduce dependencies between different adapters
- Follow SOLID principles
- It should be possible to change and add adapters without impact on business logic
- Technology specific errors should not leave adapter boundaries (e.g., application doesn’t know anything about particular SQL errors)
- Cover adapters by integration tests
As a result it should be possible to run the application with any combination of configured ports and adapters.
References:
- Ports and Adapters as They Should Be
- Ports and Adapters Pattern
#architecture #systemdesign #patterns