3pigs-ddd
3pigs-ddd copied to clipboard
DDD & Clean Architecture on the example of The Three Little Pigs
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:
- Build house:
POST
localhost:8080/houses
Possible values for{ "owner": "VERY_LAZY" }
owner
:-
VERY_LAZY
-
LAZY
-
NOT_LAZY
-
NOT_LAZY_ANYMORE
-
- Verify the house state:
GET
localhost:8080/houses/{id}
- 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
Design Level - commands, rules & actors
Design Level - naming 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 ofWolfResignedFromAttacking
, new event fromHouse
, e.g.WolfEscaped
(when burns in the fireplace) -
WolfStartedClimbing
should call bothlitFire
andcomeDownTheChimneyOf
in a proper order -
WolfEscaped
should result in knowledge sharing
- New
- 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 onApplicationEventPublisher
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
(orconstrucitng
) anddestroying
, where the domain logic would lay, similar as presented in this great tech talk by Victor Rentea