Command Query Separation (CQS) for .NET and C#
- Build services that separate the responsibility of commands and queries
- Focus on implementing the handlers for commands and queries
- Create APIs with less boilerplate code
Available for:
๐ ASP.NET Core
โก AWS Lambda
โก Azure Functions
โก Google Cloud Functions
๐ ASP.NET Web API 2
Command Query Separation?
Queries: Return a result and do not change the observable state of the system (are free of side effects).
Commands: Change the state of a system but do not return a value.
โ Martin Fowler
In other words:
- Commands
- Writes (create, update, delete) data
- Queries
- Reads and returns data
The traditional approach that commands do not return a value is a bit inconvenient.
CommandQuery
has a pragmatic take and supports both commands with and without result ๐
Command Query Separation for .NET
- ๐ README: CommandQuery.md
- ๐ Samples:
Command Query Separation for ASP.NET Core
- ๐ README: CommandQuery.AspNetCore.md
- ๐ Samples:
Command Query Separation for AWS Lambda
- ๐ README: CommandQuery.AWSLambda.md
- ๐ Samples:
Command Query Separation for Azure Functions
- ๐ README: CommandQuery.AzureFunctions.md
- ๐ Samples:
Command Query Separation for Google Cloud Functions
- ๐ README: CommandQuery.GoogleCloudFunctions.md
- ๐ Samples:
Clients for CommandQuery APIs
- ๐ README: CommandQuery.Client.md
- ๐ Samples:
Command Query Separation for ASP.NET Web API 2
โ This package is no longer maintained and new versions will not be published
- ๐ README: CommandQuery.AspNet.WebApi.md
- ๐ Samples:
โฌ๏ธ Upgrading from version
1.0.0
to2.0.0
Upgrade command/query handlers:
- Upgrade the project target framework from
tonet461
netstandard2.0
or greater - Add a
CancellationToken
parameter to theHandleAsync
methods in classes that implementICommandHandler<TCommand>
,ICommandHandler<TCommand, TResult>
andIQueryHandler<TQuery, TResult>
Upgrade AspNet.WebApi:
- Migrate from
CommandQuery.AspNet.WebApi
toCommandQuery.AspNetCore
Upgrade AspNetCore:
- Consider to upgrade the project target framework to
netcoreapp3.1
ornet5.0
- Consider to use the extension methods
AddCommandControllers
andAddQueryControllers
inStartup.cs
Upgrade AWSLambda:
- Upgrade the project target framework to
netcoreapp3.1
- Change the method invocation on
CommandFunction
andQueryFunction
fromtoHandle
HandleAsync
- Change the argument on
HandleAsync
methods fromtoILambdaContext
ILambdaLogger
- Consider to use the extension methods
AddCommandFunction
andAddQueryFunction
onIServiceCollection
- Consider to use the
JsonSerializerOptions
constructor argument inCommandFunction
andQueryFunction
to configure JSON serialization/deserialization
Upgrade AzureFunctions:
- Upgrade the project target framework to
netcoreapp3.1
ornet5.0
- Change the method invocation on
CommandFunction
andQueryFunction
fromtoHandle
HandleAsync
- Consider to use the extension methods
AddCommandControllers
andAddQueryControllers
inStartup.cs
/Program.cs
- Consider to use the
CancellationToken
argument onHandleAsync
methods innetcoreapp3.1
projects - Consider to use the
JsonSerializerSettings
/JsonSerializerOptions
constructor argument inCommandFunction
andQueryFunction
to configure JSON serialization/deserialization
Upgrade Client:
- Change the method invocation on
CommandClient
andQueryClient
fromtoPost
PostAsync
and fromtoGet
GetAsync
- Consider to use the
AddHttpClient
extension method onIServiceCollection
to create theCommandClient
andQueryClient
(see sample) - Consider to use the
CancellationToken
argument to methods inCommandClient
andQueryClient
Validation:
- Consider to use the
AssertConfigurationIsValid
method onCommandProcessor
andQueryProcessor
to validate handler and type configuration
Inspired by Steven van Deursen blog posts: