CQRS & Mediator Part 2: Domain scaffolding with Roslyn API and Dotnet CLI in this part we will be building a dotnet CLI tool which follows the CQRS and Mediator patterns to auto generate commands, queries, responses and handlers in the domain layer using Roslyn API for code generation.
Show help information:
scaffold -h
Concern: Name of the domain area you are working in i.e Orders, People, Invoice ect.
-c|--concern <NAME>
Operation: Name of the action you are taking in your concern i.e GetById, GetPagedResult for queries or Create, Patch, Update for commands.
-o|--operation <OPERATION>
OperationType: The type of CQRS operation you are scaffolding i.e command or query
-ot|--operationtype <TYPE>
GroupBy: Specify how to group the domain actions. Either by concern or operation: Group domain objects by [C] for concerns or [O] for operations, defaults to concerns
-g|--groupBy <TYPE>
By concern: i.e group by concern when parameters are -c Orders -o GetById -ot query
| YourDomainLayer
| Orders
| Handlers
| OrdersGetByIdHandler.cs
| Responses
| OrdersGetByIdResponse.cs
| Queries
| OrderGetByIdQuery.cs
By operation i.e group by operation when parameters are -c Orders -o GetById -ot query -g O
| YourDomainLayer
| Handler
| OrdersGetByIdHandler.cs
| Responses
| OrdersGetByIdResponse.cs
| Queries
| OrderGetByIdQuery.cs
Install using dotnet cli:
dotnet tool install --global CQRSAndMediator.Scaffolding
To uninstall use:
dotnet tool uninstall cqrsandmediator.scaffolding --global
Note: The tool requires that the project is setup already and that the actions are executed in the top level directory of where your domain layer directory is located.
Use case scaffold out the CRUD domain for an invoice:
The Create Command
scaffold -c Invoices -o Create -ot command
The Get By id query
scaffold -c Invoices -o GetById -ot query
The Patch Command
scaffold -c Invoices -o Patch -ot command
Update the nuget package version located in CQRSAndMediator.Scaffolding.csproj
dotnet build .
dotnet pack
then in the solution directory:
dotnet tool update -g cqrsandmediator.scaffolding --add-source ./nupkg --version x.x.x