DEV Community

kaede
kaede

Posted on

Kotlin Springboot -- Part 2 Driver に data class を定義して instance を作り、Usecase でまとめて Main で呼び出す

そもそもの main のフォルダ構成と方針

~/source/springboot/src/main/
kotlin/com/example/springboot/
SpringbootApplication.kt
Enter fullscreen mode Exit fullscreen mode

ここに

package com.example.springboot

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class SpringbootApplication

fun main(args: Array<String>) {
    runApplication<SpringbootApplication>(*args)
    println("main worked")
}
Enter fullscreen mode Exit fullscreen mode

main が有る形。

ここを REST の一番外側として扱う?Driver として扱う?

とりあえず main を gateway として扱い、
インメモリで変数をデータとして扱う Driver を作成して import する


SpringbootDriver の作成

SpringbootApplication と並列に
SpringbootDriver を作成

package com.example.springboot

data class Person(val name: String = "Taro", val age: Int = 0 ) {
    fun findName(): String {
        return name
    }
    fun findAge(): Int {
        return age
    }
}
val taro = Person("Taro", 12)
val hanako = Person("Hanako", 10)

Enter fullscreen mode Exit fullscreen mode

Person クラスにデータを入れて初期化して
findName, findAge という getter を用意する
Person クラスを使って 太郎と花子のインスタンスを作成
export は必要なかった。


main で Driver を呼び出す

import com.example.springboot.taro
import com.example.springboot.hanako

fun main(args: Array<String>) {
    runApplication<SpringbootApplication>(*args)
    println("main worked")
    val taroName = taro.findName()
    val hanakoName = hanako.findName()
    println(taroName)
    println(hanakoName)
}
Enter fullscreen mode Exit fullscreen mode

SpringBootDriver で作ったインスタンスを importして
main で直接呼び出せた。

(グローバルに宣言しているインスタンスなので
import しなくても呼べた)

これで Driver で作ったモノを main で呼び出した。


Usecase で Driver を呼び出す

main でいきなり Driver を呼ぶのは不自然なので
せめて UseCase を介する形にする。

package com.example.springboot

import com.example.springboot.taro
import com.example.springboot.hanako

fun printAllName () {
    val taroName = taro.findName()
    val hanakoName = hanako.findName()
    println(taroName)
    println(hanakoName)
}
Enter fullscreen mode Exit fullscreen mode

Driver を集約した UseCase を作って

import com.example.springboot.printAllName


@SpringBootApplication
class SpringbootApplication

fun main(args: Array<String>) {
    runApplication<SpringbootApplication>(*args)
    printAllName()
}
Enter fullscreen mode Exit fullscreen mode

main で呼び出した。

2022-08-28 18:53:33.005  INFO 1430266 --- [           main] c.e.springboot.SpringbootApplicationKt   : Started SpringbootApplicationKt in 1.725 seconds (JVM running for 2.063)
Taro
Hanako
Enter fullscreen mode Exit fullscreen mode

これでもいける。

Kotlin のファイルの値のやりとり完全に理解した


まとめ

Kotlin/SpringBoot で CleanArchTechture っぽく

Driver -> Usecase -> main

って値をやり取りするなら

Driver で
package でアプリ名を明示
データクラスとゲッターを作成
データクラスを使って実際のデータとなるインスタンスを作成

Usecase でアプリ名からインスタンスを直接インポート
ユースケースに基づいた形にして fun で定義

Main で アプリ名から(Usecaseでつくった)関数を直接インポーと
main の中で呼び出す

こうする。

Top comments (0)

One Million Strong

Image description
We are an active and inclusive community of over one million registered creators, developers, and tech enthusiasts.

Create your account.