MediatR CRUD Template
dotnet new mediatr-crud generates MediatR-based CRUD commands and queries for one entity, following the CQRS pattern. It is meant for solutions created with the Milvonion Template.
Tech stack
- .NET 11
- MediatR
- FluentValidation
- The Milvonion solution layout
Installation
dotnet new install Milvasoft.Templates
Usage
Run the command from the solution root (the folder of the .slnx file). The files land in the solution's own folders:
dotnet new mediatr-crud -n Product --projectName MyApp --pluralName Products --entityKeyType int
Parameters
| Parameter | Description |
|---|---|
-n | Singular entity name (for example Product, Category). |
--projectName | Root namespace of the application (for example MyApp). |
--pluralName | Plural name used in namespaces (for example Products). |
--entityKeyType | Key type of the entity: int, long or Guid. Default int. |
Generated files
src/{projectName}.Api/Controllers/{pluralName}Controller.cs
src/{projectName}.Application/Dtos/{name}Dtos/{name}DetailDto.cs, {name}ListDto.cs
src/{projectName}.Application/Features/{pluralName}/Create{name}/Create{name}Command.cs, ...Handler.cs, ...Validator.cs
src/{projectName}.Application/Features/{pluralName}/Update{name}/...
src/{projectName}.Application/Features/{pluralName}/Delete{name}/...
src/{projectName}.Application/Features/{pluralName}/Get{name}Detail/...
src/{projectName}.Application/Features/{pluralName}/Get{name}List/...
Every namespace matches its folder, so the solution's architecture tests pass once the steps below are done.
Before the first build
The generated code relies on a few things you add once per entity. For -n Product --pluralName Products:
- Entity:
src/MyApp.Domain/Product.cs(with[Table(TableNames.Products)], for example aFullAuditableEntity<int>with aNameproperty),TableNames.Products, andDbSet<Product> ProductsinMyAppDbContext. - Message keys:
MessageKey.ProductNotFound = nameof(ProductNotFound)andMessageKey.Product = "Global.Product". MapProductNotFoundto 404 inResponseStatusCodes. - Activities:
UserActivity.CreateProduct,UpdateProductandDeleteProduct, with explicit values that are never reused (and[Description]plus XML docs like the other members). - Permissions:
PermissionCatalog.ProductManagementwithList,Detail,Create,UpdateandDelete. - Resx entries in both
SharedResource.tr-TR.resxandSharedResource.en-US.resx:ProductNotFound,Global.Product,ProductManagement,PG.ProductManagement.MissingResxKeyFinderand theResxTestsunit test report any key that is still missing. - Migration for the new table:
dotnet ef migrations add AddProducts --project src/MyApp.Api
Notes
- The detail, update and delete handlers return
MessageKey.{name}NotFound(404 throughResponseStatusCodes) when the record does not exist. - The list handler clamps the page size (default 20, at most
GlobalConstant.MaxRowCount). - The update and delete validators do not apply the seed record protection; only the seed tables (
Users,Roles,Permissions,Languages) keep their IDs below 21.
Uninstall
dotnet new uninstall Milvasoft.Templates