Creating a Service

We'll cover the following

Defining a service

In a simple example such as the one we’re looking at, we may skip the service and directly let the controller talk to the repository. However, introducing a service will help us see how Kotlin can be used to create a service. And if we decide to expand the example, to add more behavior, then that can readily go into the service.

The service will sit in between the controller and the database and take care of making all the calls necessary to manipulate the persistent data. The service needs to talk to the repository; but no worries, Spring can auto-wire that dependency in a blink. We need a method to get all the tasks—one to save a new task, and one to delete a task with a given id.

Kotlin vs. Java

Given all that, the Java version is sure to be more verbose than the Kotlin version. If we were to write in Java, we’d end up with something like this:

Get hands-on with 1200+ tech skills courses.