Slide 1

Slide 1 text

ͳʹ΋ߟ͑ͣClean ArchitectureͰExposedΛ࢖ͬͨΒ ύϑΥʔϚϯεͰ஍ࠈΛݟͨ࿩ UPSIDERɹਗ਼ਫݦ

Slide 2

Slide 2 text

SELF INTRODUCTION • ਗ਼ਫݦ(@kaonash_ ) • Lead Software Engineer & PdM & ΤϯδχΞ૊৫ߏங etc… @ UPSIDE R • Ҡॅ͍ͨ͠ܥΤϯδχΞʢ2022೥ɺ௕໺ʹҠॅ༧ఆʣ • Love: ୌ / Kitkat / Kotlin

Slide 3

Slide 3 text

એ఻ ௒࠾༻ͯ͠ΔͷͰ͚͍ͨͯͩ͘͢͞ʂ ʢϑϧϦϞʔτɾϑϧϑϨοΫεɾ෭ۀͰ΋OKʣ https://corporatecard.up-sider.jp/career

Slide 4

Slide 4 text

WHAT IS • Pure KotlinͷORMϑϨʔϜϫʔΫ • JetBrains੡ • 2छྨͷσʔλΞΫηεํ๏ • λΠϓηʔϑʹSQLͬΆ͘ΫΤϦΛ૊ΈཱͯΒΕΔDSL AP I • DAO(Data Access Object)Λ࢖͏DAO API

Slide 5

Slide 5 text

ࠓճαϯϓϧͰ࢖͏ςʔϒϧΛఆٛ object BillTable : LongIdTable("bills") { val name = varchar("name", 128 ) } object BillStatusTable : LongIdTable("bill_status") { val billId = reference("bill_id", BillTable ) val status = enumerationByName("status", 20, BillStatus::class ) val startAt = datetime("start_at").defaultExpression(CurrentDateTime() ) } billsςʔϒϧ΁ͷ֎෦ΩʔΛදݱ

Slide 6

Slide 6 text

DAO APIΛ࢖͏৔߹͸ENTITY(DAO)΋ఆٛ class BillEntity(id: EntityID) : LongEntity(id) { companion object : LongEntityClass(BillTable ) var name by BillTable.nam e val statusHistory by BillStatusEntity.referrersOn ( column = BillStatusTable.bill , cache = tru e ) } class BillStatusEntity(id: EntityID) : LongEntity(id) { companion object : LongEntityClass(BillStatusTable ) var bill by BillStatusTable.bil l var status by BillStatusTable.statu s var startAt by BillStatusTable.startA t } referrersOnΛ࢖͏͜ͱͰbill_statusςʔϒϧͷσʔλ΋ҰॹʹऔΕΔʂ

Slide 7

Slide 7 text

࣮ࡍͷσʔλऔಘํ๏ʢDAO APIʣ BillEntity.all().forEach { bill - > println("[${bill.id.value}] ${bill.name}" ) } billsςʔϒϧʹϨίʔυ͕2݅͋Δ৔߹ͷ݁Ռ ࣮ࡍͷΫΤϦ

Slide 8

Slide 8 text

͋Εɺbill_statusςʔϒϧ͸ɾɾɾʁ

Slide 9

Slide 9 text

஗ԆධՁͳͷͰɺ σʔλΛࢀর͢Δ࣌ʹॳΊͯऔΓʹߦ͘ ʢJOIN͍ͯ͠ΔΘ͚Ͱ͸ͳ͍ʣ BillEntity.all().forEach { bill - > println("[${bill.id.value}] ${bill.name}" ) println(bill.statusHistory.joinToString(separator = ",") { it.status.name } ) }

Slide 10

Slide 10 text

Exposed, ݡ͍ʂʂ

Slide 11

Slide 11 text

͕͔͠͠

Slide 12

Slide 12 text

CleanArchitectureͰ͜ͷػೳΛ ͦͷ··࢖ͬͯ஍ࠈΛݟͨ

Slide 13

Slide 13 text

CleanArchitectureͱ͸ʁ

Slide 14

Slide 14 text

ҎԼུ

Slide 15

Slide 15 text

େࣄͳ͜ͱɿϢʔεέʔε͸DBΛ஌Βͳ͍

Slide 16

Slide 16 text

Ϣʔεέʔε͸DBΛ஌Βͳ͍ ↓ ౰વɺExposedͷ͜ͱ΋஌Βͳ͍ ↓ Exposedʹґଘͨ͠Entity΋ฦͤͳ͍ ↓ σʔλऔಘͷࡍ͸ɺϢʔεέʔε͕஌ͬͯΔ ΦϒδΣΫτʹ٧Ίସ͑Δඞཁ͕͋Δ

Slide 17

Slide 17 text

٧Ίସ͑Ε͹͑͑΍ͳ͍ͷ data class Bill ( val name: String , val statusHistory: List ) data class BillStatus ( val status: String , val startAt: DateTime , ) ————————————————————————————————————————————————————————————————— — return BillEntity.all().map { bill - > Bill ( name = bill.name , statusHistory = bill.statusHistory.map { status - > BillStatus(status = status.status.name, startAt = status.startAt ) } ) }

Slide 18

Slide 18 text

ผͷΦϒδΣΫτʹ٧Ίସ͑Δ ↓ ͦͷ࣌఺Ͱ஗ԆධՁ͕࣮ߦ͞ΕΔ ↓ billsͷϨίʔυ਺͚ͩ bill_status΁ͷselect͕࣮ߦ͞ΕΔ ʢϏδωεϩδοΫ಺Ͱ࢖͏͔Ͳ͏͔ʹؔΘΒͣʣ

Slide 19

Slide 19 text

Ϋι஗͍ʢ࣮ࡍ͸΋ͬͱςʔϒϧߏ଄͕ෳࡶͳ ͷͰ͞Βʹ๲େͳ਺ͷSELECTจ͕͕͕ʣ

Slide 20

Slide 20 text

ղܾࡦɿEager LoadingΛ࢖͏ BillEntity.all().with(BillEntity::statusHistory).map { bill - > Bill ( name = bill.name , statusHistory = bill.statusHistory.map { status - > BillStatus(status = status.status.name, startAt = status.startAt ) } ) }.forEach { println(it) } ͜ͷ࣌఺Ͱ·ͱΊͯಡΈࠐΜͰ͘ΕΔʂ

Slide 21

Slide 21 text

஫ҙ఺ɿEntityఆٛ࣌ʹcache=trueʹ͓ͯ͠ ͔ͳ͍ͱ݁ہେྔͷΫΤϦ͕౤͛ΒΕΔ class BillEntity(id: EntityID) : LongEntity(id) { companion object : LongEntityClass(BillTable ) var name by BillTable.nam e val statusHistory by BillStatusEntity.referrersOn ( column = BillStatusTable.bill , cache = fals e ) } ͜ΕͰಉ͡ॲཧΛ࣮ߦ͢Δͱɾɾɾ

Slide 22

Slide 22 text

Thank you!!