Ana içeriğe geç
Bu sayfa henüz Türkçeye çevrilmedi; İngilizce sürümü gösteriliyor.
Versiyon: 11.0

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​

ParameterDescription
-nSingular entity name (for example Product, Category).
--projectNameRoot namespace of the application (for example MyApp).
--pluralNamePlural name used in namespaces (for example Products).
--entityKeyTypeKey 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 a FullAuditableEntity<int> with a Name property), TableNames.Products, and DbSet<Product> Products in MyAppDbContext.
  • Message keys: MessageKey.ProductNotFound = nameof(ProductNotFound) and MessageKey.Product = "Global.Product". Map ProductNotFound to 404 in ResponseStatusCodes.
  • Activities: UserActivity.CreateProduct, UpdateProduct and DeleteProduct, with explicit values that are never reused (and [Description] plus XML docs like the other members).
  • Permissions: PermissionCatalog.ProductManagement with List, Detail, Create, Update and Delete.
  • Resx entries in both SharedResource.tr-TR.resx and SharedResource.en-US.resx: ProductNotFound, Global.Product, ProductManagement, PG.ProductManagement. MissingResxKeyFinder and the ResxTests unit 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 through ResponseStatusCodes) 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