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 2Command 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.
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.0to2.0.0
Upgrade command/query handlers:
- Upgrade the project target framework from
tonet461netstandard2.0or greater - Add a
CancellationTokenparameter to theHandleAsyncmethods in classes that implementICommandHandler<TCommand>,ICommandHandler<TCommand, TResult>andIQueryHandler<TQuery, TResult>
Upgrade AspNet.WebApi:
- Migrate from
CommandQuery.AspNet.WebApitoCommandQuery.AspNetCore
Upgrade AspNetCore:
- Consider to upgrade the project target framework to
netcoreapp3.1ornet5.0 - Consider to use the extension methods
AddCommandControllersandAddQueryControllersinStartup.cs
Upgrade AWSLambda:
- Upgrade the project target framework to
netcoreapp3.1 - Change the method invocation on
CommandFunctionandQueryFunctionfromtoHandleHandleAsync - Change the argument on
HandleAsyncmethods fromtoILambdaContextILambdaLogger - Consider to use the extension methods
AddCommandFunctionandAddQueryFunctiononIServiceCollection - Consider to use the
JsonSerializerOptionsconstructor argument inCommandFunctionandQueryFunctionto configure JSON serialization/deserialization
Upgrade AzureFunctions:
- Upgrade the project target framework to
netcoreapp3.1ornet5.0 - Change the method invocation on
CommandFunctionandQueryFunctionfromtoHandleHandleAsync - Consider to use the extension methods
AddCommandControllersandAddQueryControllersinStartup.cs/Program.cs - Consider to use the
CancellationTokenargument onHandleAsyncmethods innetcoreapp3.1projects - Consider to use the
JsonSerializerSettings/JsonSerializerOptionsconstructor argument inCommandFunctionandQueryFunctionto configure JSON serialization/deserialization
Upgrade Client:
- Change the method invocation on
CommandClientandQueryClientfromtoPostPostAsyncand fromtoGetGetAsync - Consider to use the
AddHttpClientextension method onIServiceCollectionto create theCommandClientandQueryClient(see sample) - Consider to use the
CancellationTokenargument to methods inCommandClientandQueryClient
Validation:
- Consider to use the
AssertConfigurationIsValidmethod onCommandProcessorandQueryProcessorto validate handler and type configuration
Inspired by Steven van Deursen blog posts: