Command Query Responsibility Segregation
Or in its weaker form Command Query Segregation (CQS)
In n-tier architecture, operations are clustered - all operations on User go in UserService and UserRepository
Hard to do higher-order operations e.g. decorators.
An aside - example
Simple RESTful UserController
How to know what is updated?
Fetching data is too generic
Semantic actions
Implementation in the service layer
Explosion of dependencies
Start with the original method
Obvious intention and data (serializable method call)
Handling commands is isolated
Refactoring to command
Refactoring to command
Time to update our user controller
Use Dependency Injection
Execute, Handle, Do, _void, Task, Task<Either<Error, Unit>>, Task<Result>IQuery<TQueryResult>IQueryHandler<TQuery, TQueryResult>Either you specialize them too much or have generic repository
Not all reads are equal
Specification pattern can be replaced with query object pattern
Reading in commands is _different_ than reading from the client!
Can use raw SQL (or Dapper as opposed to EF in the command model)
An example query
Separating databases
Key idea is that we can split read from write model


[dbo].[Schedule] on every change[dbo].[Cache] - it might take a minuteUnit testing is easy
Easy to do integration tests!
Easy to do integration tests!
Decorators on commands extract cross cutting concerns.
Handler returns another handler!
Example usage
Refactoring traditional "service" object to CQRS involves separating the read part from the write part. After that, write part is split into task based methods as opposed to resource based methods. We can have multiple POST/PUT/PATCH methods that have semantic meaning.