Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rethink your architecture with CQRS

devNetNoord
September 20, 2012

Rethink your architecture with CQRS

devNetNoord 3, sessie 2

devNetNoord

September 20, 2012
Tweet

More Decks by devNetNoord

Other Decks in Technology

Transcript

  1. Rethink your architecture with CQRS Pieter Joost van de Sande

    Passionated software developer, Atos Email: [email protected] @twitter: pjvds
  2. Presentation layer Data layer Domain layer Send DTO Send DTO

    Map DTO Map DTO Write data Write data Map DTO Write data Write data Send DTO Request DTO Map DTO Request DTO Map DTO Query data Query data Request data Map DTO Query data Query data Request DTO Send update Update screen
  3. SELECT CCUS.CUST_FIRST_NAME , CCUS.CUST_LAST_NAME , CINT.CUST_INTEREST_RANK , CILO.CUST_INTERST FROM CUST_CUSTOMER

    AS CCUS , CUST_INTEREST_LOOKUP AS CILO , CUST_INTEREST AS CINT WHERE ( CCUS.CUST_CITY = 'Singapore' AND CCUS.CUST_PROV_STATE = 'Singapore' AND CCUS.CUST_CODE IN ( SELECT COHE.CUST_CODE FROM CUST_ORDER_HEADER AS COHE , CUST_ORDER_STATUS AS COST WHERE ( COHE.CUST_ORDER_DATE >='2009-01-01 00:00:00.001' AND COST.CUST_ORDER_STATUS IN( 'Shipped', 'Back-ordered', 'In-process' ) AND COHE.CUST_ORDER_STATUS_CODE = COST.CUST_ORDER_STATUS_CODE ) ) AND CCUS.CUST_CODE = CINT.CUST_CODE AND CINT.CUST_INTEREST_CODE = CILO.CUST_INTEREST_CODE ) ORDER BY CCUS.CUST_LAST_NAME ASC , CCUS.CUST_FIRST_NAME ASC , CINT.CUST_INTEREST_RANK ASC
  4. Bottom line • Getting data should be simple – select

    * from x where y • Examples: – Get all products of user X – Get total price of all orders of month May
  5. Commands • Simple message contains all info needed • Lower

    coupling (law of demeter) • Causes a state transition • Contains the intend of the user • Exists in a bounded context • Examples: – AddProductToShoppingCart – PurchaseOrder – MoveUserToNewAddress – CorrectAddressForUser – FinalizeOrder
  6. Change has intent • Users don’t make changes for nothing

    • This intent has value! Correct the address for user X Customer X moved to new address VS.
  7. Events • Simple message that describes an event • Contains

    intent • Contains all the data related to the event • Past tense CustomerMoved { CustomerId, NewAddress }
  8. Event store • Contains all domain events • Does not

    need to be relational • Write are *awesomely* fast! • Rock solid audit log • Read models can (re)build themself of it • The domain uses it to get the current state • Can be used to build any structural model you want!
  9. User interface Commandhandlers Repository Event bus Denormalizers Read databases Write

    side Read side events events events commands DTO’s Domein Facade
  10. Wrap up • Optimize for reading and writing • Makes

    changes part of the problem domain • Free rock solid audit log • Capture the intent of changes • Allow multiple read models