惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
WordPress大学
WordPress大学
Martin Fowler
Martin Fowler
博客园 - 聂微东
L
LangChain Blog
D
Docker
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
C
Cisco Blogs
L
LINUX DO - 热门话题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News and Events Feed by Topic
Latest news
Latest news
PCI Perspectives
PCI Perspectives
TaoSecurity Blog
TaoSecurity Blog
博客园 - 司徒正美
量子位
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
SecWiki News
SecWiki News
美团技术团队
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
AWS News Blog
AWS News Blog
C
Check Point Blog
S
Security Affairs
Vercel News
Vercel News
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog
IT之家
IT之家
A
About on SuperTechFans
Help Net Security
Help Net Security
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Recorded Future
Recorded Future
月光博客
月光博客
博客园_首页
小众软件
小众软件
H
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
[.NET] Streamline Your DTOs with DKNet.EfCore.DtoGenerator
Steven Hoang · 2026-05-18 · via DEV Community

Writing Data Transfer Objects (DTOs) is one of those repetitive tasks that every developer encounters when building applications with Entity Framework Core. While DTOs are essential for separating your domain models from your API contracts, manually creating and maintaining them can be tedious, error-prone, and time-consuming. What if there was a way to automate this process while maintaining type safety and validation consistency?

DKNet.EfCore.DtoGenerator is a lightweight Roslyn Incremental Source Generator that automatically creates immutable DTO types from your EF Core entities at compile time. It eliminates the need to manually write repetitive DTO classes while preserving validation attributes, ensuring type safety, and reducing boilerplate code significantly.

Table of Contents

  1. The Problem with Manual DTOs
  2. What is DKNet.EfCore.DtoGenerator?
  3. Getting Started
  4. Basic Usage
  5. Validation Attributes Support
  6. Advanced Features
  7. Integration with Mapster
  8. Viewing Generated Code
  9. Best Practices
  10. Conclusion

The Problem with Manual DTOs

Let's consider a typical scenario. You have an EF Core entity with validation attributes:

public class Product
{
    public Guid Id { get; set; }

    [Required]
    [StringLength(100, MinimumLength = 3)]
    public string Name { get; set; } = string.Empty;

    [MaxLength(50)]
    public string Sku { get; set; } = string.Empty;

    [Range(0.01, 999999.99)]
    public decimal Price { get; set; }

    [EmailAddress]
    public string ContactEmail { get; set; } = string.Empty;

    public DateTime CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
}

Enter fullscreen mode Exit fullscreen mode

Traditionally, you would need to manually create a corresponding DTO:

public record ProductDto
{
    public Guid Id { get; init; }

    [Required]
    [StringLength(100, MinimumLength = 3)]
    public required string Name { get; init; }

    [MaxLength(50)]
    public required string Sku { get; init; }

    [Range(0.01, 999999.99)]
    public decimal Price { get; init; }

    [EmailAddress]
    public required string ContactEmail { get; init; }

    public DateTime CreatedAt { get; init; }
    public DateTime? UpdatedAt { get; init; }
}

Enter fullscreen mode Exit fullscreen mode

This approach has several issues:

  • Repetitive Code: You're duplicating property definitions
  • Maintenance Burden: When the entity changes, you must manually update the DTO
  • Validation Inconsistency: Easy to forget copying validation attributes
  • Human Error: Typos and mismatched types are common
  • Time Consuming: Scales poorly with multiple entities

What is DKNet.EfCore.DtoGenerator?

DKNet.EfCore.DtoGenerator is a Roslyn Incremental Source Generator that solves these problems by automatically generating DTOs at compile time. Here's what makes it special:

  • Zero Runtime Overhead: Code generation happens at compile time
  • Type Safety: Generated DTOs are fully typed and checked by the compiler
  • Validation Preservation: Automatically copies all validation attributes from entities
  • Immutable by Default: Uses init properties for immutability
  • Mapster Integration: Provides efficient mapping when Mapster is available
  • Customizable: Support for property inclusion, exclusion, and custom extensions
  • Incremental: Only regenerates when needed, keeping builds fast

Getting Started

Prerequisites

Before using DKNet.EfCore.DtoGenerator, ensure you have:

  • .NET 9.0 SDK or later
  • A project using Entity Framework Core

Installation

Add the NuGet package to your project:

dotnet add package DKNet.EfCore.DtoGenerator

Enter fullscreen mode Exit fullscreen mode

Or add it directly to your .csproj file:

<ItemGroup>
  <PackageReference Include="DKNet.EfCore.DtoGenerator" Version="latest"
                    PrivateAssets="all"
                    OutputItemType="Analyzer" />
</ItemGroup>

Enter fullscreen mode Exit fullscreen mode

Optional but Recommended: Add Mapster for enhanced mapping capabilities:

dotnet add package Mapster

Enter fullscreen mode Exit fullscreen mode

Project Configuration

To enable the source generator and view generated files, add the following properties to your .csproj file:

<PropertyGroup>
  <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
  <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
  <!-- Force analyzer to reload on every build -->
  <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

Enter fullscreen mode Exit fullscreen mode

These settings ensure:

  • Generated files are emitted to the obj/Generated directory
  • The analyzer runs correctly on every build
  • You can inspect the generated code when needed

Basic Usage

Using DKNet.EfCore.DtoGenerator is incredibly simple. Here's a complete example:

Step 1: Define Your Entity

public class MerchantBalance
{
    public Guid Id { get; set; }

    [MaxLength(100)]
    public string MerchantId { get; set; } = string.Empty;

    [Range(0, double.MaxValue)]
    public decimal Balance { get; set; }

    public DateTime LastUpdated { get; set; }
}

Enter fullscreen mode Exit fullscreen mode

Step 2: Declare the DTO

Create an empty partial record with the [GenerateDto] attribute:

using DKNet.EfCore.DtoGenerator;

[GenerateDto(typeof(MerchantBalance))]
public partial record BalanceDto;

Enter fullscreen mode Exit fullscreen mode

That's it! The generator will automatically create BalanceDto.g.cs with:

  • All properties from MerchantBalance with init accessors
  • Validation attributes copied from the entity
  • Helper methods for mapping between entity and DTO

Step 3: Use the Generated DTO

// Convert entity to DTO
var entity = await dbContext.MerchantBalances.FindAsync(id);
var dto = mapper.Map<BalanceDto>(entity);

// Convert DTO back to entity
var newEntity = mapper.Map<MerchantBalance>(dto);

// Convert multiple entities
var dtos = mapper.Map<IEnumerable<BalanceDto>>(dbContext.MerchantBalances);

Enter fullscreen mode Exit fullscreen mode

Validation Attributes Support

One of the most powerful features of DKNet.EfCore.DtoGenerator is its automatic copying of validation attributes. This ensures that validation rules are consistently applied across your application layers without manual duplication.

Supported Attributes

All System.ComponentModel.DataAnnotations attributes are supported, including:

  • [Required] - Marks a property as required
  • [StringLength] - Limits string length with optional minimum
  • [MaxLength] - Sets maximum length for strings or collections
  • [MinLength] - Sets minimum length for strings or collections
  • [Range] - Validates numeric ranges
  • [EmailAddress] - Validates email format
  • [Phone] - Validates phone number format
  • [Url] - Validates URL format
  • [RegularExpression] - Validates against regex pattern
  • [Compare] - Compares two properties
  • [CreditCard] - Validates credit card format
  • And all other custom validation attributes

Example with Validation

Entity with Validation:

public class User
{
    public Guid Id { get; set; }

    [Required(ErrorMessage = "Username is required")]
    [StringLength(50, MinimumLength = 3,
                  ErrorMessage = "Username must be between 3 and 50 characters")]
    public string Username { get; set; } = string.Empty;

    [Required]
    [EmailAddress(ErrorMessage = "Invalid email format")]
    public string Email { get; set; } = string.Empty;

    [Phone]
    public string? PhoneNumber { get; set; }

    [Range(18, 120, ErrorMessage = "Age must be between 18 and 120")]
    public int Age { get; set; }

    [Url]
    public string? Website { get; set; }
}

Enter fullscreen mode Exit fullscreen mode

DTO Declaration:

[GenerateDto(typeof(User))]
public partial record UserDto;

Enter fullscreen mode Exit fullscreen mode

Generated DTO (simplified view):

public partial record UserDto
{
    public Guid Id { get; init; }

    [Required(ErrorMessage = "Username is required")]
    [StringLength(50, MinimumLength = 3,
                  ErrorMessage = "Username must be between 3 and 50 characters")]
    public required string Username { get; init; }

    [Required]
    [EmailAddress(ErrorMessage = "Invalid email format")]
    public required string Email { get; init; }

    [Phone]
    public string? PhoneNumber { get; init; }

    [Range(18, 120, ErrorMessage = "Age must be between 18 and 120")]
    public int Age { get; init; }

    [Url]
    public string? Website { get; init; }
}

Enter fullscreen mode Exit fullscreen mode

Advanced Features

Excluding Properties

You can exclude specific properties from the generated DTO:

[GenerateDto(typeof(MerchantBalance),
             Exclude = new[] { nameof(MerchantBalance.LastUpdated), "Id" })]
public partial record BalanceSummaryDto;

Enter fullscreen mode Exit fullscreen mode

Generated DTO will exclude LastUpdated and Id from Entity's properties.

Global Properties Exclusions

For projects with common audit properties across multiple entities (such as CreatedBy, UpdatedBy, CreatedAt, UpdatedAt), you can configure global exclusions that apply to all generated DTOs. This eliminates the need to specify these exclusions repeatedly for each DTO.

Add the following configuration to your .csproj file:

<!-- Configure global exclusions for DTO generator -->
<ItemGroup>
    <CompilerVisibleProperty Include="DtoGeneratorExclusions"/>
</ItemGroup>
<PropertyGroup>
    <DtoGeneratorExclusions>CreatedBy,UpdatedBy,CreatedAt,UpdatedAt</DtoGeneratorExclusions>
</PropertyGroup>

Enter fullscreen mode Exit fullscreen mode

With this configuration, all properties listed in DtoGeneratorExclusions will be automatically excluded from all generated DTOs throughout your project. This is particularly useful for:

  • Audit Fields: Automatically exclude common audit tracking properties
  • Consistency: Ensure the same properties are excluded across all DTOs
  • Maintainability: Change exclusions in one place instead of updating multiple DTO declarations

You can still use the Exclude or Include parameters on individual DTOs to override or supplement the global exclusions for specific cases.

Including Only Specific Properties

Alternatively, specify only the properties you want:

[GenerateDto(typeof(MerchantBalance),
             Include = new[] { nameof(MerchantBalance.MerchantId), "Balance" })]
public partial record BalanceOnlyDto;

Enter fullscreen mode Exit fullscreen mode

Note: Include and Exclude are mutually exclusive. If both are specified, Include takes precedence, and a warning will be generated.

Custom Properties

You can extend generated DTOs with custom properties or methods:

[GenerateDto(typeof(MerchantBalance))]
public partial record BalanceDto
{
    // Custom computed property
    public string DisplayBalance => $"${Balance:N2}";

    // Custom method
    public bool IsPositive() => Balance > 0;

    // Override generated property. The Balance property will be ignored from Entity.
    public decimal Balance { get; init; }
}

Enter fullscreen mode Exit fullscreen mode

Multiple DTOs from Same Entity

You can generate multiple DTOs from the same entity for different use cases:

// Full DTO with all properties
[GenerateDto(typeof(Product))]
public partial record ProductDto;

// Summary DTO for list views
[GenerateDto(typeof(Product),
             Include = new[] { "Id", "Name", "Price" })]
public partial record ProductSummaryDto;

// Create DTO without Id
[GenerateDto(typeof(Product),
             Exclude = new[] { "Id", "CreatedAt", "UpdatedAt" })]
public partial record CreateProductDto;

Enter fullscreen mode Exit fullscreen mode

Integration with Mapster

When Mapster is present in your project, DKNet.EfCore.DtoGenerator automatically generates code that uses TypeAdapter.Adapt for efficient mapping.

Generated Code with Mapster

public partial record BalanceDto
{
    // Properties...

    public static BalanceDto FromEntity(MerchantBalance entity)
        => Mapster.TypeAdapter.Adapt<BalanceDto>(entity);

    public MerchantBalance ToEntity()
        => Mapster.TypeAdapter.Adapt<MerchantBalance>(this);

    public static IEnumerable<BalanceDto> FromEntities(
        IEnumerable<MerchantBalance> entities)
        => Mapster.TypeAdapter.Adapt<IEnumerable<BalanceDto>>(entities);
}

Enter fullscreen mode Exit fullscreen mode

Mapster Configuration

You can customize mapping behavior with Mapster configurations:

// Global configuration during startup
TypeAdapterConfig.GlobalSettings.Scan(Assembly.GetExecutingAssembly());

// Type-specific configuration
TypeAdapterConfig<MerchantBalance, BalanceDto>
    .NewConfig()
    .Map(dest => dest.DisplayBalance, src => $"${src.Balance:N2}")
    .Ignore(dest => dest.SomeCustomProperty);

Enter fullscreen mode Exit fullscreen mode

EF Core Query Projections

For optimal performance with database queries, use Mapster's projection:

using Mapster;

var balances = await dbContext.MerchantBalances
    .Where(b => b.Balance > 0)
    .ProjectToType<BalanceDto>()
    .ToListAsync();

Enter fullscreen mode Exit fullscreen mode

This translates the projection to SQL, avoiding loading unnecessary entity data.

Viewing Generated Code

Generated files are located in the obj/Generated directory by default. To make them more accessible, you can add an MSBuild target to copy them to your project:

<Target Name="CopyGeneratedDtos" AfterTargets="CoreCompile">
    <ItemGroup>
        <GeneratedDtoFiles Include="$(CompilerGeneratedFilesOutputPath)\**\*Dto.g.cs"/>
    </ItemGroup>
    <MakeDir Directories="$(ProjectDir)GeneratedDtos"
             Condition="'@(GeneratedDtoFiles)' != ''"/>
    <Copy SourceFiles="@(GeneratedDtoFiles)"
          DestinationFiles="$(ProjectDir)GeneratedDtos\%(Filename)%(Extension)"
          SkipUnchangedFiles="false"
          OverwriteReadOnlyFiles="true"
          Condition="'@(GeneratedDtoFiles)' != ''"/>
</Target>

<!-- Make generated DTOs visible but excluded from compilation -->
<ItemGroup>
    <Compile Remove="GeneratedDtos\**\*.cs"/>
    <None Include="GeneratedDtos\**\*.cs"/>
</ItemGroup>

Enter fullscreen mode Exit fullscreen mode

This target:

  • Copies generated files to a GeneratedDtos folder
  • Keeps them visible in Solution Explorer for inspection
  • Excludes them from compilation to avoid duplicates

Best Practices

1. Use Records for DTOs

Records are ideal for DTOs because they provide:

  • Value-based equality
  • Immutability with init properties
  • Concise syntax with positional parameters
  • Built-in ToString() implementation
[GenerateDto(typeof(Product))]
public partial record ProductDto;  // ✅ Recommended

Enter fullscreen mode Exit fullscreen mode

2. Keep DTOs Simple

DTOs should be simple data containers. Avoid adding business logic:

[GenerateDto(typeof(Order))]
public partial record OrderDto
{
    // ✅ Good: Computed display property
    public string DisplayTotal => $"${Total:N2}";

    // ❌ Avoid: Business logic
    public void ProcessPayment() { /* ... */ }
}

Enter fullscreen mode Exit fullscreen mode

3. Use Meaningful DTO Names

Choose descriptive names that indicate the DTO's purpose:

// ✅ Good
[GenerateDto(typeof(Product))]
public partial record ProductDto;

[GenerateDto(typeof(Product), Include = new[] { "Id", "Name" })]
public partial record ProductSummaryDto;

[GenerateDto(typeof(Product), Exclude = new[] { "Id" })]
public partial record CreateProductDto;

// ❌ Avoid
[GenerateDto(typeof(Product))]
public partial record ProductDto1;

Enter fullscreen mode Exit fullscreen mode

4. Leverage Include/Exclude for Different Scenarios

Create specialized DTOs for different use cases:

// List view - minimal data
[GenerateDto(typeof(User),
             Include = new[] { "Id", "Username", "Email" })]
public partial record UserListDto;

// Detail view - full data
[GenerateDto(typeof(User))]
public partial record UserDetailDto;

// Create/Update - no Id or audit fields
[GenerateDto(typeof(User),
             Exclude = new[] { "Id", "CreatedAt", "UpdatedAt" })]
public partial record UserInputDto;

Enter fullscreen mode Exit fullscreen mode

5. Combine with FluentValidation

While validation attributes are automatically copied, you can layer additional validation:

public class CreateProductDtoValidator : AbstractValidator<CreateProductDto>
{
    public CreateProductDtoValidator()
    {
        // Additional business rules
        RuleFor(x => x.Price)
            .GreaterThan(0)
            .When(x => x.IsActive);

        RuleFor(x => x.Sku)
            .MustAsync(async (sku, ct) => await IsUniqueSkuAsync(sku, ct))
            .WithMessage("SKU must be unique");
    }
}

Enter fullscreen mode Exit fullscreen mode

6. Version Your DTOs

When making breaking changes, version your DTOs:

// V1
[GenerateDto(typeof(Product))]
public partial record ProductDtoV1;

// V2 with additional fields
[GenerateDto(typeof(Product))]
public partial record ProductDtoV2;

Enter fullscreen mode Exit fullscreen mode

7. Use Mapster for Complex Mappings

For complex scenarios, leverage Mapster's configuration:

TypeAdapterConfig<Product, ProductDto>
    .NewConfig()
    .Map(dest => dest.CategoryName, src => src.Category.Name)
    .Map(dest => dest.DiscountedPrice,
         src => src.Price * (1 - src.DiscountPercentage / 100));

Enter fullscreen mode Exit fullscreen mode

Conclusion

DKNet.EfCore.DtoGenerator revolutionizes how we work with DTOs in .NET applications by:

  • Eliminating Boilerplate: No more manual DTO creation and maintenance
  • Ensuring Consistency: Validation attributes are automatically synchronized
  • Improving Type Safety: Compile-time generation ensures correctness
  • Boosting Productivity: Focus on business logic instead of plumbing code
  • Maintaining Performance: Zero runtime overhead with compile-time generation

The source generator seamlessly integrates into your development workflow, automatically updating DTOs as your entities evolve. Combined with Mapster, it provides a complete solution for entity-DTO mapping that's both powerful and easy to use.

Whether you're building a small API or a large enterprise application, DKNet.EfCore.DtoGenerator can significantly reduce development time while improving code quality and maintainability.

References

Thank You

Thank you for taking the time to read this guide! I hope it has been helpful, feel free to explore further, and happy coding! 🌟✨

Steven | GitHub