Spring with Kotorin --7 Service has been modified to avoid concentrating responsibilities on a single module. The reason is to avoid becoming a ** Fat Controller ** with low maintainability, extensibility, and readability. For that purpose, we separated the application logic from the Controller layer and provided a ** Service ** layer.
This makes it possible to separate the functional areas to be focused on in the Controller layer and the Service layer.
layer | role |
---|---|
Controller layer | Accepting requests |
Service layer | Processing of business logic other than request acceptance |
Now consider the newly created responsibilities of the Service layer.
The role of the Service layer is to handle business logic other than accepting requests
.
In other words, as it is, the persistence processing of the result of business logic will also be performed in this Sevice layer.
By the way, if you think about data persistence, if you are aware of the target area to be persisted in the Service layer, It is necessary to design according to the type of target area.
In addition, the dependency on the type of external area becomes stronger, and the range of influence becomes larger when making changes. For example, taking a database as an example, changing from Oracle DB to MySQL requires a design that is aware of the dialect differences.
Considering the role of the service layer, ** business logic processing **, it is easier to design and maintainability if the result can be processed transparently without being aware of the persistence destination.
A ** Repository ** layer is provided as a mechanism to separate the persistence process from this Service layer.
Spring Dependencies
When setting up the Repository layer, this time we will target the database as a persistence area. Also, for the sake of brevity, we will use the H2 database as the database type and run it in embedded mode.
Dependency Add the following Dependency to build.gradle for database access.
Also, add the following Dependency to use H2 Database.
Add the following definitions for the H2 database to application.yml.
spring:
datasource:
tomcat:
test-on-borrow: true
validation-interval: 30000
validation-query: SELECT 1
remove-abandoned: true
remove-abandoned-timeout: 10000
log-abandoned: true
log-validation-errors: true
max-age: 1800000
max-active: 50
max-idle: 10
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:app;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
username: guest
password: guest
h2:
console:
enabled: true
jpa:
hibernate:
ddl-auto: update
show-sql: true
The following interface definition was made to provide a layer for persistence processing.
interface MessageRepository : CrudRepository<Message, String>
Makes you work with entities that persist data through this interface.
From the Service layer, the entity is operated via the injected Repository layer instance.
@Autowired
lateinit var repository: MessageRepository
fun getMessages() : Iterable<MessageDTO> = repository.findAll().map { it -> MessageDTO(it) }
fun insertMessage(messageDto: MessageDTO) = MessageDTO(
repository.save(Message(
title = messageDto.title,
message = messageDto.message
))
)
You can see that the code has become much simpler than before the separation by designing the roles as Controller / Service / Repository separately. By designing in this way, it is possible to prevent the module from becoming bloated, spaghetti coded, and reduced in scalability.
Recommended Posts