Upgrade to Pro — share decks privately, control downloads, hide ads and more …

秩序あるKotlinの拡張 / Orderly Kotlin Extensions

秩序あるKotlinの拡張 / Orderly Kotlin Extensions

Masatoshi Kubode

June 23, 2021
Tweet

More Decks by Masatoshi Kubode

Other Decks in Programming

Transcript

  1. ©2021 Wantedly, Inc. w ෳ਺ͷGSBHNFOUΛ͏·͘อଘ͍ͨ͠ fragmentͱ͸ • σʔλϞσϧΛஅยԽͨ͠΋ͷ • ಉ͡ϞσϧΛࢦ͕͢৘ใྔ͕ҧ͏

    • ҰཡͰ͸࠷খݶ • ৄࡉͰ͸࠷େݶ Ϟσϧ͸ಉ͡ͳͷͰɺ ಉ͡ςʔϒϧʹอଘ͍ͨ͠ ΍Γ͍ͨ͜ͱ fragment ProfileMini on Profile { userId name avatarUrl } fragment ProfileDetail on Profile { userId name avatarUrl coverImageUrl introduction tagline statement # ... all fields }
  2. ©2021 Wantedly, Inc. w ୯७ʹ֦ுΛॻ͍ͨ Ͳ͏͔ͨ͠ʁ fun Db.merge(fragment: ProfileMiniFragment) {

    profilePageQueries.upsertMiniFragment( userId = UserId(fragment.userId), // ... ) }
  3. ©2021 Wantedly, Inc. ໰୊఺ fun Db.merge(fragment: ProfileMiniFragment) { profilePageQueries.upsertMiniFragment( userId

    = UserId(fragment.userId), // ... ) } ໊લʹറΓ͕ͳ͍ɺ໎͏ mergeDetail, saveͱ͔ϒϨ΍͍͢ Ҿ਺ʹറΓ͕ͳ͍ interfaceͰ੍໿Λ͍࣋ͨͤͨʂ
  4. ©2021 Wantedly, Inc. PCKFDU಺֦ுؔ਺ object Foo { fun String.hello() {

    println("Hello, $this") } } object಺ʹ֦ுؔ਺Λ࡞Δ ී௨ͷ֦ுؔ਺ͷΑ͏ʹར༻Ͱ͖Δ import Foo.hello fun main() { "World".hello() }
  5. ©2021 Wantedly, Inc. ղܾख๏ internal interface Merger<FragmentT> { fun Db.merge(fragment:

    FragmentT) } internal object ProfileMiniFragmentMerger : Merger<ProfileMiniFragment> { override fun Db.merge(fragment: ProfileMiniFragment) { profilePageQueries.upsertFragment( userId = UserId(fragment.userId), // ... ) } } // Repository fun fetchMiniList() { val miniList = api.query(GetProfileMiniListQuery()) db.transaction { miniList.forEach { db.merge(it) } } } internal object ProfileDetailFragmentMerger : Merger<ProfileDetailFragment> { override fun Db.merge(fragment: ProfileDetailFragment) { profilePageQueries.upsertFragment( userId = UserId(fragment.userId), // ... ) } } // Repository fun fetchDetail() { val detail = api.query(GetProfileDetailQuery()) db.merge(detail) } ֦ுؔ਺Λ࣋ͭ object΁࣮૷ object಺֦ுؔ਺ΛݺΜͰ͍Δ