Service Architecture for Spring Boot
Introduction
Are your spring boot services getting too complex to understand & maintain? Do you spend too much time trying to fit new functionalities into existing services/code instead of focusing on solutions? Writing service test cases & maintaining them has become painstaking?
If yes, let's see how applying SOLID design principles can solve some of these problems.
SOLID Principles
First, let's recall the SOLID principles before diving deep. SOLID is one of the most popular sets of design principles in object-oriented software development. It’s a mnemonic acronym for the following five design principles:
Let's take a look at how a typical service looked earlier in our projects.
Design problems with the existing structure
A few other design problems with our code base were -
New Design Architecture for Services
The below figure shows the new architecture designed which tries to enforce certain aspects of the SOLID principle and solves problems with our old service design.
The following are the main components of the new architecture.
Let's understand each component in detail before understanding how it solves the problems discussed earlier.
Service
The service component can be a spring service or a spring component which interacts with external systems/services or service util. The service component depends on other components like mappers, DAO decorators, service validators, other external services, and service utils to perform expected operations.
The service component only decides the flow of data and interaction among other components. This makes unit testing of service easier since other components can be easily mocked and the flow of data and interaction with other components (mocks) can be easily verified.
领英推荐
Mapper
The mapper component is used to map DTOs (Data Transfer Object) to & from Spring JPA/Hibernate entities. If mapping requires custom logic or data from the database, the map struct mapper can be extended using a decorator pattern.
Let's see how to add custom logic with decorator pattern and map struct. Say you have a requirement where if an admin user has requested user details, you need to send SMS usage along with user details. We can use map struct @DecoratedWith annotation and create and custom abstract class to write custom mapping logic.
Following is a sample implementation -
You can use the above mapper in your service class as you would a non-decorated mapper.
DAO Decorator(DD)
DAO decorators are wrappers over Spring JPA repositories. They are used to enforce database validations, custom locking, handling type conversions, create abstractions like read-only DAO, and create abstract actions of database tables which modified multiple columns.
Let's see what a DAO decorator looks like -
Service Validator
Service validators abstract all business level validations. Service validators can throw different types of exceptions related to business validation.
Let's see what a service validator looks like -
Validating new design against SOLID principles
First, let's see what a service will look like after applying the new design
Now let's validate it against design problems with the older design
Few other topics
Let us know which topic we should talk about next in the comments.