Skip to main content

Overview

The Intent.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.Services

Key Features

  • Flexible Type Declaration: Choose between class or record types
  • Configurable Property Setters: Control accessibility with init, public, private, protected, or internal
  • 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

intent install Intent.Application.Dtos
Or install via the Intent Architect Module Manager.

Configuration

Module Settings

Access DTO settings in your application settings:
Application Settings > DTO Settings

Type Declaration

Options: class (default) | record Choose whether DTOs are generated as classes or records.
public class ProductDto
{
    public ProductDto()
    {
        Name = null!;
    }

    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Property Setter Accessibility

Options: public (default) | init | private | protected | internal Controls the accessibility level of property setters.
public class ProductDto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}
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
public sealed class ProductDto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

Static Factory Method

Default: true Generates a static Create method for DTO instantiation.
ProductDto.cs
public class ProductDto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    public static ProductDto Create(Guid id, string name, decimal price)
    {
        return new ProductDto
        {
            Id = id,
            Name = name,
            Price = price,
        };
    }
}

Advanced Features

DTO Inheritance

DTOs support inheritance hierarchies:
public class BaseDto
{
    public Guid Id { get; set; }
    public DateTime CreatedDate { get; set; }
}

Generic DTOs

Full support for generic type parameters:
GenericDto.cs
public class ResultDto<T>
{
    public bool Success { get; set; }
    public T? Data { get; set; }
    public string? ErrorMessage { get; set; }

    public static ResultDto<T> Create(bool success, T? data, string? errorMessage = null)
    {
        return new ResultDto<T>
        {
            Success = success,
            Data = data,
            ErrorMessage = errorMessage,
        };
    }
}
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 CaseproductName
  • Pascal CaseProductName
  • Snake Caseproduct_name
  • Kebab Caseproduct-name
  • Custom → Specify exact name
public class ProductDto
{
    [JsonPropertyName("productId")]
    public Guid ProductId { get; set; }
    
    [JsonPropertyName("productName")]
    public string ProductName { get; set; }
}

OpenAPI Example Values

Add example values for OpenAPI documentation using the OpenAPI Settings stereotype:
ProductDto.cs
public class ProductDto
{
    /// <summary>
    /// The unique identifier of the product
    /// </summary>
    /// <example>3fa85f64-5717-4562-b3fc-2c963f66afa6</example>
    public Guid Id { get; set; }
    
    /// <summary>
    /// The product name
    /// </summary>
    /// <example>Premium Widget</example>
    public string Name { get; set; }
}

Default Values

Specify default values for DTO properties in the designer:
ProductDto.cs
public class ProductDto
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int Quantity { get; set; } = 1;
    public bool IsActive { get; set; } = true;
}

Template Roles

The module provides two main template roles:
Template IDRoleDescription
Intent.Application.Dtos.DtoModelApplication.Contract.DtoGenerates DTO classes/records
Intent.Application.Dtos.ContractEnumModelApplication.Contract.EnumGenerates 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

When your DTOs represent immutable data structures, use the record type setting for cleaner syntax and value-based equality.
Consider using the Sealed setting for DTOs that shouldn’t be extended, especially in public APIs.
Use init setters for DTOs that should be immutable after construction while still supporting object initializers.
Use XML documentation and OpenAPI example values for DTOs exposed in public APIs.

Troubleshooting

Issue: Duplicate constructor errors.Solution: Check your property setter accessibility setting. When using non-public setters, the module generates constructors automatically.
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.
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.

Release Notes

For detailed version history and release notes, see the module changelog.

Build docs developers (and LLMs) love