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

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

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

E505897a79eede1a676f92740261e8f8?s=128

Masatoshi Kubode

June 23, 2021
Tweet

Transcript

  1. ©2021 Wantedly, Inc. டং͋ΔKotlinͷ֦ு potatotips #74 2021/06/23 ٱอग़խढ़

  2. ©2021 Wantedly, Inc. XXXXBOUFEMZDPNJELVCPEF

  3. ©2021 Wantedly, Inc. w JOUFSGBDFͱPCKFDU಺֦ுؔ਺Ͱɺ֦ுʹடং Λ΋ͨͤΔ࿩ 5-%3

  4. ©2021 Wantedly, Inc. ܦҢ

  5. ©2021 Wantedly, Inc. w ,..ʹ(SBQI2-Λಋೖ ܦҢ

  6. ©2021 Wantedly, Inc. w 3FQPTJUPSZͰϑΣονͨ͠σʔλΛอଘ͍ͨ͠ ΍Γ͍ͨ͜ͱ อଘͨ͠Β GetUseCaseʹ ௨஌͞ΕΔ

  7. ©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 }
  8. ©2021 Wantedly, Inc. w ECNFSHF GSBHNFOU Έ͍ͨʹॻ͖͍ͨ w 42-%FMJHIUʹ͸ࠩΛٵऩͯ͠ɺ͍͍ײ͡ʹอଘ ͢Δػೳ͸ͳ͍

    ΍Γ͍ͨ͜ͱ
  9. ©2021 Wantedly, Inc. w ୯७ʹ֦ுΛॻ͍ͨ Ͳ͏͔ͨ͠ʁ fun Db.merge(fragment: ProfileMiniFragment) {

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

    = UserId(fragment.userId), // ... ) } ໊લʹറΓ͕ͳ͍ɺ໎͏ mergeDetail, saveͱ͔ϒϨ΍͍͢ Ҿ਺ʹറΓ͕ͳ͍ interfaceͰ੍໿Λ͍࣋ͨͤͨʂ
  11. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2021 Wantedly, Inc. JOUFSGBDF PCKFDU಺֦ுؔ਺

  12. ©2021 Wantedly, Inc. PCKFDU಺֦ுؔ਺ object Foo { fun String.hello() {

    println("Hello, $this") } } object಺ʹ֦ுؔ਺Λ࡞Δ ී௨ͷ֦ுؔ਺ͷΑ͏ʹར༻Ͱ͖Δ import Foo.hello fun main() { "World".hello() }
  13. ©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಺֦ுؔ਺ΛݺΜͰ͍Δ
  14. ©2021 Wantedly, Inc. w JOUFSGBDFͱPCKFDU಺֦ுؔ਺Ͱɺ֦ுʹடংΛ ΋ͨͤΔ͜ͱ͕Ͱ͖Δ w ࠜຊతʹ͸ίʔυੜ੒ͱ͔ͰϘΠϥʔϓϨʔτΛ ݮΒ͢΂͖ʜ ·ͱΊ

  15. ϖʔδλΠτϧ ϖʔδαϒλΠτϧ ©2021 Wantedly, Inc. 🙇