SlideShare une entreprise Scribd logo
1  sur  103
An Introduction to

   Command/Query Responsibility
          Segregation

                presented by Dennis Traub
              .NET Stammtisch Konstanz/Kreuzlingen - 20. März 2013




Dennis Traub – Software Development Consultancy            @dtraub – mail@dennistraub.de
Who‘s that guy?

          Dennis Traub
          Founder, Owner, CEO, Single Employee of
          D. Traub Software Development Consultancy




             Consultant, developer, trainer, speaker


             39 years old


             Married, father of one


             20+ years in software development


             Hire me at mail@dennistraub.de




               @dtraub




Dennis Traub – Software Development Consultancy        @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                      Break         Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
The „Blue Book“ by Eric Evans




                                          Image Placeholder




Dennis Traub – Software Development Consultancy               @dtraub – mail@dennistraub.de
Domain-Driven Design

                                                         Domain
                                                         Service
                      Value Object
                                                                   Aggregate
                   Specification             The well-
                                              known
                    Reposit ory              Patterns               Entity


                               Factory                      And so on . .




Dennis Traub – Software Development Consultancy                        @dtraub – mail@dennistraub.de
Domain-Driven Design


                                                    Ubiquit ous Language
                     Core Domain
                                                                Domain Expert
               Bounded Context                 The
                                            important
                 Domain Model                Patterns             (Generic) Subdomains


                        Context Map                          And so on . .




Dennis Traub – Software Development Consultancy                         @dtraub – mail@dennistraub.de
don‘t build one big model
                            that works for everyone




Dennis Traub – Software Development Consultancy      @dtraub – mail@dennistraub.de
the tactical Building Blocks
                                    we don‘t use DDD
                             when there is no     value in
                             formalizing the problem




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
we only use DDD in parts
                            where we get a competitive
                                        advantage




Dennis Traub – Software Development Consultancy     @dtraub – mail@dennistraub.de
we focus our effort and resources on
                the most             important subdomain




Dennis Traub – Software Development Consultancy    @dtraub – mail@dennistraub.de
a.k.a. The             Core Domain




Dennis Traub – Software Development Consultancy             @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
Presentation Layer


                                      Application Services


                            Domain / Business Model / BLL


                                       DAL / O/R-Mapper




Dennis Traub – Software Development Consultancy              @dtraub – mail@dennistraub.de
Pros of the „Best Practice Architecture“



             1    Easy to build


             2    Accepted by management


             3    Well-know throughall all seniority levels


             4    We know ist limitiations




Dennis Traub – Software Development Consultancy               @dtraub – mail@dennistraub.de
Cons of the „Best Practice Architecture“



             1    Limited scalability


             2    Loss of intent


             3    Lazy loading, locking, race conditions, …


             4    Few classes do way too much stuff




Dennis Traub – Software Development Consultancy               @dtraub – mail@dennistraub.de
And:
                      If the model is basically
                                         CRUD
                   where does Business Logic live?




Dennis Traub – Software Development Consultancy          @dtraub – mail@dennistraub.de
Domain Model Responsibilities



             1    Business Logic                  6   Aggregation

             2    Validation                      7   Presentation

             3    Structure                       8   Persistence

             4    Projection                      9   Processes

             5    Associations                    10 Lazy / Eager Loading




Dennis Traub – Software Development Consultancy                      @dtraub – mail@dennistraub.de
the S in SOLID




Dennis Traub – Software Development Consultancy        @dtraub – mail@dennistraub.de
SINGLE RESPONSIBILITY PRINCIPLE
             Just because you can doesn‘t mean you should

Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
why do we try to build
                 One Model to Rule Them All?




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
reading and writing are
              completely different from eachother




Dennis Traub – Software Development Consultancy             @dtraub – mail@dennistraub.de
Sometimes it‘s cheaper to do two things
                       than dealing with the trade-offs




Dennis Traub – Software Development Consultancy      @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
Command Query Separation




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Command Query Separation (CQS)




                        „Every method should either be
        a command that       performs an action or
             a query that returns data to the caller“

                                                  -- Dr. Bertrand Meyer




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Command Query Separation (CQS)




                                         in other words:
                            Asking a question should
                            not change the answer




Dennis Traub – Software Development Consultancy            @dtraub – mail@dennistraub.de
Command/Query
                   Responsability Segregation




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
CQS on an Architectural Level



                          Queries                                  Commands




         Project Details               Project List                 Project




                                                      Data Store


Dennis Traub – Software Development Consultancy                    @dtraub – mail@dennistraub.de
Queries and Projection (Read)


                                            Show something




                                            Thin Read Layer



                                                  Data Store

Dennis Traub – Software Development Consultancy                @dtraub – mail@dennistraub.de
Thin Read Layer



             Concerns:

             1    Filtering, Scope


             2    Data Presentation




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Thin Read Layer



             Attributes:

             1    Data-Oriented


             2    Indexable

             3    Can be denormalized and distributed for fast access


             4    Only Structure




Dennis Traub – Software Development Consultancy                @dtraub – mail@dennistraub.de
Behavior and Modification (Write)


                                              Do something



                                          Command Handler


                                                  Domain Model


                                                  O/R-Mapper



                                                  Data Store

Dennis Traub – Software Development Consultancy                  @dtraub – mail@dennistraub.de
Domain Model



             Concerns:

             1    Business Logic


             2    Validation




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Domain Model



             Attributes:

             1    Normalized


             2    Transactional

             3    Object-Oriented


             4    Persistence Ignorant


             5    Only Behavior




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Commands



             Refactor Application Service Calls to Objects

             1    Serializable


             2    Can be enveloped

             3    Can be intercepted


             4    Can be enriched


             5    Communicate intent




Dennis Traub – Software Development Consultancy    @dtraub – mail@dennistraub.de
Command Handlers



             Refactored Application Service Methods:

             1    Unify interface to all application services


             2    Can be wrapped (e.g. Transaction, Authorization, Logging)

             3    Can be enveloped (e.g. REST)




Dennis Traub – Software Development Consultancy                 @dtraub – mail@dennistraub.de
Command Handlers




                                         Rule of thumb:
      One Command Handler per use Case




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
This is CQRS


                                                    Client


       Commands                                                                 Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                            Thin Read Layer



                                                  Data Store

Dennis Traub – Software Development Consultancy                       @dtraub – mail@dennistraub.de
BREAK




Dennis Traub – Software Development Consultancy      @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
Domain Event




Dennis Traub – Software Development Consultancy    @dtraub – mail@dennistraub.de
Domain Event




                              captures the memory of
       something interesting affecting the domain




Dennis Traub – Software Development Consultancy        @dtraub – mail@dennistraub.de
Domain Event




                the essence is to capture         events that
                       trigger a change to the state




Dennis Traub – Software Development Consultancy        @dtraub – mail@dennistraub.de
Domain Event




                       event objects are processed to
                        cause the respective change




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Domain Event




                  and stored to provide an        audit log




Dennis Traub – Software Development Consultancy       @dtraub – mail@dennistraub.de
Shopping Cart




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
we don‘t know:
                          What led to this state?




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Added
            to
           Cart




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Added                  Added
            to                     to
           Cart                   Cart




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Added                  Added            Added
            to                     to               to
           Cart                   Cart             Cart




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Added                  Added            Added   Removed
            to                     to               to      from
           Cart                   Cart             Cart     Cart




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Which model contains more information?




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Shopping Cart




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Added                  Added            Added   Removed
            to                     to               to      from
           Cart                   Cart             Cart     Cart




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
what if we
                   capture every single event?




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
… and reproduce state from
                              this stream of events?




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
An Event Stream …




        Added             Added            Added   Removed
          to                to               to      from
         Cart              Cart             Cart     Cart




Dennis Traub – Software Development Consultancy              @dtraub – mail@dennistraub.de
An Event Stream …




        Added             Added            Added   Removed
          to                to               to      from
         Cart              Cart             Cart     Cart




                                                             Shopping
                                                               Cart
        Can be projected into:




Dennis Traub – Software Development Consultancy                   @dtraub – mail@dennistraub.de
An Event Stream …




        Added             Added            Added   Removed
          to                to               to      from
         Cart              Cart             Cart     Cart



                                                   Sales History
                                                            Shopping
                                                               Cart
              … or into this:




Dennis Traub – Software Development Consultancy                  @dtraub – mail@dennistraub.de
An Event Stream …




        Added             Added            Added   Removed
          to                to               to      from
         Cart              Cart             Cart     Cart



                                                   Sales History
                                                      Audit Trail
                                                            Shopping
                                                               Cart
              … or into this:




Dennis Traub – Software Development Consultancy                  @dtraub – mail@dennistraub.de
An Event Stream …




        Added             Added            Added     Removed
          to                to               to        from
         Cart              Cart             Cart       Cart



                                                     Sales History
                                                        Audit Trail
                                                              Shopping
                                                   Campaign Effectiveness
                                                                 Cart
              … or into this:




Dennis Traub – Software Development Consultancy                    @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
Client


       Commands                                                                 Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                            Thin Read Layer



                                                  Data Store

Dennis Traub – Software Development Consultancy                       @dtraub – mail@dennistraub.de
Client


       Commands                                                                 Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer


                                                               SQL
                       Data Store                             Views


Dennis Traub – Software Development Consultancy                       @dtraub – mail@dennistraub.de
Client


       Commands                                                                 Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer

                                                                          INNER JOIN …
                                                               SQL        LEFT OUTER JOIN …
                       Data Store                             Views       UNION …
                                                                          GROUP BY …

Dennis Traub – Software Development Consultancy                       @dtraub – mail@dennistraub.de
Client


       Commands                                                              Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer

                                                      SELECT * FROM …
                                                                          Table
                       Data Store                                          per
                                                                          Query



Dennis Traub – Software Development Consultancy                    @dtraub – mail@dennistraub.de
Client


       Commands                                                             Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer


                                                                         Table
                       Data Store                                         per
                                                                         Query



Dennis Traub – Software Development Consultancy                   @dtraub – mail@dennistraub.de
Client


       Commands                                                             Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer
                                  Event

                                                                         Table
                       Data Store                                         per
                                                                         Query



Dennis Traub – Software Development Consultancy                   @dtraub – mail@dennistraub.de
Client


       Commands                                                             Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer
                                  Event
                                    Event
                                                                         Table
                       Data Store                                         per
                                                                         Query



Dennis Traub – Software Development Consultancy                   @dtraub – mail@dennistraub.de
Client


       Commands                                                             Queries

                    Command Handlers


                     Domain Model

                         O/R Mapper                        Thin Read Layer
                                  Event
                                    Event
                                      Event
                                                                         Table
                       Data Store                                         per
                                                                         Query



Dennis Traub – Software Development Consultancy                   @dtraub – mail@dennistraub.de
Simplified:




Dennis Traub – Software Development Consultancy       @dtraub – mail@dennistraub.de
Events

                             Domain                          Read Model




            Commands                                                 DTOs




                                                    Client




Dennis Traub – Software Development Consultancy                    @dtraub – mail@dennistraub.de
Some Code




Dennis Traub – Software Development Consultancy    @dtraub – mail@dennistraub.de
Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
if ISBN exists in ShoppingCartDetails
          Increase Amount
      else
          Add New Row




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
if Shopping Cart ID exists in ShoppingCartSummary
          Increase NumberOfItems
          Add OfferedPrice to TotalAmount
      else
          Add New Row




Dennis Traub – Software Development Consultancy      @dtraub – mail@dennistraub.de
Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
if Amount > 1 ShoppingCartDetails
          Decrease Amount
      else
          Delete Row




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Subtract OfferedPrice to TotalAmount
                   Decrease NumberOfItems




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Testability




Dennis Traub – Software Development Consultancy        @dtraub – mail@dennistraub.de
Testing the Write Model



                    Command Handlers


                     Domain Model




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Testing the Write Model



                    Command Handlers

                                                  Given that certain Events happened
                     Domain Model




Dennis Traub – Software Development Consultancy              @dtraub – mail@dennistraub.de
Testing the Write Model

                                                  When a specific Command is sent



                    Command Handlers

                                                  Given that certain Events happened
                     Domain Model




Dennis Traub – Software Development Consultancy              @dtraub – mail@dennistraub.de
Testing the Write Model

                                                               When a specific Command is sent



                    Command Handlers

                                                              Given that certain Events happened
                     Domain Model


                                          Event
                                            Event


                                          Then certain Events (and only those!) should be emitted


Dennis Traub – Software Development Consultancy                           @dtraub – mail@dennistraub.de
Testing the Read Model




                                                  Thin Read Layer


                                                                Table
                                                                 per
                                                                Query



Dennis Traub – Software Development Consultancy          @dtraub – mail@dennistraub.de
Testing the Read Model




                                                  Thin Read Layer

      Given that certain Events happened
                                                                Table
                                                                 per
                                                                Query



Dennis Traub – Software Development Consultancy          @dtraub – mail@dennistraub.de
Testing the Read Model




                                                  Thin Read Layer

      Given that certain Events happened
                                                                Table
                                                                 per
      When a specific Event happens                             Query



Dennis Traub – Software Development Consultancy          @dtraub – mail@dennistraub.de
Testing the Read Model




      Then each query should return the expected results




                                                   Thin Read Layer

      Given that certain Events happened
                                                                  Table
                                                                   per
      When a specific Event happens                               Query



Dennis Traub – Software Development Consultancy            @dtraub – mail@dennistraub.de
Main Values




Dennis Traub – Software Development Consultancy     @dtraub – mail@dennistraub.de
Main Values




                                           additive only
                       we don‘t            lose information




Dennis Traub – Software Development Consultancy            @dtraub – mail@dennistraub.de
Main Values




                                 linearly scalable and
                           distributable Read Model




Dennis Traub – Software Development Consultancy          @dtraub – mail@dennistraub.de
Main Values




                     every new view can be created
                      from the beginning of time




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Main Values




                       We can come up with new questions
                                        at any time




Dennis Traub – Software Development Consultancy       @dtraub – mail@dennistraub.de
Main Values




                     built-in integration model




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
Proven Technology




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
mature business models
                           move away from Update/Delete
                 and become purely                transactional




Dennis Traub – Software Development Consultancy           @dtraub – mail@dennistraub.de
Human resources
                                  Medicine
                                Bookkeeping
                                  Banking
                                  Finances
                                 Government
                                      …


Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de
AGENDA



 DDD Recap                                          CQRS                                    Step by Step
  the strategic patterns                  probably simpler than you think!                      towards CQRS


          1                     2                      3                      4                     5                6
                   „Best Practice“                              Domain Events                                       End
                     what we call an Architecture                    are first class citizens                  Questions & Answers




Dennis Traub – Software Development Consultancy                                                     @dtraub – mail@dennistraub.de
?

                                        QUESTIONS



Dennis Traub – Software Development Consultancy       @dtraub – mail@dennistraub.de
THANK YOU!




Dennis Traub – Software Development Consultancy   @dtraub – mail@dennistraub.de

Contenu connexe

Similaire à An Introduction to CQRS

Finding balance of DDD while your application grows
Finding balance of DDD while your application growsFinding balance of DDD while your application grows
Finding balance of DDD while your application grows
Carolina Karklis
 
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefieldITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
ITCamp
 
Software Development in 21st Century
Software Development in 21st CenturySoftware Development in 21st Century
Software Development in 21st Century
Henry Jacob
 

Similaire à An Introduction to CQRS (20)

Finding balance of DDD while your application grows
Finding balance of DDD while your application growsFinding balance of DDD while your application grows
Finding balance of DDD while your application grows
 
Sunny Tech 2019 - Craft Forever
Sunny Tech 2019 - Craft ForeverSunny Tech 2019 - Craft Forever
Sunny Tech 2019 - Craft Forever
 
Let's go to the whiteboard: how and why software developers use drawings
Let's go to the whiteboard: how and why software developers use drawingsLet's go to the whiteboard: how and why software developers use drawings
Let's go to the whiteboard: how and why software developers use drawings
 
Agile Architecture (Scrum + DevOps) by Milan Chheda
Agile Architecture (Scrum + DevOps) by Milan ChhedaAgile Architecture (Scrum + DevOps) by Milan Chheda
Agile Architecture (Scrum + DevOps) by Milan Chheda
 
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefieldITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
ITCamp 2012 - Sergiu Damian - Architectural decisions on the battlefield
 
DOMAIN DRIVER DESIGN
DOMAIN DRIVER DESIGNDOMAIN DRIVER DESIGN
DOMAIN DRIVER DESIGN
 
DDD In Agile
DDD In Agile   DDD In Agile
DDD In Agile
 
DDD knowledge sharing
DDD knowledge sharingDDD knowledge sharing
DDD knowledge sharing
 
DaraBaf
DaraBafDaraBaf
DaraBaf
 
Soliworks presentation
Soliworks presentationSoliworks presentation
Soliworks presentation
 
designertoday0809
designertoday0809designertoday0809
designertoday0809
 
designertoday0809
designertoday0809designertoday0809
designertoday0809
 
Design Driven Development
Design Driven DevelopmentDesign Driven Development
Design Driven Development
 
Software Development in 21st Century
Software Development in 21st CenturySoftware Development in 21st Century
Software Development in 21st Century
 
DNetZone Corporate profile
DNetZone Corporate profileDNetZone Corporate profile
DNetZone Corporate profile
 
Product Development Brochure
Product Development BrochureProduct Development Brochure
Product Development Brochure
 
DDD
DDDDDD
DDD
 
Intergen Welcome Keynote: Microsoft Dynamics - Bringing it all together
Intergen Welcome Keynote: Microsoft Dynamics - Bringing it all togetherIntergen Welcome Keynote: Microsoft Dynamics - Bringing it all together
Intergen Welcome Keynote: Microsoft Dynamics - Bringing it all together
 
BDD presentation
BDD presentationBDD presentation
BDD presentation
 
Dashlane Triple Track
Dashlane Triple TrackDashlane Triple Track
Dashlane Triple Track
 

Plus de Dennis Traub

Plus de Dennis Traub (8)

Application Integration Patterns (not only) for Microservices
Application Integration Patterns (not only) for MicroservicesApplication Integration Patterns (not only) for Microservices
Application Integration Patterns (not only) for Microservices
 
Serverless SecOps Automation on AWS at AWS UG Krakow, Poland
Serverless SecOps Automation on AWS at AWS UG Krakow, PolandServerless SecOps Automation on AWS at AWS UG Krakow, Poland
Serverless SecOps Automation on AWS at AWS UG Krakow, Poland
 
Serverless Security Automation on AWS - Hamburg AWS User Group
Serverless Security Automation on AWS - Hamburg AWS User GroupServerless Security Automation on AWS - Hamburg AWS User Group
Serverless Security Automation on AWS - Hamburg AWS User Group
 
Cloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
Cloud ist keine Strategie - Keynote des AWS Cloud Day, SolingenCloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
Cloud ist keine Strategie - Keynote des AWS Cloud Day, Solingen
 
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
Cloud ist keine Strategie - AWS Tech Community Summit Cologne, 2017
 
Taming the Monolith - Microservices Meetup Hamburg
Taming the Monolith - Microservices Meetup HamburgTaming the Monolith - Microservices Meetup Hamburg
Taming the Monolith - Microservices Meetup Hamburg
 
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
DDD / Microservices @ Trivento Spring Camp, Utrecht, 2015
 
DDD Modeling Workshop
DDD Modeling WorkshopDDD Modeling Workshop
DDD Modeling Workshop
 

Dernier

Dernier (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

An Introduction to CQRS

  • 1. An Introduction to Command/Query Responsibility Segregation presented by Dennis Traub .NET Stammtisch Konstanz/Kreuzlingen - 20. März 2013 Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 2. Who‘s that guy? Dennis Traub Founder, Owner, CEO, Single Employee of D. Traub Software Development Consultancy Consultant, developer, trainer, speaker 39 years old Married, father of one 20+ years in software development Hire me at mail@dennistraub.de @dtraub Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 3. AGENDA DDD Recap CQRS Break Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 4. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 5. The „Blue Book“ by Eric Evans Image Placeholder Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 6. Domain-Driven Design Domain Service Value Object Aggregate Specification The well- known Reposit ory Patterns Entity Factory And so on . . Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 7. Domain-Driven Design Ubiquit ous Language Core Domain Domain Expert Bounded Context The important Domain Model Patterns (Generic) Subdomains Context Map And so on . . Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 8. don‘t build one big model that works for everyone Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 9. the tactical Building Blocks we don‘t use DDD when there is no value in formalizing the problem Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 10. we only use DDD in parts where we get a competitive advantage Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 11. we focus our effort and resources on the most important subdomain Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 12. a.k.a. The Core Domain Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 13. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 14. Presentation Layer Application Services Domain / Business Model / BLL DAL / O/R-Mapper Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 15. Pros of the „Best Practice Architecture“ 1 Easy to build 2 Accepted by management 3 Well-know throughall all seniority levels 4 We know ist limitiations Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 16. Cons of the „Best Practice Architecture“ 1 Limited scalability 2 Loss of intent 3 Lazy loading, locking, race conditions, … 4 Few classes do way too much stuff Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 17. And: If the model is basically CRUD where does Business Logic live? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 18. Domain Model Responsibilities 1 Business Logic 6 Aggregation 2 Validation 7 Presentation 3 Structure 8 Persistence 4 Projection 9 Processes 5 Associations 10 Lazy / Eager Loading Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 19. the S in SOLID Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 20. SINGLE RESPONSIBILITY PRINCIPLE Just because you can doesn‘t mean you should Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 21. why do we try to build One Model to Rule Them All? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 22. reading and writing are completely different from eachother Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 23. Sometimes it‘s cheaper to do two things than dealing with the trade-offs Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 24. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 25. Command Query Separation Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 26. Command Query Separation (CQS) „Every method should either be a command that performs an action or a query that returns data to the caller“ -- Dr. Bertrand Meyer Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 27. Command Query Separation (CQS) in other words: Asking a question should not change the answer Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 28. Command/Query Responsability Segregation Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 29. CQS on an Architectural Level Queries Commands Project Details Project List Project Data Store Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 30. Queries and Projection (Read) Show something Thin Read Layer Data Store Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 31. Thin Read Layer Concerns: 1 Filtering, Scope 2 Data Presentation Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 32. Thin Read Layer Attributes: 1 Data-Oriented 2 Indexable 3 Can be denormalized and distributed for fast access 4 Only Structure Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 33. Behavior and Modification (Write) Do something Command Handler Domain Model O/R-Mapper Data Store Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 34. Domain Model Concerns: 1 Business Logic 2 Validation Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 35. Domain Model Attributes: 1 Normalized 2 Transactional 3 Object-Oriented 4 Persistence Ignorant 5 Only Behavior Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 36. Commands Refactor Application Service Calls to Objects 1 Serializable 2 Can be enveloped 3 Can be intercepted 4 Can be enriched 5 Communicate intent Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 37. Command Handlers Refactored Application Service Methods: 1 Unify interface to all application services 2 Can be wrapped (e.g. Transaction, Authorization, Logging) 3 Can be enveloped (e.g. REST) Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 38. Command Handlers Rule of thumb: One Command Handler per use Case Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 39. This is CQRS Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Data Store Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 40. BREAK Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 41. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 42. Domain Event Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 43. Domain Event captures the memory of something interesting affecting the domain Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 44. Domain Event the essence is to capture events that trigger a change to the state Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 45. Domain Event event objects are processed to cause the respective change Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 46. Domain Event and stored to provide an audit log Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 47. Shopping Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 48. we don‘t know: What led to this state? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 49. Added to Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 50. Added Added to to Cart Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 51. Added Added Added to to to Cart Cart Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 52. Added Added Added Removed to to to from Cart Cart Cart Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 53. Which model contains more information? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 54. Shopping Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 55. Added Added Added Removed to to to from Cart Cart Cart Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 56. what if we capture every single event? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 57. … and reproduce state from this stream of events? Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 58. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart Cart Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 59. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart Cart Shopping Cart Can be projected into: Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 60. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart Cart Sales History Shopping Cart … or into this: Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 61. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart Cart Sales History Audit Trail Shopping Cart … or into this: Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 62. An Event Stream … Added Added Added Removed to to to from Cart Cart Cart Cart Sales History Audit Trail Shopping Campaign Effectiveness Cart … or into this: Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 63. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 64. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Data Store Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 65. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer SQL Data Store Views Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 66. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer INNER JOIN … SQL LEFT OUTER JOIN … Data Store Views UNION … GROUP BY … Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 67. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer SELECT * FROM … Table Data Store per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 68. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Table Data Store per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 69. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Table Data Store per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 70. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Event Table Data Store per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 71. Client Commands Queries Command Handlers Domain Model O/R Mapper Thin Read Layer Event Event Event Table Data Store per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 72. Simplified: Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 73. Events Domain Read Model Commands DTOs Client Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 74. Some Code Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 75. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 76. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 77. if ISBN exists in ShoppingCartDetails Increase Amount else Add New Row Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 78. if Shopping Cart ID exists in ShoppingCartSummary Increase NumberOfItems Add OfferedPrice to TotalAmount else Add New Row Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 79. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 80. Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 81. if Amount > 1 ShoppingCartDetails Decrease Amount else Delete Row Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 82. Subtract OfferedPrice to TotalAmount Decrease NumberOfItems Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 83. Testability Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 84. Testing the Write Model Command Handlers Domain Model Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 85. Testing the Write Model Command Handlers Given that certain Events happened Domain Model Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 86. Testing the Write Model When a specific Command is sent Command Handlers Given that certain Events happened Domain Model Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 87. Testing the Write Model When a specific Command is sent Command Handlers Given that certain Events happened Domain Model Event Event Then certain Events (and only those!) should be emitted Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 88. Testing the Read Model Thin Read Layer Table per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 89. Testing the Read Model Thin Read Layer Given that certain Events happened Table per Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 90. Testing the Read Model Thin Read Layer Given that certain Events happened Table per When a specific Event happens Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 91. Testing the Read Model Then each query should return the expected results Thin Read Layer Given that certain Events happened Table per When a specific Event happens Query Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 92. Main Values Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 93. Main Values additive only we don‘t lose information Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 94. Main Values linearly scalable and distributable Read Model Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 95. Main Values every new view can be created from the beginning of time Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 96. Main Values We can come up with new questions at any time Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 97. Main Values built-in integration model Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 98. Proven Technology Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 99. mature business models move away from Update/Delete and become purely transactional Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 100. Human resources Medicine Bookkeeping Banking Finances Government … Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 101. AGENDA DDD Recap CQRS Step by Step the strategic patterns probably simpler than you think! towards CQRS 1 2 3 4 5 6 „Best Practice“ Domain Events End what we call an Architecture are first class citizens Questions & Answers Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 102. ? QUESTIONS Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de
  • 103. THANK YOU! Dennis Traub – Software Development Consultancy @dtraub – mail@dennistraub.de