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: