Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library...
Transcript of Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library...
![Page 1: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/1.jpg)
Optimizing the query stack of CQRS-layered applications
Dino EspositoJetBrains
[email protected]@despos
facebook.com/naa4e
![Page 2: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/2.jpg)
Drink Eat
![Page 3: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/3.jpg)
QueryCommand
![Page 4: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/4.jpg)
CQRS ≈ Separating Stacks
![Page 5: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/5.jpg)
Domain layer
Presentation layer
Application layer
Infrastructure layer
Canonical
Layered Architecture
Domain layer
Presentation layer
Application layer
Infrastructure layer
Commands Queries
Data
access
+
DTODomain layer Domain layer
CQRS
Layered Architecture
![Page 6: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/6.jpg)
Conceptual model split into separate
models for update and display
Focus on tasks
Build the data model right for the task
Stop god-mode development
Stop wondering if your model is perfect
![Page 7: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/7.jpg)
CQRS
Architectural spinoffs
![Page 8: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/8.jpg)
Presentation layer
VIEWSFORMS
Infrastructure layer
![Page 9: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/9.jpg)
Domain layer
Presentation layer
Application layer
Infrastructure layer
Commands Queries
Data
access
+
DTO
CQRS
Layered Architecture
Infrastructure layer
![Page 10: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/10.jpg)
Synchronous
Every command triggers sync
updates
Asynchronous
Every command triggers async
updates
Scheduled
A job runs periodically and
updates the read storage
On-demand
Updates triggered by
requests (if older enough)
Automatically
up-to-date
Eventually
up-to-date
Controlled
stalenessControlled
up-to-date
![Page 11: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/11.jpg)
CQRS for Plain CRUD Applications
Presentation
&
Application
Presentation
&
Application
YourApp.CommandStack YourApp.ReadStack
![Page 12: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/12.jpg)
Use the code that does the job
Existing code
Existing products
Existing skills
COMMAND stack
Known patterns
Known techniques
Known practices
![Page 13: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/13.jpg)
O/RM of choice
LINQ
Database in use
READ stackUse the code that does the job
SQL queries
Data binding/grids
JSON / OData
![Page 14: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/14.jpg)
CQRS for Rich CRUD Applications
Presentation
&
Application
Presentation
&
Application
YourApp.CommandStack YourApp.ReadStack
![Page 15: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/15.jpg)
COMMAND STACK
Request for action
Log of actions
READ
DATA
STORE
Aggregate ID Data
Aggregate ID Data
Request for data Formatted data
DETAILED VIEW
![Page 16: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/16.jpg)
Logging the list of commands makes any
CRUD an historical CRUD
![Page 17: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/17.jpg)
Logging the list of
commands lets you
keep track of the
latest without
forgetting anything
that happened in
the past.
PS: if you really want to forget … go with plain CRUD.
![Page 18: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/18.jpg)
SELECT relevant business events
FROM System
WHERE TimeStamp BETWEEN Time1 AND Time2
SELECT relevant business events
FROM System
WHERE TimeStamp NEWER THAN Time1
SELECT relevant business events
FROM System
WHERE TimeStamp OLDER THAN Time1
REPLAY EVENTS
REBUILD THE STATECQRS leads to
Business Intelligence
![Page 19: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/19.jpg)
Ubiquitous Language
When the language of the business is
fully replicated in the codebase
![Page 20: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/20.jpg)
Ubiquitous
Language Query stack
public class Database : IDisposable{
private readonly QueryDbContext _db = new QueryDbContext();
public IQueryable<Customer> Customers{
get { return _db.Customers; } }public void Dispose() {
_db.Dispose(); }
}
![Page 21: Optimizing the query stack of CQRS-layered applicationssddconf.com › brands › sdd › library › sdd16_esposito_querystack.pdf · Optimizing the query stack of CQRS-layered applications](https://reader033.fdocuments.us/reader033/viewer/2022060314/5f0baddc7e708231d431af95/html5/thumbnails/21.jpg)
DEMOWeb API service using OData and domain-friendly
extension methods for LINQ queries