KEMBAR78
Introduction to-ddd | PDF
Domain Driven Design
software that reflects the business domain




                         John Ferguson Smart
So who is this guy, anyway?
               Consulta
                       nt
               Trainer
              Mentor
              Author
             Speaker
             Coder
   John Fer
            guson S
                    mar t
What is Domain-Driven Design?
Domain-driven design is not a
technology or a methodology.
It’s a set of principles and patterns
for focusing design effort where it
matters most.
Basic principles
Basic principles




          Focus on the domain

   Domain design is based on a model
Basic principles




A Ubiquitous Language...


                   ...and a shared vision
So what’s a model?
How do you represent your domain model?
How do you represent your domain model?




      Customer                Account

                (UML) Diagrams?


                              Branch
How do you represent your domain model?




             Detailed specifications?
How do you represent your domain model?

Account Holder withdraws cash
As an account holder
I want to withdraw cash from an ATM
so that I can get money out when the bank is closed

            Scenario 1: Account has sufficient funds
            Given the account balance is $100
            And the card is valid
            And the machine contains enough money           Free text?
            When the Account Holder requests $20
            Then the ATM should dispense $20
            And the account balance should be $80
            And the card should be returned

                                 Scenario 2: Account has insufficient funds
                                 Given the account balance is $10
                                 And the card is valid
                                 And the machine contains enough money
                                 When the Account Holder requests $20
                                 The ATM should not dispense any money
How do you represent your domain model?

 @Test
 public void aCashWithdrawalShouldDeductSumFromBalance() {
     Account account = new Account();
     account.makeDeposit(100);
     account.makeCashWithdraw(60);
     assertThat(account.getBalance(), is(40));
 }
                    Automated tests?
How do you represent your domain model?

   class Account(val   owner : Customer,
                 val   accountNumber : String,
                 val   accountType : AccountType,
                 var   balance : Double) {

       def deposit(amount : Double) { balance += amount }
       def withdraw(amount : Double) { balance -= amount }
   }
                            Code?
   class Customer(val firstName : String,
                  val lastName : String) {

       createAccount(accountType : AccountType) : Account
   }

   class AccountType extends Enumeration {
     val Current = value("CURRENT")
     val Savings = value("SAVINGS")
   }
The model is the mental representation




         Everything else is just a communication tool
Elaborating the model
Elaborating the model




A collaborative exercise
Elaborating the model




...based on a common language
Elaborating the model


Account Holder withdraws cash
As an account holder
I want too withdraw cash from an ATM
so that I can get money out when the bank is closed




                @Test
                public void aCashWithdrawalShouldDeductSumFromBalance() {
                    Account account = new Account();
                    account.makeDeposit(100);
                    account.makeCashWithdraw(60);
                    assertThat(account.getBalance(), is(40));
                }




          ...and expressed at all levels
Elaborating the model




An evolutionary process
Elaborating the model




 So testing is essential
Elaborating the model




           Test




...automated testing is essential
Domain-Driven Design practices
Domain-Driven Design architecture

                                   User Interface
         Presenta(on	
  Layer

                                   Service layer
          Applica(on	
  Layer      Stateless



                                   domain knowledge and
            Domain	
  Layer        business logic



                                   supporting libraries
         Infrastructure	
  Layer   persistence
                                   messaging
Domain-Driven Design architecture

           Entities
               • Identify and state
               • Persistence
Domain-Driven Design architecture

        Value	
  Objects
               • No conceptual identity
               • Immutable
Domain-Driven Design architecture

           Services
               • Domain-level services
               • Stateless
Domain-Driven Design architecture

         Repositories
             • Data retrieval
             • Abstraction of the persistence layer
Domain-Driven Design architecture

           Factories
               • Creation of complex entities
Application code should be a
reflection of the business domain




                                     John	
  Ferguson	
  Smart
                      Email:	
  john.smart@wakaleo.com
                       Web:	
  hAp://www.wakaleo.com
                                         TwiAer:	
  wakaleo

Introduction to-ddd

  • 1.
    Domain Driven Design softwarethat reflects the business domain John Ferguson Smart
  • 2.
    So who isthis guy, anyway? Consulta nt Trainer Mentor Author Speaker Coder John Fer guson S mar t
  • 3.
  • 4.
    Domain-driven design isnot a technology or a methodology.
  • 5.
    It’s a setof principles and patterns for focusing design effort where it matters most.
  • 6.
  • 7.
    Basic principles Focus on the domain Domain design is based on a model
  • 8.
    Basic principles A UbiquitousLanguage... ...and a shared vision
  • 9.
  • 13.
    How do yourepresent your domain model?
  • 14.
    How do yourepresent your domain model? Customer Account (UML) Diagrams? Branch
  • 15.
    How do yourepresent your domain model? Detailed specifications?
  • 16.
    How do yourepresent your domain model? Account Holder withdraws cash As an account holder I want to withdraw cash from an ATM so that I can get money out when the bank is closed Scenario 1: Account has sufficient funds Given the account balance is $100 And the card is valid And the machine contains enough money Free text? When the Account Holder requests $20 Then the ATM should dispense $20 And the account balance should be $80 And the card should be returned Scenario 2: Account has insufficient funds Given the account balance is $10 And the card is valid And the machine contains enough money When the Account Holder requests $20 The ATM should not dispense any money
  • 17.
    How do yourepresent your domain model? @Test public void aCashWithdrawalShouldDeductSumFromBalance() { Account account = new Account(); account.makeDeposit(100); account.makeCashWithdraw(60); assertThat(account.getBalance(), is(40)); } Automated tests?
  • 18.
    How do yourepresent your domain model? class Account(val owner : Customer, val accountNumber : String, val accountType : AccountType, var balance : Double) { def deposit(amount : Double) { balance += amount } def withdraw(amount : Double) { balance -= amount } } Code? class Customer(val firstName : String, val lastName : String) { createAccount(accountType : AccountType) : Account } class AccountType extends Enumeration { val Current = value("CURRENT") val Savings = value("SAVINGS") }
  • 19.
    The model isthe mental representation Everything else is just a communication tool
  • 20.
  • 21.
    Elaborating the model Acollaborative exercise
  • 22.
    Elaborating the model ...basedon a common language
  • 23.
    Elaborating the model AccountHolder withdraws cash As an account holder I want too withdraw cash from an ATM so that I can get money out when the bank is closed @Test public void aCashWithdrawalShouldDeductSumFromBalance() { Account account = new Account(); account.makeDeposit(100); account.makeCashWithdraw(60); assertThat(account.getBalance(), is(40)); } ...and expressed at all levels
  • 24.
    Elaborating the model Anevolutionary process
  • 25.
    Elaborating the model So testing is essential
  • 26.
    Elaborating the model Test ...automated testing is essential
  • 27.
  • 28.
    Domain-Driven Design architecture User Interface Presenta(on  Layer Service layer Applica(on  Layer Stateless domain knowledge and Domain  Layer business logic supporting libraries Infrastructure  Layer persistence messaging
  • 29.
    Domain-Driven Design architecture Entities • Identify and state • Persistence
  • 30.
    Domain-Driven Design architecture Value  Objects • No conceptual identity • Immutable
  • 31.
    Domain-Driven Design architecture Services • Domain-level services • Stateless
  • 32.
    Domain-Driven Design architecture Repositories • Data retrieval • Abstraction of the persistence layer
  • 33.
    Domain-Driven Design architecture Factories • Creation of complex entities
  • 34.
    Application code shouldbe a reflection of the business domain John  Ferguson  Smart Email:  john.smart@wakaleo.com Web:  hAp://www.wakaleo.com TwiAer:  wakaleo