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

ScalaによるDDDライクなプロダクト開発

 ScalaによるDDDライクなプロダクト開発

2016/5/23 モバイルファクトリーさんとの勉強会で話したスライド

Yoshinobu Wakamatsu

May 23, 2016
Tweet

More Decks by Yoshinobu Wakamatsu

Other Decks in Technology

Transcript

  1. 4DBMBʹΑΔ%%%ϥΠΫͳ
    ϓϩμΫτ։ൃ

    φΠϧɾϞόΠϧϑΝΫτϦʔ߹ಉษڧձ
    φΠϧגࣜձࣾएদܚ৴

    View full-size slide

  2. ࣗݾ঺հ
    एদܚ৴
    φΠϧגࣜձࣾΤϯδχΞ

    5XJUUFSɾ(JUIVCZTIOC
    ʢ΄ͱΜͲ׆ಈͯ͠·ͤΜʣ

    ͜͜࠷ۙ͸4DBMBΛ΍͍ͬͯ·͢

    WJNΛ࢖͍ͬͯ·͢





    View full-size slide

  3. ࠓճͷϓϩμΫτ։ൃʹ͍ͭͯ

    View full-size slide

  4. ࠓճͷϓϩμΫτ։ൃͷ৚݅
    •  Ϗδωε্ͷཁٻ͕ൺֱతଟذʹΘͨΔ

    •  ؔ܎ऀͷϩʔϧ΋ଟ͍
    –  ϓϩμΫτΦʔφʔɺΦϖϨʔλʔɺ؅ཧ෦໳ͳͲ
    •  ௕ظతͳӡ༻͕ݟࠐ·ΕΔ
    ͜ΕΒͷ৚݅Λߟྀͯ͠
    υϝΠϯۦಈઃܭʢ%%%ʣΛ࠾༻

    View full-size slide

  5. ࠓճͷʮυϝΠϯۦಈઃܭʯʹ͍ͭͯ
    •  υϝΠϯۦಈઃܭͷίϯηϓτΛऔΓೖΕͯɺ
    ઃܭ΍։ൃΛ࣮ࢪ
    •  ඞͣ͠΋ݪଇ௨ΓͰ͸ͳ͍ܗͰ
    υϝΠϯۦಈઃܭͷίϯηϓτΛ࠾༻

    View full-size slide

  6. ͳͥ4DBMBͰ%%%ͳͷ͔ʁ
    •  ࣮༻తͳγϯλοΫεͷ؆ܿ͞
    –  ϞσϧΛ࣮૷ʹམͱ͢ίετ͕௿͍
    •  ܕγεςϜ͕ڧྗ
    –  ੩తܕ෇͚ʹΑΔܕ҆શੑ͸
    ϏδωεϩδοΫ͕ෳࡶͳέʔεͰޮՌత
    –  ϞσϧΛܕͰදݱ͢Δ͜ͱ͕Ͱ͖Δ

    View full-size slide

  7. υϝΠϯۦಈઃܭͱ͸

    View full-size slide

  8. υϝΠϯۦಈઃܭͱ͸
    ιϑτ΢ΣΞͷઃܭख๏Ͱ͋ΓɺʮෳࡶͳυϝΠ
    ϯͷઃܭ͸ɺϞσϧϕʔεͰߦ͏΂͖ʯͰ͋Γɺ
    ·ͨʮେ൒ͷιϑτ΢ΣΞϓϩδΣΫτͰ͸ɺγ
    εςϜΛ࣮૷͢ΔͨΊͷಛఆͷٕज़Ͱ͸ͳ͘ɺυ
    ϝΠϯͦͷ΋ͷͱυϝΠϯͷϩδοΫʹয఺Λஔ
    ͘΂͖ʯͰ͋Δͱ͢Δ

    CZ8JLJQFEJB

    View full-size slide

  9. ΋͏গ͠Θ͔Γ΍͘͢

    View full-size slide

  10. υϝΠϯۦಈͰ͸ͳ͍ઃܭͷྫ
      ࣮ݱ͍ͨ͠ػೳɾݟͨ໨ΛܾΊΔ
      ٕज़બఆ΍ઃܭΛߦ͍ɺ
    ۩ମతͳ࣮૷ٕज़ͰػೳΛ࣮ݱ͢Δ
    ͜Ε͸υϝΠϯͷຊ࣭͕Կͳͷ͔ʁΛ໌Β͔ʹ͓ͯ͠Βͣɺ
    υϝΠϯۦಈઃܭͰ͸ͳ͍

    View full-size slide

  11. υϝΠϯۦಈઃܭ͸ʁ
      ϏδωεྖҬͷϢϏΩλεݴޠ
    ʢ౷ҰݴޠʣΛߏங
      ʮ࣮ݱ͍ͨ͜͠ͱʯʮ࣮ݱ͢Δ͜ͱʯΛ
    ϢϏΩλεݴޠͰදݱ
      ϢϏΩλεݴޠͰදݱ͞ΕͨυϝΠϯϞσϧΛ
    ۩ମతͳ࣮૷ٕज़Ͱ࣮૷͢Δ

    View full-size slide

  12. ϢϏΩλεݴޠʹΑΔ
    υϝΠϯϞσϧ

    View full-size slide

  13. ϓϩμΫτ։ൃʹ͓͚ΔݴޠͷִͨΓ
    ϓϩμΫτΦʔφʔ
    ։ൃऀ
    ΫϥΠΞϯτ ΫϥΠΞϯτ

    View full-size slide

  14. ϓϩμΫτ։ൃʹ͓͚ΔݴޠͷִͨΓ
    ϓϩμΫτΦʔφʔ
    ։ൃऀ
    ΫϥΠΞϯτ ΫϥΠΞϯτ
    ʮΫϥΠΞϯτʯ͸ಉ͡ҙຯʁ

    View full-size slide

  15. ϢϏΩλεݴޠͰ༻ޠΛ౷Ұ
    ༻ޠͷఆٛΛ໌֬ʹ͢Δ
    ސ٬
    ͓٬༷
    Ϗδωε্ͷΫϥΠΞϯτ
    ΫϥΠΞϯτ
    αʔόɾΫϥΠΞϯτϞσϧͷ
    ΫϥΠΞϯτʢٕज़༻ޠʣ
    •  ౷ҰͷޠኮͰཁ݅ɾ࢓༷ΛܾΊΔ
    •  ౷ҰޠኮͰද͞ΕͨϏδωε্ͷ஌ࣝΛϞσϧԽ͢Δ

    View full-size slide

  16. ౷ҰޠኮͰυϝΠϯͷ஌ࣝΛද͢
    •  ̍ͭͷʮސ٬ʯ͸ෳ਺ͷʮϓϩδΣΫτʯΛ࣋ͭ
    •  ʮϢʔβʯ͸ʮϓϩδΣΫτʯ͝ͱ
    ʮݖݶʯΛมߋ͢Δ͜ͱ͕Ͱ͖Δ

    ʮϢʔβʯͱ͸ʁʮݖݶʯͱ͸ʁͳͲ͕
    ໌֬ʹͳ͍ͬͯΔͷͰɺ
    ʮϢʔβʯʹର͢ΔೝࣝͷζϨ΋ੜ͡ͳ͘ͳΔ

    View full-size slide

  17. υϝΠϯͷ஌ࣝΛϞσϧԽ
    顧客 プロジェクト


    ϞσϧΛͲ͏දݱ͢Δ͔ʁ͸ͦ͜·ͰॏཁͰ͸ͳ͍
    Ͳ͏͍͏ϞσϧͰ͋Δ͔ʁΛ
    νʔϜͷதͰڞ༗Ͱ͖͍ͯΔ͜ͱ͕ॏཁ
    ϞσϧԽͷྫ

    View full-size slide

  18. υϝΠϯۦಈΛ࣮ݱ͢Δ
    ΞʔΩςΫνϟ

    View full-size slide

  19. ΞʔΩςΫνϟ΁ͷཁٻ
    •  υϝΠϯϞσϧ͕ओମʹͳΔ΂͖
    –  Ϟσϧ͕ಛఆͷ࣮૷ٕज़ʹґଘͯ͠͸͍͚ͳ͍
    •  ٕज़తͳؔ৺ࣄ͸υϝΠϯͷؔ৺ࣄͱ
    ෼཭͞Ε͍ͯΔ΂͖

    View full-size slide

  20. ҰൠతͳϨΠϠʔԽΞʔΩςΫνϟ
    Infrastructure Layer
    UI & Presenta1on Layer
    Applica1on Layer
    Domain Layer

    View full-size slide

  21. ҰൠతͳϨΠϠʔԽΞʔΩςΫνϟ
    Infrastructure Layer
    UI & Presenta1on Layer
    Applica1on Layer
    Domain Layer
    υϝΠϯ૚͕ΠϯϑϥετϥΫνϟ૚
    ʢ۩ମతͳ࣮૷ٕज़ʣʹґଘͯ͠͠·͍ͬͯΔ

    View full-size slide

  22. ࠾༻ͨ͠ΞʔΩςΫνϟ
    Domain Layer
    Infrastructure Layer
    UI & Presenta1on Layer
    Applica1on Layer

    View full-size slide

  23. ࠾༻ͨ͠ΞʔΩςΫνϟ
    Domain Layer
    Infrastructure Layer
    UI & Presenta1on Layer
    Applica1on Layer
    υϝΠϯ૚Λ࠷ԼҐͷϨΠϠʔ΁
    ΠϯϑϥετϥΫνϟ૚ͷར༻͕ඞཁͳՕॴ͸
    ͦͷΠϯλʔϑΣΠεΛఏڙ

    View full-size slide

  24. ࠾༻ͨ͠ΞʔΩςΫνϟ
    Domain Layer
    Infrastructure Layer
    UI & Presenta1on Layer
    Applica1on Layer
    ΠϯϑϥετϥΫνϟ૚͸࠷্ҐͷϨΠϠʔʹ͠
    ԼҐϨΠϠʔͷΠϯλʔϑΣΠεΛ࣮૷

    View full-size slide

  25. ΞʔΩςΫνϟΛ࣮ݱ͢Δʹ͸
    •  ηύϨʔτΠϯλʔϑΣΠε΁
    ۩ମతͳ࣮૷Λ༩͑Δඞཁ͕͋Δ
    •  ޙ͔Β࣮૷Λ༩͑Δखஈ
    –  %FQFOEFODZ*OKFDUJPO %*

    –  4FSWJDF-PDBUPS
    –  ͳͲ
    •  ࠓճ͸%*Ͱ࣮૷Λఏڙˠ4DBMBͰ͸ʁ

    View full-size slide

  26. 4DBMBʹΑΔ੩త%*
    trait RepositoryRegistry {
    val fooRepository: Repo
    }
    // fooRepositoryΛ࢖͏Ϋϥε
    trait BarService {
    this: RepositoryRegistry =>
    def doSomething = {
    fooRepository.fetch
    }
    }
    // ࣮૷Λ࣋ͭτϨΠτ
    trait RepositoryRegistryImpl {
    // Repo͸࣮૷
    val fooRepository = Repo
    }
    // ࣮૷Λಋೖ
    class BerServiceImpl
    extends RepositoryRegistryImpl

    View full-size slide

  27. 4DBMBʹΑΔ੩త%*
    trait RepositoryRegistry {
    val fooRepository: Repo
    }
    // fooRepositoryΛ࢖͏Ϋϥε
    trait BarService {
    this: RepositoryRegistry =>
    def doSomething = {
    fooRepository.fetch
    }
    }
    // ࣮૷Λ࣋ͭτϨΠτ
    trait RepositoryRegistryImpl {
    // Repo͸࣮૷
    val fooRepository = Repo
    }
    // ࣮૷Λಋೖ
    class BerServiceImpl
    extends RepositoryRegistryImpl

    ͜Ε͸ͨͩͷΠϯλʔϑΣΠεʢUSBJUʣ

    View full-size slide

  28. 4DBMBʹΑΔ੩త%*
    trait RepositoryRegistry {
    val fooRepository: Repo
    }
    // fooRepositoryΛ࢖͏Ϋϥε
    trait BarService {
    this: RepositoryRegistry =>
    def doSomething = {
    fooRepository.fetch
    }
    }
    // ࣮૷Λ࣋ͭτϨΠτ
    trait RepositoryRegistryImpl {
    // Repo͸࣮૷
    val fooRepository = Repo
    }
    // ࣮૷Λಋೖ
    class BerServiceImpl
    extends RepositoryRegistryImpl

    ࣗ෼ܕΞϊςʔγϣϯͰUSBJU΁ͷґଘΛએݴ

    View full-size slide

  29. 4DBMBʹΑΔ੩త%*
    trait RepositoryRegistry {
    val fooRepository: Repo
    }
    // fooRepositoryΛ࢖͏Ϋϥε
    trait BarService {
    this: RepositoryRegistry =>
    def doSomething = {
    fooRepository.fetch
    }
    }
    // ࣮૷Λ࣋ͭτϨΠτ
    trait RepositoryRegistryImpl {
    // Repo͸࣮૷
    val fooRepository = Repo
    }
    // ࣮૷Λಋೖ
    class BerServiceImpl
    extends RepositoryRegistryImpl

    ͜͜Ͱ۩ମతͳ࣮૷Λ༩͍͑ͯΔ %*

    View full-size slide

  30. 4DBMBʹΑΔ੩త%*
    trait RepositoryRegistry {
    val fooRepository: Repo
    }
    // fooRepositoryΛ࢖͏Ϋϥε
    trait BarService {
    this: RepositoryRegistry =>
    def doSomething = {
    fooRepository.fetch
    }
    }
    // ࣮૷Λ࣋ͭτϨΠτ
    trait RepositoryRegistryImpl {
    // Repo͸࣮૷
    val fooRepository = Repo
    }
    // ࣮૷Λಋೖ
    class BerServiceImpl
    extends RepositoryRegistryImpl

    ࣮૷Λಋೖ͢Δલͷஈ֊Ͱ
    ґଘΛએݴͨ͠USBJUͷϝιου͕࢖͑Δ

    View full-size slide

  31. 4DBMBʹΑΔϞσϧͷ࣮૷

    View full-size slide

  32. ϞσϧΛઃܭɾ࣮૷ʹམͱ͢
    •  υϝΠϯ૚ͷ࣮૷
    •  υϝΠϯۦಈઃܭʹ͓͍ͯఆٛ͞Εͨಓ۩Λ࢖
    ͍4DBMBͷίʔυͰදݱ͢Δ
    •  %%%ͷಓ۩
    –  &OUJUZ
    –  7BMVF0CKFDU
    –  4FSWJDF
    –  3FQPTJUPSZ
    –  ͳͲ

    View full-size slide

  33. ྫɿ7BMVF0CKFDUͷ࣮૷
    •  7BMVF0CKFDU
    –  ஋Λදݱ͢ΔΦϒδΣΫτ
    –  ஋ࣗମʹυϝΠϯ্ͷҙຯΛ࣋ͨͤΔ
    •  ྫɿՁ֨ʢ1SJDFʣ
    –  ʮԁʯʹؚ·ΕΔཁૉ͸ʮʯͱʮԁʯ
    –  1SJDFͷϞσϧ͸࣍ͷཁૉΛ࣋ͭ
    •  WBMVF
    •  DVSSFODZ+1:

    View full-size slide

  34. +BWBʹΑΔ1SJDFΫϥε
    public class Price {
    public int value; // Ձ֨ͷ஋
    public Currency currency; // ௨՟
    Price(int value, Currency currency) {
    this.value = value;
    this.currency = currency;
    }
    }
    // PriceΦϒδΣΫτͷੜ੒
    new Price(100, new Currency(JPY))

    View full-size slide

  35. 4DBMBʹΑΔ1SJDFΫϥε
    case class Price(value: Int, currency: Currency)
    // case class ʹΑΓ্هͷҰߦͰදݱग़དྷΔ
    // ͜Ε͚ͩͰ৽͍͠ΦϒδΣΫτΛੜ੒Մೳ
    Price(100, JPY)

    View full-size slide

  36. 4DBMBʹΑΔ1SJDFΫϥε
    case class Price(value: Int, currency: Currency)
    // case class ʹΑΓ্هͷҰߦͰදݱग़དྷΔ
    // ͜Ε͚ͩͰ৽͍͠ΦϒδΣΫτΛੜ੒Մೳ
    Price(100, JPY)
    ϞσϧԽ͢Δ࣮૷ίετ͕ͱͯ΋খ͍͞

    View full-size slide

  37. ܕύϥϝʔλʹΑΔදݱ

    trait Tax
    case object IncludingTax extends Tax // ੫ࠐ
    case object ExcludingTax extends Tax // ੫ൈ
    // ܕύϥϝʔλʢδΣωϦοΫʣʹΑͬͯ΋දݱՄೳ
    // A͸Taxͷαϒܕ
    case class Price[A <: Tax](value: Int, currency: Currency)
    // 100ԁʢ੫ࠐʣͷදݱ
    Price[IncludingTax](100, JPY)

    View full-size slide

  38. ܕύϥϝʔλʹΑΔදݱ

    trait Tax
    case object IncludingTax extends Tax // ੫ࠐ
    case object ExcludingTax extends Tax // ੫ൈ
    // ܕύϥϝʔλʢδΣωϦοΫʣʹΑͬͯ΋දݱՄೳ
    // A͸Taxͷαϒܕ
    case class Price[A <: Tax](value: Int, currency: Currency)
    // 100ԁʢ੫ࠐʣͷදݱ
    Price[IncludingTax](100, JPY)
    ੫ൈɾ੫ࠐΛܕύϥϝʔλʹΑͬͯදݱ

    View full-size slide

  39. ܕύϥϝʔλʹΑΔදݱ

    // ঎඼ͷՁ֨͸੫ࠐͰ͋Δ͜ͱ͕ܕͰදݱग़དྷΔ
    case class Product(
    name: String,
    price: Price[IncludingTax]
    )
    // ঎඼ͷදݱྫ
    Product(“ίοϓຊ”, Price[IncludingTax](4968, JPY))
    ͜ͷΑ͏ͳܗͰɺϞσϧΛ؆͔ܿͭܕ҆શʹ
    ίʔυʹམͱ͍ͯ͘͜͠ͱ͕Մೳ

    View full-size slide

  40. 4DBMBʹΑΔ%%%͸Ͳ͏ͳͷʁ
    •  υϝΠϯۦಈઃܭʹ͓͚Δ4DBMBͷ࠾༻͸
    ʢ࣮૷ίετɾදݱྗͷ؍఺ͰʣϝϦοτ͕େ͖͍
    •  ͨͩ͠ɺͲͷϓϩμΫτʹ΋ద͍ͯ͠ΔΘ͚Ͱ͸ͳ͍
    –  ྫ͑͹ɺϓϩτλΠϓతͳϓϩμΫτʹ͸ෆ޲͖
    •  ϓϩμΫτʹదͨ͠ઃܭख๏ΛऔΕ͍ͯΔ͔͕
    ࠷ऴతʹ͸ॏཁ

    View full-size slide

  41. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide