Overview
TheIntent.Application.Dtos.AutoMapper module generates AutoMapper mapping profiles for DTOs that map to/from domain entities. This eliminates the need to manually write repetitive mapping code while maintaining type safety and performance.
Module:
Intent.Application.Dtos.AutoMapperVersion: 4.0.19+Dependencies:Intent.Application.DtosIntent.Application.AutoMapperIntent.Modelers.Services
Key Features
- Automatic Profile Generation: Creates AutoMapper profiles for domain-to-DTO mappings
- Bi-Directional Mapping: Supports mapping in both directions
- Complex Path Mapping: Handles nested property paths and associations
- Mapping Extensions: Generates convenient extension methods
- Expression-Based Projections: Supports EF Core
IQueryableprojections - Advanced Mapping Support: Custom mappings, conditional logic, and transformations
- Nullable Association Handling: Safe traversal of nullable relationships
Installation
Intent.Application.AutoMapper module if not already present.
How It Works
When you map a DTO field to a domain entity attribute in the Services Designer, the module:- Generates an AutoMapper
Profileclass - Creates bidirectional mappings between entity and DTO
- Adds member-specific mapping configurations for complex paths
- Generates mapping extension methods for convenience
Generated Code
Mapping Profile
For a DTO mapped to a domain entity:Mapping Extensions
The module also generates convenient extension methods:Generated Extensions
Usage Examples
Basic Mapping in Services
Service Implementation
EF Core Projections
AutoMapper profiles work seamlessly with EF Core for efficient database queries:Efficient Query Projection
Using
ProjectTo<T>() is more efficient than Select().ToList() followed by Map() because AutoMapper generates the SQL projection directly.Reverse Mapping
DTO to Entity
Advanced Mapping Scenarios
Complex Path Mappings
When mapping across multiple associations:Collection Mappings
AutoMapper automatically handles collection mappings:Collection Mapping
Surrogate Key Mapping
Map association entities to their IDs:Configuration
Profile Location
Control where AutoMapper profiles are generated:Mappings/
Custom Mapping Configuration
For custom mapping logic not supported by the designer, use decorators:Custom Mapping Decorator
Performance Considerations
Use ProjectTo for Database Queries
Use ProjectTo for Database Queries
Always use
ProjectTo<T>() when working with EF Core IQueryable to generate optimized SQL queries:Avoid Over-Mapping
Avoid Over-Mapping
Don’t map entire object graphs if you only need a few properties. Create specialized DTOs for specific use cases.
Cache Mapper Configuration
Cache Mapper Configuration
The
IMapper instance is registered as a singleton by the Intent.Application.AutoMapper module, so configuration is cached automatically.Troubleshooting
Missing Type Maps
Missing Type Maps
Issue:
AutoMapperMappingException: Missing type map configurationSolution:- Ensure the DTO field is mapped in the Services Designer
- Run the Software Factory to regenerate profiles
- Verify the target entity/DTO exists and has the correct template role
Null Reference Exceptions
Null Reference Exceptions
Issue: Null reference exceptions when mapping nullable associations.Solution: While the generated code includes null-forgiving operators, ensure your domain entities maintain referential integrity or handle nulls explicitly in custom configurations.
Ambiguous Type References
Ambiguous Type References
Issue: Compilation errors due to namespace conflicts.Solution: The module automatically adds necessary using directives. If issues persist, check for naming conflicts between your DTOs and entities.
Comparison with Mapperly
| Feature | AutoMapper | Mapperly |
|---|---|---|
| Performance | Runtime reflection | Compile-time source generation |
| Setup | Configuration via profiles | Attribute-based |
| Flexibility | Highly configurable | More limited |
| Debugging | Harder to debug | Easy to inspect generated code |
| EF Projections | Excellent support | Not supported |
| Learning Curve | Moderate | Easier |
Related Modules
- DTOs - Base DTO generation
- Mapperly Integration - Alternative mapping approach
- AutoMapper Core - Core AutoMapper infrastructure
- Domain Interactions - Domain entity queries
