Implementing the Domain Services (Hexagonal Architecture with Spring Boot — Part 3)
This is the third article of a 4 5 part series explaining Hexagonal Architecture and how to implement such an architecture using Spring Boot and TDD.
Part 1 - Introduction to Hexagonal Architecture and Key Concepts
Part 2 - Coding demo project and implementation of the API adaptor
Part 3 - Implementation of Domain Services
Part 4 - Implementation of MongoDB repository adaptor
Part 5 - Implementation of REST adaptor to an external data source
Part 2 of the video is live now:
Domain Services
At the end of implementing the REST API adaptor, we have mocked out two domain services GetStockPositionService and GetStockMarketValueService.
As you go through the code below, notice that we will write the domain services functionally, and we can test them without using the Spring framework for dependency injection. Testing domain services functionally and without the Spring framework enables very fast testing.
Also, notice that where the domain services need to call some adaptors, whether data repository or external services, a port to interface is used. Again, in the tests, we specify the required behaviour of these adaptors through these ports, and the domain services does not have to know any further details of these adaptors beyond their behaviours.
Let’s start with the test for GetStockPositionService, GetStockPositionServiceTest as shown here: