59
Doma2 with Kotlin 7/9( ) Doma 勉強会 in 東京 @yy_yank

Doma2 with Kotlin

  • Upload
    yy-yank

  • View
    467

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Doma2 with Kotlin

Doma2 with Kotlin

【7/9(土)】Doma勉強会 in 東京

@yy_yank

Page 2: Doma2 with Kotlin

          こいつです

          ・viとサクラエディタ好き

          ・小物SIer          ・JavaとKotlinが好き

ヤンク(@yy_yank)

自己紹介

Page 3: Doma2 with Kotlin

とつぜんですがワタクシ、

Doma2のコントリビューターなんです

Page 4: Doma2 with Kotlin
Page 5: Doma2 with Kotlin
Page 6: Doma2 with Kotlin

fix typo(ドヤッ)

Page 7: Doma2 with Kotlin

はい、すいませんでした

Page 8: Doma2 with Kotlin
Page 9: Doma2 with Kotlin

with

Page 10: Doma2 with Kotlin

with

Page 11: Doma2 with Kotlin

with

Page 12: Doma2 with Kotlin

with

Doma2、Kotlinでも使えるよ!!!の話

Page 13: Doma2 with Kotlin

(一応・・・) Doma とは

・めっちゃ良いORM・Domain Oriented Database MApping Framework・Annotation Processingでのコンパイルチェック素敵

・JRE 8以上でうごく

・2Way-SQL

※NASAではない

Page 14: Doma2 with Kotlin

(一応・・・) Kotlinとは

・JVM言語

・JetBrains製・Gradleスクリプトも書ける(ようになる)・Spring Initializrでも選択可能

・Null Safety(らしい)

・ScalaとGroovyのいいとこどり(らしい)

※ボクは昔のロゴの方が好き

Page 15: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 16: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 17: Doma2 with Kotlin

Entity@Entity(immutable = true)data class Person( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Int? = null, val name: Name, val age: Int?, val address: Address, @Version val version: Int = -1)

Page 18: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 19: Doma2 with Kotlin

Embeddable

package sample

import org.seasar.doma.Embeddable

@Embeddabledata class Address(val city: String, val street: String)

Page 20: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 21: Doma2 with Kotlin

package sample

import org.seasar.doma.Domain

@Domain(valueType = String::class)data class Name(val value: String)

Domain

Page 22: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 23: Doma2 with Kotlin

Daopackage sample

import org.seasar.doma.*import org.seasar.doma.jdbc.Result

@Dao(config = AppConfig::class)interface PersonDao { @Script fun create() @Script fun drop() @Select fun selectById(@ParameterName("id") id:Int): Person @Insert fun insert(person: Person): Result<Person> @Update fun update(person: Person): Result<Person> @Delete fun delete(person: Person): Result<Person>}

Page 24: Doma2 with Kotlin

@Dao(config = AppConfig::class)interface PersonDao {

@Script fun create() @Script fun drop()

Dao

Page 25: Doma2 with Kotlin

@Select fun selectById(@ParameterName("id") id:Int): Person

@Insert fun insert(person: Person): Result<Person>

Dao

Page 26: Doma2 with Kotlin

@Update fun update(person: Person): Result<Person> @Delete fun delete(person: Person): Result<Person>

Dao

Page 27: Doma2 with Kotlin

特に言うことなし

Page 28: Doma2 with Kotlin

(別にKotlinじゃなくても)コードがシンプルでキレイ

Page 29: Doma2 with Kotlin

あえていうならgettter/setterが消えてLombokぐらいの活躍はしている!

Page 30: Doma2 with Kotlin

コードは全て

https://github.com/domaframework/kotlin-sample

から拝借しました!

Page 31: Doma2 with Kotlin

DomaのKotlinサポートのページに詳しく指針が出ているので迷うことなし!

http://doma.readthedocs.io/ja/stable/kotlin-support/

Page 32: Doma2 with Kotlin

Kotlinサポート

ざっくり言うと

・data class使おう

・イミュータブルなクラスにしよう

・コンストラクタは1つだけにしよう

・kaptにハマったら@DaoはJavaにするなど

混在させるのもアリ

Page 33: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 34: Doma2 with Kotlin

・kaptはJSR269をKotlinでも使えるようにしてくれる

コンパイルツール(個人的にはaptってやめて欲しい名前)・Doma 2のAnnotation Processingに対応している

・以前のツラミ->backpaper0さんの資料

http://backpaper0.github.io/ghosts/kotlin-doma.html・Kotlinの最新version 1.0.3で改善されたりしてる

・Doma 2としてもサポートしてくれていて

ツラミ減

kapt

Page 35: Doma2 with Kotlin

kaptのツラミ(backpaper0さん資料抜粋)

Page 36: Doma2 with Kotlin

・少なくとも1つ注釈処理のつくJavaクラスが必要問題

→解消されたっぽい!

・arg0、arg1問題

→@ParameterNameで解消

多分これのおかげです(よね?)

kaptのツラミ

Page 37: Doma2 with Kotlin

GitHubのissueにもあったのでこれで間違いなさそう

・issuehttps://github.com/domaframework/doma/issues/129

・javadochttp://doma.seasar.org/apidocs/org/seasar/doma/ParameterName.html

kaptのツラミ

Page 38: Doma2 with Kotlin

@Select fun selectById(@ParameterName("id") id:Int): Person

kaptのツラミ

@ParameterNameによりarg1、arg2...問題が解消された

Page 39: Doma2 with Kotlin

アジェンダ

Doma2 ・Entity ・Embeddable ・Domain ・DaoKotlin ・kapt ・java.util.stream.Stream

Page 40: Doma2 with Kotlin

・KotlinはJDK6対応

・JDK8対応はまだらしい

・でもボクはJavaでいう

@SelectR select(Integer id, Function<Stream<T>, R> mapper)したい!!!!

java.util.stream.Stream

Page 41: Doma2 with Kotlin

・KotlinはJDK6対応

・JDK8対応はまだらしい

・でもボクはJavaでいう

@SelectR select(Integer id, Function<Stream<T>, R> mapper)したい!!!!

ということで検証

java.util.stream.Stream

Page 42: Doma2 with Kotlin

 結論:出来た(なんでか分かんないけど普通に動くっぽい)

Page 43: Doma2 with Kotlin

 結論:出来た(でもKotlinからStream API使いにくい)

Page 44: Doma2 with Kotlin

@Select(strategy = SelectType.STREAM) fun selectById( @ParameterName("id") id:Int, func :java.util.function.Function<java.util.stream.Stream<Person>, Name>): Name

Dao

Page 45: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, Function { it.map {it.name}.findFirst().orElseThrow { Exception("あってほしい!") } }) Assert.assertEquals(Name("SMITH"), personName) }}

Test Code

Page 46: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, Function { it.map {it.name}.findFirst().orElseThrow { Exception("あってほしい!") } }) Assert.assertEquals(Name("SMITH"), personName) }}

Test CodeSAM(Single Abstract Method)

Page 47: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, Function { it.map {it.name}.findFirst().orElseThrow { Exception("あってほしい!") } }) Assert.assertEquals(Name("SMITH"), personName) }}

Test CodeFunction#applyをFunction{}とかける

Page 48: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, Function { it.map {it.name}.findFirst().orElseThrow { Exception("あってほしい!") } }) Assert.assertEquals(Name("SMITH"), personName) }}

Test Codeit = Stream<Person>stream -> streamと思ってくれれば。

Page 49: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, Function { it.map {it.name}.findFirst().orElseThrow { Exception("あってほしい!") } }) Assert.assertEquals(Name("SMITH"), personName) }}

Test Code Javaで書くならstream -> stream.map(person -> person.getName()).findFirst().orElseThrow(()-> throw new Exception(“あってほしい! ”)

Page 50: Doma2 with Kotlin

うん。。

Page 51: Doma2 with Kotlin

でもどうせならKotlin的には。。

Page 52: Doma2 with Kotlin

@Select(strategy = SelectType.STREAM) fun selectById( @ParameterName("id") id:Int, func : (person : Person) -> Name): Name

Dao

Page 53: Doma2 with Kotlin

@Select(strategy = SelectType.STREAM) fun selectById( @ParameterName("id") id:Int, func : (person : Person) -> Name): Name

Dao

ここをこうして

Page 54: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, {it.name}) Assert.assertEquals(Name("SMITH"), personName) }}

Test Code

Page 55: Doma2 with Kotlin

@Test fun test2() { tm.required { val personName = dao.selectById(1, {it.name}) Assert.assertEquals(Name("SMITH"), personName) }}

Test Codeこうじゃ

Page 56: Doma2 with Kotlin

エラー: [DOMA4247] @Selectのstrategy要素に

SelectStrategyType.STREAMを設定した場合、

Function型のパラメータが必要です。 at sample.PersonDao.selectByIdエラー1個

Page 57: Doma2 with Kotlin

Domaさん賢い!!!!

#そこじゃない

Page 58: Doma2 with Kotlin

・Doma2はKotlinでも普通に使える

・手軽な感じでコード書くには良いのかも

・KotlinからStream APIは使いにくい

(あんまりDoma2関係ない)・でもJavaで使ってもDoma2は充分気持ち良い

・お世辞じゃなくDoma2は素敵

・対応の早さ、柔軟さも素敵

まとめ

Page 59: Doma2 with Kotlin

Domaは素敵!!!!