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

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

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

3ae2d821854f9f6749f5ab265559f8ef?s=128

Yoshinobu Wakamatsu

May 23, 2016
Tweet

Transcript

  1. 4DBMBʹΑΔ%%%ϥΠΫͳ ϓϩμΫτ։ൃ  φΠϧɾϞόΠϧϑΝΫτϦʔ߹ಉษڧձ φΠϧגࣜձࣾएদܚ৴

  2. ࣗݾ঺հ एদܚ৴ φΠϧגࣜձࣾΤϯδχΞ  5XJUUFSɾ(JUIVCZTIOC ʢ΄ͱΜͲ׆ಈͯ͠·ͤΜʣ  ͜͜࠷ۙ͸4DBMBΛ΍͍ͬͯ·͢  WJNΛ࢖͍ͬͯ·͢

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

  4. ࠓճͷϓϩμΫτ։ൃͷ৚݅ •  Ϗδωε্ͷཁٻ͕ൺֱతଟذʹΘͨΔ  •  ؔ܎ऀͷϩʔϧ΋ଟ͍ –  ϓϩμΫτΦʔφʔɺΦϖϨʔλʔɺ؅ཧ෦໳ͳͲ •  ௕ظతͳӡ༻͕ݟࠐ·ΕΔ

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

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

    –  ϞσϧΛܕͰදݱ͢Δ͜ͱ͕Ͱ͖Δ
  7. υϝΠϯۦಈઃܭͱ͸

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

  9. ʁʁʁ

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

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

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

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

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

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

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

    •  ౷ҰޠኮͰද͞ΕͨϏδωε্ͷ஌ࣝΛϞσϧԽ͢Δ
  17. ౷ҰޠኮͰυϝΠϯͷ஌ࣝΛද͢ •  ̍ͭͷʮސ٬ʯ͸ෳ਺ͷʮϓϩδΣΫτʯΛ࣋ͭ •  ʮϢʔβʯ͸ʮϓϩδΣΫτʯ͝ͱ ʮݖݶʯΛมߋ͢Δ͜ͱ͕Ͱ͖Δ  ʮϢʔβʯͱ͸ʁʮݖݶʯͱ͸ʁͳͲ͕ ໌֬ʹͳ͍ͬͯΔͷͰɺ ʮϢʔβʯʹର͢ΔೝࣝͷζϨ΋ੜ͡ͳ͘ͳΔ

  18. υϝΠϯͷ஌ࣝΛϞσϧԽ 顧客 プロジェクト    ϞσϧΛͲ͏දݱ͢Δ͔ʁ͸ͦ͜·ͰॏཁͰ͸ͳ͍ Ͳ͏͍͏ϞσϧͰ͋Δ͔ʁΛ νʔϜͷதͰڞ༗Ͱ͖͍ͯΔ͜ͱ͕ॏཁ ϞσϧԽͷྫ

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

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

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

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

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

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

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

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

    –  4FSWJDF-PDBUPS –  ͳͲ •  ࠓճ͸%*Ͱ࣮૷Λఏڙˠ4DBMBͰ͸ʁ
  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 
  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ʣ
  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  ࣗ෼ܕΞϊςʔγϣϯͰUSBJU΁ͷґଘΛએݴ
  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  ͜͜Ͱ۩ମతͳ࣮૷Λ༩͍͑ͯΔ %* 
  31. 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ͷϝιου͕࢖͑Δ
  32. 4DBMBʹΑΔϞσϧͷ࣮૷

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

    –  7BMVF0CKFDU –  4FSWJDF –  3FQPTJUPSZ –  ͳͲ
  34. ྫɿ7BMVF0CKFDUͷ࣮૷ •  7BMVF0CKFDU –  ஋Λදݱ͢ΔΦϒδΣΫτ –  ஋ࣗମʹυϝΠϯ্ͷҙຯΛ࣋ͨͤΔ •  ྫɿՁ֨ʢ1SJDFʣ – 

    ʮԁʯʹؚ·ΕΔཁૉ͸ʮʯͱʮԁʯ –  1SJDFͷϞσϧ͸࣍ͷཁૉΛ࣋ͭ •  WBMVF •  DVSSFODZ+1:
  35. +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))
  36. 4DBMBʹΑΔ1SJDFΫϥε case class Price(value: Int, currency: Currency) // case class

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

    ʹΑΓ্هͷҰߦͰදݱग़དྷΔ // ͜Ε͚ͩͰ৽͍͠ΦϒδΣΫτΛੜ੒Մೳ Price(100, JPY) ϞσϧԽ͢Δ࣮૷ίετ͕ͱͯ΋খ͍͞
  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)
  39. ܕύϥϝʔλʹΑΔදݱ   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) ੫ൈɾ੫ࠐΛܕύϥϝʔλʹΑͬͯදݱ
  40. ܕύϥϝʔλʹΑΔදݱ   // ঎඼ͷՁ֨͸੫ࠐͰ͋Δ͜ͱ͕ܕͰදݱग़དྷΔ case class Product( name: String,

    price: Price[IncludingTax] ) // ঎඼ͷදݱྫ Product(“ίοϓຊ”, Price[IncludingTax](4968, JPY)) ͜ͷΑ͏ͳܗͰɺϞσϧΛ؆͔ܿͭܕ҆શʹ ίʔυʹམͱ͍ͯ͘͜͠ͱ͕Մೳ
  41. ·ͱΊ

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

    ࠷ऴతʹ͸ॏཁ
  43. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠