Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CQRS with Event Source in Functional sauce served by Kotlin

503 views

Published on

Slides of my talk at DDD eXchange 2018 London.

Published in: Technology
  • Be the first to comment

CQRS with Event Source in Functional sauce served by Kotlin

  1. 1. @Ramtop CQRS with Event Source in Functional sauce served by Kotlin Uberto Barbinigithub.com/uberto/anticapizzeria
  2. 2. @Ramtop CQRS + Event Source Very powerful but a bit intimidating… Can we do better?
  3. 3. @Ramtop Functional Programming ● Immutability ● Purity ● Higher order functions ● Transformations and preserving properties
  4. 4. @Ramtop Functional vs OO Programming
  5. 5. @Ramtop Command ● A Command is a request for changing the internal state of the system ● A Command can “fail” if is not congruent with the current state of the System ● Each Command is executed in an atomic context
  6. 6. @Ramtop Query ● A query ask for a snapshot of the state ● Queries typically need different data and denormalization from domain model. So we separate the models in CQRS. ● Queries are eventually consistent with the domain
  7. 7. @Ramtop Values-Entities ● They represent the state of the domain ● In functional programming everything is immutable, so there is no identity ● To keep state changes we have Algebraic Data Types
  8. 8. @Ramtop Aggregates ● An aggregate has all the information for a transaction unit ● Aggregates is what Events fold to ● They are composed by Entities
  9. 9. @Ramtop Event ● Events are the “atoms” of System state change ● Nothing can change without an event, every event can change only one entity ● Entity + Event => Entity
  10. 10. @Ramtop Actors ● More powerful and easy to use concurrency model than Threads/Locks ● They communicate asynchronously and potentially remotely ● Useful for Bounded Contexts and Services
  11. 11. @Ramtop
  12. 12. @Ramtop Antica Pizzeria ● Real application is about finance products booking but a pizzeria has a surprising similar domain ● We want to do implement the backend for a ChatBot that will assist booking and enquires about orders.
  13. 13. @Ramtop Kotlin Syntax ● No semicolon! ● String? Is a different type from String ● myFun{println(it)} is equivalent to myFun(x -> println(x)) ● val xy is declaring xy immutable var xy is declaring xy mutable ● User(“Joe”) is equivalent to Java new User(“Joe”)
  14. 14. @Ramtop Commands
  15. 15. @Ramtop Commands
  16. 16. @Ramtop Queries
  17. 17. @Ramtop Actors (where we can put all this stuff)
  18. 18. @Ramtop Commands emit Events (it’s the only way to change)
  19. 19. @Ramtop Events
  20. 20. @Ramtop Queries listen to Events (to keep up with the changes)
  21. 21. @Ramtop Let's fold Events (to create Entities)
  22. 22. @Ramtop Entities as EventComposable
  23. 23. @Ramtop Execute Commands
  24. 24. @Ramtop Logic inside Commands
  25. 25. @Ramtop Application
  26. 26. @Ramtop We can now process C&Q (Fun with Kotlin ExtFun)
  27. 27. @Ramtop Real Code Now!
  28. 28. @Ramtop QAUberto Barbini @Ramtop github.com/uberto/anticapizzeria
  29. 29. @Ramtop
  30. 30. @Ramtop

×