home / skills / duc01226 / easyplatform / generate-dto

generate-dto skill

/.claude/skills/generate-dto

This skill generates a DTO for a given entity in C#, mapping core properties and enabling optional load methods.

npx playbooks add skill duc01226/easyplatform --skill generate-dto

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.0 KB
---
name: generate-dto
description: "[Implementation] Generate entity DTO from an existing entity"
infer: true
---

Generate DTO for entity: $ARGUMENTS

## Instructions

1. **Parse arguments**:
   - Entity name (required): e.g., `Employee`, `TextSnippetText`
   - Options: `--with-mapping` (include MapToEntity), `--minimal` (core props only)

2. **Find the entity**:
   - Search in `*.Domain/Entities/` folders
   - Read the entity class to understand its properties

3. **Generate DTO following platform patterns**:

   Location: `*.Application/EntityDtos/<EntityName>Dto.cs`

   Template:

   ```csharp
   public class {Entity}Dto : PlatformEntityDto<{Entity}, string>
   {
       // Empty constructor required
       public {Entity}Dto() { }

       // Constructor maps from entity
       public {Entity}Dto({Entity} entity) : base(entity)
       {
           // Map core properties
       }

       // CORE PROPERTIES
       public string? Id { get; set; }
       // ... other properties from entity

       // OPTIONAL LOAD PROPERTIES (for With* methods)
       public RelatedDto? Related { get; set; }

       // WITH* FLUENT METHODS
       public {Entity}Dto WithRelated(RelatedEntity related)
       {
           Related = new RelatedDto(related);
           return this;
       }

       // PLATFORM OVERRIDES
       protected override object? GetSubmittedId() => Id;
       protected override string GenerateNewId() => Ulid.NewUlid().ToString();
       protected override {Entity} MapToEntity({Entity} entity, MapToEntityModes mode)
       {
           // Map DTO properties back to entity
           return entity;
       }
   }
   ```

4. **Read the template file** for complete pattern:
   - `.github/prompts/create-entity-dto.prompt.md`

5. **After generation**:
   - Show the generated DTO
   - Ask if any properties should be excluded or modified
   - Offer to create the file

## IMPORTANT Task Planning Notes

- Always plan and break many small todo tasks
- Always add a final review todo task to review the works done at the end to find any fix or enhancement needed

Overview

This skill generates a C# Data Transfer Object (DTO) for an existing domain entity. It reads the entity class, follows platform DTO patterns, and produces a ready-to-use DTO file with constructors, core properties, optional load properties, fluent With* methods, and platform overrides. It can optionally include a MapToEntity implementation or emit a minimal DTO with core properties only.

How this skill works

The skill parses command-line arguments to identify the entity name and options (--with-mapping, --minimal). It searches project folders for the entity under *.Domain/Entities/, reads the class to extract properties and navigation relations, then applies the platform DTO template to generate a file at *.Application/EntityDtos/<EntityName>Dto.cs. After generation it presents the DTO content, prompts for exclusions or modifications, and offers to create the file.

When to use it

  • When you need a standardized DTO for an existing domain entity to use in application or API layers.
  • When implementing projection or transfer logic and you want platform overrides (id handling, id generation).
  • When you want consistent With* methods for loading related entities into DTOs.
  • When migrating entities to a DTO pattern or adding mapping support back to entities.
  • When you need a minimal DTO for lightweight payloads using --minimal.

Best practices

  • Run the skill from the repository root so it can locate *.Domain/Entities and *.Application folders.
  • Review extracted properties and relationships before file creation to exclude sensitive or computed fields.
  • Prefer --with-mapping if you need two-way conversion; use --minimal for API surface reduction.
  • Add unit tests for MapToEntity behavior when mapping back to domain objects.
  • Perform the final review todo task to verify naming, nullability, and any platform-specific interfaces.

Example use cases

  • Generate EmployeeDto for an Employee entity, including WithDepartment to load department data.
  • Create a minimal ProductDto for public API responses to reduce payload size.
  • Add MapToEntity to an OrderDto so updates received from clients can be applied to the domain Order.
  • Regenerate DTOs after entity property changes and quickly spot fields to exclude before committing.
  • Generate DTOs for nested aggregates and use With* methods to populate related DTOs during queries.

FAQ

What does --with-mapping do?

It includes a MapToEntity implementation in the DTO so the DTO can map its values back onto an existing entity instance.

What is produced with --minimal?

The minimal option emits only core properties (typically Id and primitive scalar props) and omits optional load properties and fluent With* methods.