Overview
The Intent.Application.MediatR.CRUD module automatically implements common CRUD (Create, Read, Update, Delete) operations in your command and query handlers. Instead of manually writing repository code, this module detects CRUD patterns and generates the implementation for you.This module works by analyzing the names and mappings of your commands/queries to determine their intent (e.g., “CreateCustomer” → Create operation).
Supported CRUD Operations
The module recognizes and implements these operations:Create
Commands:
Create*, Add*, New*Generates code to instantiate and add entities to the repository.Read
Queries:
Get*, Find*, Lookup*Generates code to retrieve entities by ID or other criteria.Update
Commands:
Update*, Modify*, Edit*Generates code to find and update existing entities.Delete
Commands:
Delete*, Remove*Generates code to find and remove entities.How It Works
1. Name-Based Detection
The module examines your command/query names:2. Mapping Detection
It also checks if your command/query is mapped to a domain entity in the designer:- Command mapped to
Customerentity → CRUD operation likely - Return type is a DTO mapped to the same entity → Mapping included
3. Code Generation
When both conditions are met, the module generates the full handler implementation.Generated Create Implementation
Given aCreateCustomerCommand mapped to a Customer entity:
CreateCustomerCommandHandler.cs
- Automatically injects required repository
- Maps command properties to entity properties
- Handles nested composite entities
- Returns entity ID or mapped DTO
Generated GetById Implementation
Given aGetCustomerByIdQuery mapped to a Customer entity:
GetCustomerByIdQueryHandler.cs
- Finds entity by ID from query parameters
- Throws
NotFoundExceptionif not found (or returns null if nullable) - Maps entity to DTO using AutoMapper or Mapperly
Generated Update Implementation
Given anUpdateCustomerCommand mapped to a Customer entity:
UpdateCustomerCommandHandler.cs
- Finds existing entity
- Updates properties from command
- Handles domain operations and value objects
Generated Delete Implementation
Given aDeleteCustomerCommand with an Id property:
DeleteCustomerCommandHandler.cs
- Finds entity before deletion
- Removes from repository
- Throws exception if not found
Advanced Features
Nested Composite Entities
For entities with compositional relationships:CreateOrderLineCommand
- Loads aggregate root
- Adds child entity to collection
- Updates aggregate root
Value Object Mapping
For properties mapped to value objects:- Generates factory methods for value objects
- Handles both single and collection mappings
Pagination Support
ForGetAll queries with pagination:
GetCustomersQuery
Requirements for CRUD Generation
For the module to generate CRUD implementations, you need:- Naming Convention: Command/query name matches a CRUD pattern
- Domain Mapping: Command/query is mapped to a domain entity
- DTO Mapping: Return type (for queries) is a DTO mapped to the same entity
- Repository Available: A repository interface exists for the entity
- Entity Settings: Domain entity has
Ensure Private Property Settersdisabled
When CRUD Is NOT Generated
The module will not generate CRUD code when:- Command/query name doesn’t match a CRUD pattern
- No mapping to domain entity exists
Ensure Private Property Settersis enabled- Repository interface is missing
- You’ve added custom Domain Interactions in the designer
Dependencies
This module requires:Intent.Application.MediatR- Base MediatR implementationIntent.Application.DomainInteractions- Domain interaction patternsIntent.Common.Types- Common type definitionsIntent.Modelers.Domain- Domain entity modeling
Intent.Application.AutoMapperorIntent.Application.Dtos.Mapperly- For DTO mapping
Related Modules
MediatR
Core CQRS implementation
Domain Interactions
Manual domain interaction patterns
Repositories
Repository pattern implementation
Troubleshooting
CRUD Code Not Generating?
Check these:- Command/query name matches CRUD pattern (Create*, Get*, Update*, Delete*)
- Mapping exists in the Services Designer to a domain entity
- Domain Settings →
Ensure Private Property Settersis disabled - Repository interface exists for the entity
- Return type (for queries) is a DTO mapped to the same entity
Warning Comments in Generated Code
You may see warnings like:- Missing mappings between DTO and entity properties
- Association type mismatch (aggregation vs. composition)
- Value object mapping issues
Additional Resources
- Module ID:
Intent.Application.MediatR.CRUD - Repository Pattern Documentation
