Skip to content

DDD & Clean Architecture on the example of The Three Little Pigs

Notifications You must be signed in to change notification settings

mat3e/3pigs-ddd

Repository files navigation

The Three Little Pigs with DDD and clean architecture

My tech talk, fairy tale sources: 1, 2, 3, 4.

Java, Groovy + Spock, Kotlin, Maven, Spring

The main focus should be on The Three Little Pigs, but to show an alternative, more pragmatic approach there is also Little Red Riding Hood module, utilizing package-private access more, close to what's presented in this great tech talk by Jakub Nabrdalik.

Web app

App starts as an ordinary web app for

spring.main.web-application-type=servlet

The Three Little Pigs

Available operations:

  1. Build house: POST localhost:8080/houses
     {
         "owner": "VERY_LAZY"
     }
    Possible values for owner:
    • VERY_LAZY
    • LAZY
    • NOT_LAZY
    • NOT_LAZY_ANYMORE
  2. Verify the house state: GET localhost:8080/houses/{id}
  3. Blow house down: DELETE localhost:8080/houses/{id}

There is a dedicated Postman collection with all these operations already defined: pigs3/adapters/src/main/resources/3Pigs.postman_collection.json.

Little Red Riding Hood

There is a dedicated Postman collection with all the operations ready to use: redhood/src/main/resources/RedHood.postman_collection.json.

Console app

When

spring.main.web-application-type=none

app prints the whole The Three Little Pigs story in the console.


EventStorming

An example session with myself, for The Three Little Pigs. There was no need to run Process Level ES as we already had a single BoundedContext, and it was doable to jump straight into Design Level.

For the second module I also run Big Picture ES which helped to realize that the main things are interactions and wolf's intentions.

Big Picture

Big Picture

Design Level - commands, rules & actors

Design Level rules

Design Level - naming aggregates

Design Level aggregates

Possible improvements

  • House could have mechanisms for rebuilding
  • Story can be extended - currently there is nothing about wolf climbing through the chimney and pigs lighting the fire
    • New House method, e.g. litFire
    • New BigBadWolfService method, e.g. comeDownTheChimneyOf
    • Event, e.g. WolfStartedClimbing instead of WolfResignedFromAttacking, new event from House, e.g. WolfEscaped (when burns in the fireplace)
    • WolfStartedClimbing should call both litFire and comeDownTheChimneyOf in a proper order
    • WolfEscaped should result in knowledge sharing
  • Full Event Sourcing - House can be built just from events, no snapshots in the current form
  • Rely fully on DomainEventPublisher - although @DomainEvents annotation looks nice, it relies on ApplicationEventPublisher which has known limitations, especially without additional tooling like Spring Modulith
  • Instead of "technical" packages corresponding with Clean Architecture and DDD, there could be more process-oriented packages, like building (or construcitng) and destroying, where the domain logic would lay, similar as presented in this great tech talk by Victor Rentea