Overview
TheIntent.Application.Dtos module generates Data Transfer Objects (DTOs) for your application services, applying the Interface Segregation Principle. DTOs provide a clean contract layer between your application’s external interfaces and internal domain logic.
Module:
Intent.Application.DtosVersion: 4.5.0+Dependencies: Intent.Common.CSharp, Intent.Modelers.ServicesKey Features
- Flexible Type Declaration: Choose between
classorrecordtypes - Configurable Property Setters: Control accessibility with
init,public,private,protected, orinternal - Sealed Types: Option to generate sealed DTOs for immutability
- Static Factory Methods: Automatic creation of factory methods
- DTO Inheritance: Support for hierarchical DTO structures
- Generic Types: Full support for generic DTOs
- Serialization Control: Configure JSON property naming conventions
- Code-to-Designer Sync: Import changes from code back to designers
Installation
Configuration
Module Settings
Access DTO settings in your application settings:Type Declaration
Options:class (default) | record
Choose whether DTOs are generated as classes or records.
Property Setter Accessibility
Options:public (default) | init | private | protected | internal
Controls the accessibility level of property setters.
When using non-public setters, the module automatically generates:
- A parameterized constructor with all fields
- A protected/private parameterless constructor for serialization frameworks
Sealed Modifier
Default:false
Generate DTOs with the sealed modifier to prevent inheritance.
ProductDto.cs
Static Factory Method
Default:true
Generates a static Create method for DTO instantiation.
ProductDto.cs
Advanced Features
DTO Inheritance
DTOs support inheritance hierarchies:Generic DTOs
Full support for generic type parameters:GenericDto.cs
Generic DTOs are saved with filenames like
ResultOfT.cs to avoid file system issues.Serialization Settings
Apply the Serialization Settings stereotype to control JSON property naming: Naming Conventions:Camel Case→productNamePascal Case→ProductNameSnake Case→product_nameKebab Case→product-nameCustom→ Specify exact name
OpenAPI Example Values
Add example values for OpenAPI documentation using the OpenAPI Settings stereotype:ProductDto.cs
Default Values
Specify default values for DTO properties in the designer:ProductDto.cs
Template Roles
The module provides two main template roles:| Template ID | Role | Description |
|---|---|---|
Intent.Application.Dtos.DtoModel | Application.Contract.Dto | Generates DTO classes/records |
Intent.Application.Dtos.ContractEnumModel | Application.Contract.Enum | Generates contract enums |
Integration
DTOs integrate seamlessly with other Intent modules:AutoMapper
Automatic mapping to/from domain entities
Mapperly
Source generator-based mapping (faster)
FluentValidation
Validation rules for DTOs
Pagination
Paginated result types
Best Practices
Use Records for Immutable Data
Use Records for Immutable Data
When your DTOs represent immutable data structures, use the
record type setting for cleaner syntax and value-based equality.Apply Sealed for Security
Apply Sealed for Security
Consider using the
Sealed setting for DTOs that shouldn’t be extended, especially in public APIs.Leverage Init-Only Setters
Leverage Init-Only Setters
Use
init setters for DTOs that should be immutable after construction while still supporting object initializers.Document Public APIs
Document Public APIs
Use XML documentation and OpenAPI example values for DTOs exposed in public APIs.
Troubleshooting
Constructor Conflicts
Constructor Conflicts
Issue: Duplicate constructor errors.Solution: Check your property setter accessibility setting. When using non-public setters, the module generates constructors automatically.
Null Reference Warnings
Null Reference Warnings
Issue: Nullable reference warnings in generated code.Solution: The module automatically adds
null! assignments in constructors for required reference types. Ensure nullable reference types are enabled in your project.Generic Type File Naming
Generic Type File Naming
Issue: Files for generic DTOs have unexpected names.Solution: This is by design.
ResultDto<T> is saved as ResultOfT.cs to avoid filesystem issues with angle brackets.Related Modules
- AutoMapper Integration
- Mapperly Integration
- DTO Pagination
- FluentValidation for DTOs
- Application Contracts
