Introducing CommandQuery
I was interested in learning more about Command Query Separation (CQS). I ended up with a NuGet package for ASP.NET Core microservice apps.
Introducing CommandQuery - #CQS for https://t.co/m497h64xIR Core #Microservices #aspdotnetcore https://t.co/qd8QHljnfx
— Henrik Lau Eriksson (@hlaueriksson) August 31, 2016
You can get the binaries and source at:
What?
Command Query Separation (CQS) for ASP.NET Core Microservices
- Build microservices that separate the responsibility of commands and queries.
- Focus on implementing the handlers for commands and queries, not on writing Web APIs.
- CommandQuery provides generic actions for handling the execution of all commands and queries.
- CommandQuery provides an API based on HTTP POST, not a REST API
Command Query Separation in a nutshell:
- Commands
- Writes (Create, Update, Delete) data
- Queries
- Reads and returns data
Microservices could be done with ASP.NET Core
How?
Basically:
- Create a new ASP.NET Core project
- Install the
CommandQuery
package from NuGetPM>
Install-Package CommandQuery
- Create controllers
- Inherit from
BaseCommandController
andBaseQueryController
- Inherit from
- Create commands and command handlers
- Implement
ICommand
andICommandHandler<in TCommand>
- Implement
- Create queries and query handlers
- Implement
IQuery<TResult>
andIQueryHandler<in TQuery, TResult>
- Implement
- Add the handlers to the dependency injection container
services.AddCommands(typeof(Startup).GetTypeInfo().Assembly);
services.AddQueries(typeof(Startup).GetTypeInfo().Assembly);
Sample code is available on GitHub: CommandQuery.Sample
Why?
I wanted to:
- Publish my very first NuGet package
- Try ASP.NET Core / .NET Core
- Learn more about CQS
Credit
The great articles by Steven van Deursen:
Disclaimer
I have not considered things like:
- Service Discovery
- Authentication / Authorization
- Caching
- etc.