Slide 1

Slide 1 text

1 at Mt.Fuji 2014 ࣮ફ 4DBMBͰ %%% Practice DDD with Scala

Slide 2

Slide 2 text

“ ” 2 - Osaka, Japan. - Java 17 years, Scala 2 years, and more… - Works as Software Architect, Developer for Enterprise system. - Currently, Developing a WEB Services using Scala and DDD on AWS. - I like Cars, Formula 1 and Climbing. Yohei TSUJI ” Twitter / GitHub / Qiita @crossroad0201 ΑΖ͓͘͠ئ͍͠·͢ɻ Freelance Programmer I am...

Slide 3

Slide 3 text

3 : 02 20 1 /- 1/221/ 2 /. +BQBOFTFWFSTJPOJTBWBJMBCMF

Slide 4

Slide 4 text

This talk… 4 For such Engineers… - Practicing DDD, interested in Scala. - Using Scala, interested in DDD. - No experience with Scala and DDD. But want to use it if it's good. - Practicing DDD with Scala. Want to reference. 4DBMBͰ %%%Λ࣮ફ͍ͨ͠ΤϯδχΞͷํʹʂ

Slide 5

Slide 5 text

Contents 5 6 4 3 2 5 1 What's DDD? DDD with Scala Code Style Architecture Implement Components Practice DDD ໨࣍

Slide 6

Slide 6 text

6 1. What's DDD? %%%ͬͯʁ

Slide 7

Slide 7 text

In a nutshell… 7 Principles for developing software that's easy to change using Object-Oriented design. - Conversation using common "Ubiquitous language". - Split "Domain Context" by business concern. - Define the Components and their responsibilities. …etc มߋ͕༰қͳιϑτ΢ΣΞΛ࡞ΔͨΊͷݪଇूɻ

Slide 8

Slide 8 text

and… 8 Writing the Domain knowledge in the program code using Domain language. υϝΠϯͷ஌ࣝΛɺυϝΠϯͷݴ༿Ͱɺίʔυʹॻ͘ɻ

Slide 9

Slide 9 text

Components in DDD1 / 2 Aggregate Units that must be consistent in the domain. Factory Create a new Entity. Entity An object that need to be identified by unique ID in the Aggregate. Repository Persistence a Entity. Value Object An object that does not need to be identified in the Aggregate. Domain Service Applying the Business rule to the Domain model. 9 %%%Ͱఆٛ͞ΕΔίϯϙʔωϯτ ͦͷ̍ɻ

Slide 10

Slide 10 text

CQRSCommand Query Responsibility Segregation Components in DDD2 / 2 Domain Event Business events occurring in the Aggregate. Event Subscriber Receive a Domain Event asynchronously, and update the Query Model. Event Publisher Publish a Domain Event to the Event queue. Query Processor Execute query to the Query Model. Query Model A read only view for the Domain model. Can contains multiple aggregates in the Query model. Application Service Implementing Use-Case using the Domain models. 10 %%%Ͱఆٛ͞ΕΔίϯϙʔωϯτ ͦͷ̎ɻ

Slide 11

Slide 11 text

Anyway… 11 Domain-Driven DESIGN by Eric Evans Implementing Domain-Driven DESIGN by Vaughn Vernon But, It's necessary trial and error to practice DDD with functional programming language.... ؔ਺ܕݴޠͰͷ%%%ͷ࣮ફͰ͸ࢼߦࡨޡ͕ඞཁʜ

Slide 12

Slide 12 text

12 2. DDD with Scala 4DBMBͱ %%%

Slide 13

Slide 13 text

DDD and Scala are… 13 %%%ͱ 4DBMB͸ʜ

Slide 14

Slide 14 text

Good chemistry!! 14 ͱͯ΋૬ੑ͕͍͍ʂʂ

Slide 15

Slide 15 text

Domain knowledge can be expressible naturally in program code. Why DDD with Scala? 15 / / ) / . / ( / ( - Can use SPACE between receiver object and method. - Can be omitted parentheses if only one parameter. - Can expand a code expression as the language specification. Code is the Domain specification. ίʔυ͕ࣗવจͷ࢓༷ॻʹͳΔɻ

Slide 16

Slide 16 text

Object-Oriented with Functional programming. Why DDD with Scala? 16 Define Domain models statically. Define Domain behavior functionally. ) , ) ( , ( , ) , ( , ) , ) , ) , ( , ) , ( , ) , ) , ) , ΦϒδΣΫτࢦ޲ͱؔ਺ܕͷ͍͍ͱ͜ͲΓɻ

Slide 17

Slide 17 text

Can define a type easily using case class. Why DDD with Scala? 17 ) ) ( ) ( ,) ) ,) - No Constructor. - No GETTER and SETTER. - Provide default implementation #equals(), #hashCode() and #toString(). - Easy immutable programming using #copy() method. Eliminate Boilerplate code from the Domain models. υϝΠϯϞσϧ͔ΒϘΠϥʔϓϨʔτΛഉআɻ

Slide 18

Slide 18 text

Compatible with variables and functions. Why DDD with Scala? 18 )/( = ( / =: (/ = . = = ( / Can change a property to derived-property easily. )/( = ( / : = . = = =( ) = ( / ϓϩύςΟͱಋग़ϓϩύςΟͷมߋ͕༰қɻ

Slide 19

Slide 19 text

Can nest functions. Why DDD with Scala? 19 , ,( , : , ( , ) , , ,) = , , ) = , , , ( ) , , = Easy to name for steps of domain logic. - Clear usage scope. - Goodbye many cumbersome private methods. υϝΠϯϩδοΫʹࡉ໊͔͘લΛ෇͚΍͍͢ɻ

Slide 20

Slide 20 text

20 3. Code style ίʔσΟϯάελΠϧ

Slide 21

Slide 21 text

Use immutable. 21 : . . ) . . ) . . = ./ . .( . ) . . ) . . . : . ) . . ) . . = ./ . .( . ) . . ) . . - Do not use `var`, use `val`. In using case class, the properties are immutable. - Do not rewrite value, create an another instance that have new value. In using case class, easy to implement with #copy() method. Πϛϡʔλϒϧʹ࣮૷͢Δɻ

Slide 22

Slide 22 text

Localize the side effects. 22 - All that do not return as return value are side effects. Such as persist data, throw an Exception …etc - All side effects can not be eliminated, but side effects can be localized. - In DDD, important to eliminate side effects from the core domain logic. ෭࡞༻ΛہॴԽ͢Δɻ

Slide 23

Slide 23 text

23 ( . ) ) ) ( = = ) ( ) ) ( . ) ) ( ) ) ) / / .. ) = = ) ( ) ͍ͭͰʹӬଓԽ͠ͳ͍ɻ

Slide 24

Slide 24 text

Use `for` expression. 24 ,<)- = = : ,< )- = ( - )- = = : ,< )- : ,< )- :( : ( - < :( : Name to a step by define nested function. Combine steps using `for` expression. GPS಺แࣜΛ࢖ͬͯɺॲཧεςοϓΛͭͳ͛Δɻ

Slide 25

Slide 25 text

Use implicit parameters. 25 / =D .= . D . : =D .= D) .= D : = . .= D) .= D . D D " .= ( DD D =D.D) .= / =D : = " - Eliminate parameters that are not concerns of the domain (such as DB session) using implicit parameters. JNQMJDJUύϥϝλͰɺυϝΠϯϩδοΫ͔ΒϊΠζΛഉআɻ

Slide 26

Slide 26 text

Do not use #copy() from outside. 26 - If abuse a #copy() method, You can not understand the intent of domain logic. - Use #copy() method only in self. )( ) ( (. (. (. (. (. ) (. (. " (. ( )( ) ( (. (. ") (. ( DPQZ ͸֎͔Βݺͼग़ͯ͠͸͍͚ͳ͍ɻ

Slide 27

Slide 27 text

Do not omit variable names. 27 - < , - ( , ) , , , ) , , - , , , , , , , < ) , , < , < , , - < - ( , , ) , , - , , , < < , < Named by ubiquitous language. ม਺໊͸Ή΍Έʹུ͞ͳ͍ɻ Do not name like a formula.

Slide 28

Slide 28 text

Prefixing to Optional variable. 28 - - : - / - - / - : - Prefixing the name of an option variable(such as `maybe`) makes it easier to read. - Scala has a type inference, so helpful if you can be resolved by name. 0QUJPOܕม਺ʹ͸ɺͦΕͱΘ͔Δ઀಄ࣙΛ෇͚Δɻ

Slide 29

Slide 29 text

Use code formatter tools. 29 - < , < - ( , ) , , , ) , , - , , , , , , , < , , < , < , , - < , <<<<<<<<<<< - ( , ) , , , ) , , - , , , , , , , < , , < , < , , - Scalafmt, Scalareform …etc - Especially effective for `for` and `match-case` expressions. ࣗಈίʔυϑΥʔϚοτπʔϧͰಡΈ΍͘͢ɻ

Slide 30

Slide 30 text

30 4. The Architecture ΞʔΩςΫνϟ

Slide 31

Slide 31 text

Layers 31 ϨΠϠʔͷ෼ׂɻ

Slide 32

Slide 32 text

Layers and Responsibilities Modeling the Domain knowledge. Define Nouns as types, Verbs as functions. Persists a Domain models. Depends on middle wares, backend services. (such as RDBMS, MOM) Realize the Use-Case using a Domain models. provide interfaces for using application. (such as Web API, GUI and CLI) Infrastructure Layer Interface Layer Application Layer Domain Layer 32 ϨΠϠʔͱͦͷ੹຿ɻ

Slide 33

Slide 33 text

Layers structure 33 Interface Application Domain Infrastructure The Layered Architecture Domain is depends on a Infrastructure. - Domain models do side effects. - Infrastructure's code come into a domain logic. - Can not change a middleware, backend services and data storages easily. - Ruin testability for Domain logic. υϝΠϯ͕Πϯϑϥʹґଘͯ͠͠·͏ͷ͕໰୊ɻ

Slide 34

Slide 34 text

Layers structure 34 Interface Application Domain Infrastructure The Domain centered Architecture Domain is free. (No any depends) - Eliminated side effects in domain models. - However, Application remains Infrastructure dependent. υϝΠϯ͕Πϯϑϥʹґଘ͠ͳ͍ɻ

Slide 35

Slide 35 text

Layers structure 35 - Same basis with the Hexagonal Architecture and Clean Architecture. ͞ΒʹਐΊͯΦχΦϯΞʔΩςΫνϟɻ The Onion Architecture : part 1 http://jeffreypalermo.com/blog/the-onion-architecture-part-1/

Slide 36

Slide 36 text

Layers structure 36 Interface Application Domain Infrastructure Domain and Application are not depends on Infrastructure. - Infrastructure mixed-in into the Application and Domain by the Interface. The Onion Architecture ΞϓϦέʔγϣϯ΋Πϯϑϥʹґଘ͠ͳ͍ɻ

Slide 37

Slide 37 text

Error Handling 37 Τϥʔॲཧ

Slide 38

Slide 38 text

Error handling 38 Don't throw an Exception. - In Functional programming, Exception is side effect. - Returning a value that wrapped with Error-type. - Unify the Error-type for easy error handling. Τϥʔ͸ྫ֎Ͱ͸ͳ͘ɺ໭Γ஋Ͱฦ͢ɻ

Slide 39

Slide 39 text

Error types Option Some(value) or None Try Success(value) or Failure(Exception) Either Right(value) or Left(value) 39 Error-types in Scala. 4DBMBͰ࢖͑ΔΤϥʔܕɻ

Slide 40

Slide 40 text

Error types 40 scala.Option Used to express a optional value in domain concept. Can not have a error content, so do not used to error handling. - Optional properties of the Entity and Value Objects. - An Entity may not found in the Repository's finder methods. ( >( > )( > >= ( = > > > : > >= > >= 0QUJPO͸Τϥʔॲཧʹ͸࢖Θͳ͍ɻ

Slide 41

Slide 41 text

Error types 41 scala.util.Either Can returning a error contents if the Domain contract was not complied. Define a custom error type, and use it with Either. - Use in error handling on Domain and Application. - The custom error type is desirable to have a stack trace. == : C,= ( C > = = C = C C A == : C > C = C > > = C C = C) > C C A Τϥʔॲཧ͸ &JUIFSͰɻ

Slide 42

Slide 42 text

Error types 42 scala.util.Try Can wrap a Java's Exception, and return it as a return value. - Enclose the code that may throw an Exception with `Try{...}`. - Useful when using any Java libraries in the Infrastructure, because these throw an Exception. : ( ) = > : = > +BWBϥΠϒϥϦΛ࢖༻͢Δͱ͖͸ 5SZͰғΉ ɻ

Slide 43

Slide 43 text

Error handling 43 Errors at the Layers - Violation the Domain contract. - Mainly, cause by user's operation, and resolvable by self. - Unexpected error such as a failure at Storage, N/W and other Services etc. - Could not resolve by user. - Could not complete the Use-Case. - Mainly, cause by user's operation, and resolvable by self. - Invalid input. - Cause by user's operation, and resolvable by self. Infrastructure Layer Interface Layer Application Layer Domain layer ֤ϨΠϠʔ͝ͱͷΤϥʔͷಛੑɻ

Slide 44

Slide 44 text

Error handling 44 Converting the Error Want return the Error user friendly(What?, Why?, How to solve?), but should not be conscious the Interface at the Domain and Infrastructure. An Error from the Domain and Infrastructure need convert to user friendly Error. ϢʔβʔϑϨϯυϦʔͳΤϥʔʹม׵͢Δඞཁ͕͋Δɻ

Slide 45

Slide 45 text

Error handling 45 Interface Application Service Repository Event Publisher Repository Impl Event Pub. Impl Domain Service Entity Factory Query Processor Query Proc. Impl Java lib. Ex) JDBC driver Exception Try Either [Domain Error] Either [Service Error] Convert to Try. Convert to the Service Error from Domain Error and Try. ϨΠϠʔ͝ͱʹΤϥʔܕΛม׵͍ͯ͘͠ɻ

Slide 46

Slide 46 text

Error handling 46 Convert at Application Service Convert to the Service Error(Either) at Application Service. - From a Domain Error(Either). - From an Infrastructure Error(Try). Define some helper function for converting a errors at Application Service like as the Use-Case specification. ΞϓϦέʔγϣϯαʔϏεͰΤϥʔม׵Λࣗવʹɻ

Slide 47

Slide 47 text

 Error handling 47 I. - F <.=. I I IF = IF I = I I < EI = I<= I IIIIIIIII = = E ( I = = = < = I = I I =

Slide 48

Slide 48 text

48 5. Implement Components ίϯϙʔωϯτͷ࣮૷ɻ

Slide 49

Slide 49 text

49 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Components ίϯϙʔωϯτɻ

Slide 50

Slide 50 text

50 Intention The application code is the Use-Case specification. - The Application service is a client of the domain, and a mirror to reflect that's expressiveness. - Writing the Application services naturally using the ubiquitous language. ΞϓϦέʔγϣϯαʔϏε͸Ϣʔεέʔεهड़ɻ

Slide 51

Slide 51 text

51 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Application Service ΞϓϦέʔγϣϯαʔϏεͷ࣮૷ɻ

Slide 52

Slide 52 text

Application Service 52 - Realize a Use-Case using the Domain models. - Just using the Domain models. Do not leak a Domain knowledge out to the Application Service. - Do not depends on the Infrastructure. Even if you change infrastructure, use case specifications are not affected. - Easy to Unit test at Use-Case level. - Transaction boundary. POINT υϝΠϯΛ࢖ͬͯϢʔεέʔεΛ࣮ݱ͢Δɻ

Slide 53

Slide 53 text

Application Service 53 - Typically, Step of Application Service is… 1. Get an existing Entity using the Repository, or newly create it using the Factory. 2. Apply some business rule by the Entity or Domain Service. 3. Persist a processed Entity. 4. Publish a Domain event using the Event Publisher. - Combine these steps in `for` expression. - Convert to an Error of Use-Case level from the Domain and Infrastructure errors. POINT Ұ࿈ͷॲཧεςοϓΛɺGPS಺แࣜͰͭͳ͛Δͷ͕جຊܗɻ

Slide 54

Slide 54 text

Application Service 54 E F . E =. E =( E-F >< =( E-F >< E = = FE EI < >< F < ( E =( E-F >< F >< E = E < ) <>F < ( < > = EXAMPLE ΞϓϦέʔγϣϯαʔϏεͷίʔυྫɻ

Slide 55

Slide 55 text

55 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Entity ΤϯςΟςΟͷ࣮૷ɻ

Slide 56

Slide 56 text

Entity 56 - Typically, One Entity in the Aggregate. (as Aggregate Root) - Identified by unique Entity-ID. Override #equals() and #hashCode() to guarantee equality by it. - Using the Value Object for properties. Do not use primitive types. - Hold only the Entity-ID for association with other Aggregates. Do not hold a Entity instance. POINT ΤϯςΟςΟ͸Ұҙͳ*%Ͱࣝผ͞ΕΔɻ

Slide 57

Slide 57 text

Entity 57 - Define the Domain behavior as Entity's method. Do not become the "Anemic Domain Model" !! - Entity's method is return a modified Entity and a Domain event. Do not rewrite a own states. - If improper as the Entity's method, implement to Domain Service. (Such as processing that over multiple Entity instances …etc) POINT υϝΠϯශ݂঱ʹͳΒͳ͍Α͏ʹ஫ҙʂ

Slide 58

Slide 58 text

Entity 58 : ) A A ,A A A = ) . ) = A = A E := ( A : D := = :A := E C : A = D A A ,A A := C : C A := A A = :A A C A EXAMPLE ΤϯςΟςΟͷίʔυྫɻ

Slide 59

Slide 59 text

59 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Value Object όϦϡʔΦϒδΣΫτͷ࣮૷ɻ

Slide 60

Slide 60 text

Value Object 60 - All nouns in the Domain are define a type as the Value Object. - Type alias is not type safe. - Extends `scala.AnyVal`. By this reduce cost when instantiate Value Objects. - Define the First-class Collection for collection of objects. Do not use raw Collection(List, Set, Map…). POINT υϝΠϯͷ໊ࢺ۟͸όϦϡʔΦϒδΣΫτܕΛ࡞Δɻ

Slide 61

Slide 61 text

Value Object 61 - Automatically convert with Value Object and primitive type using `implicit conversion` and `Type-class pattern` at the Interface and Infrastructure for reduce implementation cost. POINT ϓϦϛςΟϒ஋ͱͷ૬ޓม׵͸Ͱ͖Δ͚ͩࣗಈԽɻ

Slide 62

Slide 62 text

Value Object 62 E N A FN A NL : N F N FFN F - N E A N A FN A N N = F N F E N FFN F N E A NN N A = N F L F N = N C A A FN F / - ) F N = N( F A FN F ( ) F N FFN( A N E A N A FN A F N FFN( A F A F N D ( A NLNN N+ EF FFN( A : A N( A N N C A FN N A FN "N A EXAMPLE όϦϡʔΦϒδΣΫτͷίʔυྫɻ

Slide 63

Slide 63 text

Value Object 63 <:OC >: O= E E E:O O C I:E O/E= = =I:O :O )O :O. >: = = = : E : /"O O O : / I : O/ = = = C I O/O()O O O(O C I I : O C I:E O :O. >: O )O/E= = =I:O : = = = : E : / I : O : / O/O(OI : I : N = CE O= E E E: = = = I C - : O E= : I E - : O E=

Slide 64

Slide 64 text

64 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Role ϩʔϧͷ࣮૷ɻ

Slide 65

Slide 65 text

Role 65 - (Maybe)Not defined as the Component in the DDD. - Define the relationships between the Entities across Aggregates. POINT ू໿Λ·͙ͨΤϯςΟςΟͱΤϯςΟςΟͷؔ࿈Λఆٛɻ

Slide 66

Slide 66 text

Role 66 POINT - Define the Role by the role name of relationship. - Can define a methods to handle relationship without modify targeted Entity. - Convert automatically to the Role from targeted Entity using by `implicit conversion`. - Can write code with Role as the subject by SVO style. ؔ࿈ઌͷΤϯςΟςΟʹӨڹͤͣʹؔ࿈Λѻ͏͜ͱ͕Ͱ͖Δɻ

Slide 67

Slide 67 text

Role 67 / = / / , = , =) D / / / ) : ) , =) : : , = / : ) : # D ( : / / : / . / # : A , = : # / / : = , = : # : = D D EXAMPLE ϩʔϧͷίʔυྫɻ

Slide 68

Slide 68 text

Role 68 = /: ) /: ( = ./ / / / /.( = /: ./ /. / / / /: / / : / : /.( = /: ( EXAMPLE ϩʔϧΛ࢖͏ίʔυྫɻ

Slide 69

Slide 69 text

69 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Factory ϑΝΫτϦͷ࣮૷ɻ

Slide 70

Slide 70 text

Factory 70 - 2 patterns for creation the Entity. 1. Created individually. 2. Created by a Entity in other aggregation. (If necessary to hold that Entity ID) - In individual case, create a Entity by the Factory. The Factory implement as the Companion object of the Entity. - In other aggregation case, create a Entity by the Role. POINT ΤϯςΟςΟੜ੒ͷͭͷύλʔϯɻ

Slide 71

Slide 71 text

Factory 71 - The Factory is responsible for generating a unique Entity ID for each new Entity. - In many cases, ID generation method is depends on Infrastructure, so give the ID Generator to the Factory from outside. - By doing so easy to Unit-test for the Factory. POINT ΤϯςΟςΟ*%ͷੜ੒ϩδοΫ͸֎͓ͩͯ͘͠͠ɻ

Slide 72

Slide 72 text

Factory 72 /: / / :/ . . " / . / = A . /A / . / : A ) /A= ( A AE ) /A= . . ) /A= " / / G /: . /A . / / = ,A / /A = / / :/ / / " / / / " / A = . : A :/ A = . /A / / / / : A ) /A= ( A AE ) /A= / / ) /A= " / / " / A = G /: A/ /A / / / ( / : A/ EXAMPLE ϑΝΫτϦʔͷίʔυྫɻ

Slide 73

Slide 73 text

73 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Repository ϦϙδτϦͷ࣮૷ɻ

Slide 74

Slide 74 text

Repository 74 - Persist a Entity including Value Objects. - Define the interface(trait) with the Domain, and implement it with the Infrastructure. - Typically, in addition to the following methods and it have some finder methods. 1. Get an Entity by Entity ID. 2. Persist an Entity. (INSERT or UPDATE) 3. Delete an Entity. (Do not define if it can not be deleted) POINT ΤϯςΟςΟΛऔಘอଘ࡟আ͢Δɻ

Slide 75

Slide 75 text

Repository 75 - Handle only the Entity in own Aggregate. if need handle with the other Aggregate, define as the Query Model. - In the Repository implementation, mutual conversion between a Value Object and primitive type frequently. So convert automatically using `implicit conversion` for to be easier. POINT ू໿Λ·͍ͨͰӬଓԽͯ͠͸͍͚ͳ͍ɻ

Slide 76

Slide 76 text

Repository 76 = " B B B B ) / / B B E B B / B B B / B I B D D " " B B B B B B ( D E B " . D : B " = B D D " = B D D " B " B ) / / B I B E B B / B I B B / B I I EXAMPLE ϦϙδτϦͷίʔυྫɻ

Slide 77

Slide 77 text

77 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Domain Service υϝΠϯαʔϏεͷ࣮૷ɻ

Slide 78

Slide 78 text

Domain Service 78 - Collection of any functions that is defining business rule of the Domain. - Not the Façade. Without using Repository and Event Publisher, and do not occur side effects. - Not required. Define if necessary, such as realize complex business rules. POINT ϑΝαʔυͰ͸ͳ͍ɻؔ਺ͷू·Γɻ

Slide 79

Slide 79 text

Domain Service 79 ) ( . ( : . ( ( ( = ( ( ( = ( ( ( . ( : . ( EXAMPLE υϝΠϯαʔϏεͷίʔυྫɻ

Slide 80

Slide 80 text

80 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Domain Event and Event Publisher υϝΠϯΠϕϯτͱΠϕϯτύϒϦογϟʔͷ࣮૷ɻ

Slide 81

Slide 81 text

Domain Event, Event Publisher 81 - Define each business events in the Domain knowledge as the Domain Event. (such as "Created a Task", "Change a title of Task" etc...) - In the CQRS(Command Query Responsibility Segregation) Pattern, update the Query Model asynchronously via subscribe the Domain Events. - In the Micro-Service Architecture, using Domain Events for collaborate with other Services asynchronously . POINT υϝΠϯͰى͜Δࣄ৅ΛϞσϧԽ͢Δɻ

Slide 82

Slide 82 text

Domain Event, Event Publisher 82 - Do not hold the Entity. Define only necessary properties. Because, should not recreate the Entity from a Domain Event. - Domain Event is created and returned with return value by the Entity or Domain Service. - Event Publisher is deserialize(such as JSON, XML) the Domain Event and queue to a Event queue. POINT υϝΠϯΠϕϯτ͸ΤϯςΟςΟΛอ࣋ͯ͠͸͍͚ͳ͍ɻ

Slide 83

Slide 83 text

Domain Event, Event Publisher 83 = : C A C C ( C = = DC A . A ( = : , D C ) )/) C:C ) E C )/) DC A I , CDA A C ) C:C ( = : )E C A C I ( = : , D C A C E C I ( = : , D C C A C C : C = C DC A EXAMPLE υϝΠϯΠϕϯτͷίʔυྫɻ

Slide 84

Slide 84 text

Domain Event, Event Publisher 84 C) : = ( : : = C) : C) ) : . C : . : C) : = ( : :C) : = ( : : = C) : C) ) : C C C EXAMPLE ΠϕϯτύϒϦογϟʔͷίʔυྫɻ

Slide 85

Slide 85 text

85 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Query Model and Query Processor ΫΤϦϞσϧͱΫΤϦϓϩηοαʔͷ࣮૷ɻ

Slide 86

Slide 86 text

Query Model, Query Processor 86 - The Query Model can define a view that included values of multiple Aggregation. - The Query Model is updated asynchronously by the Event Subscriber. - Do not use the Value Object for the Query Model. - The Query Processor defines some methods to search the Query Models. This does not have mutator methods. POINT ΫΤϦϞσϧͰ͸ෳ਺ͷू໿Λ·͍ͨͩϏϡʔΛఆٛͰ͖Δɻ

Slide 87

Slide 87 text

Query Model, Query Processor 87 : . , , = , : , : , . ) = = = , , : : ) = = ( , E ) = = = , , : I EXAMPLE ΫΤϦϞσϧͱΫΤϦϓϩηοαʔͷίʔυྫɻ

Slide 88

Slide 88 text

88 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Event Subscriber ΠϕϯταϒεΫϥΠόʔͷ࣮૷ɻ

Slide 89

Slide 89 text

Event Subscriber 89 - Subscribing the Domain Events asynchronously. - Execute any process like the following… 1. Update the Query Model. 2. Make a Copy of other Context for using in own Context. - Implementing the Event Subscriber depends on the Infrastructure using as Event queue. (such as AWS Kinesis, SQS and Lambda) POINT υϝΠϯΠϕϯτΛड৴ͯ͠ඞཁͳॲཧΛ͢Δɻ

Slide 90

Slide 90 text

90 Factory Repository Domain Service Query Processor Query Model Event Publisher Application Service Interface Entity Value Object Role Domain Event Event Subscriber Interface ΠϯλʔϑΣʔεͷ࣮૷ɻ

Slide 91

Slide 91 text

Interface 91 - Provide any boundary for the user and other Services. (such as GUI, CLI, API …etc) - Validate input value, and execute the Use-Case using the Application Service. - Choose implementation of the Infrastructure to use, and inject to the Application Service and more. - The implementation of the Interface depends on the framework used. POINT Ϣʔβʔ΍ଞγεςϜ΁ͷΠϯλʔϑΣʔεΛఏڙɻ

Slide 92

Slide 92 text

Interface 92 D N E )BA DB D E D N E D : C N E D D . I E E . C "N E D, EEN E )BA DB D, C A EN E )BA DB D N,AE A N N CC BAN D N A N A= N C A A E B DD N E D NA IN E D I N D AE BA I D B DD N E D CBE BDK NA IN E D CBE BDK I N E D CBE BDK/A ( B DD N E CBE BDK NA IN E CBE BDK I N E CBE BDK/A ( B DD N E A E D NA IN E A E D I N E A E D/A : EXAMPLE ΠϯλʔϑΣʔεͷίʔυྫɻ

Slide 93

Slide 93 text

93 See the Sample implementation on GitHub. https://github.com/crossroad0201/ddd-on-scala (JU)VCͷαϯϓϧίʔυ΋ࢀর͍ͯͩ͘͠͞ɻ

Slide 94

Slide 94 text

94 6. Practice DDD %%%ͷ࣮ફ

Slide 95

Slide 95 text

And more… 95 - with the Event Sourcing No update an Entity states, records all Domain events. Can re-create a Entity by replaying recorded Domain events. - with the Reactive programming Integrate the reactive programming with DDD using the Akka(Stream). Πϕϯτιʔγϯάͱ͔ϦΞΫςΟϒϓϩάϥϛϯάͱ͔ʜ

Slide 96

Slide 96 text

Require incremental learning 96 - No substitute for practicing DDD. - Establishing own(ours) style by practice, experiment and learning cyclically. - Practice gradually while consult own(ours) skill sets… - Easy to practice incrementally with the Micro Service Architecture, because can practice per a bounded contexts. %%%ͷ࣮ફ͸ܧଓతͳֶश͋ΔͷΈɻ

Slide 97

Slide 97 text

97 at Mt.Fuji 2016 ௕࣌ؒͷ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ