Mxd.DtoGenerator 1.0.5

dotnet add package Mxd.DtoGenerator --version 1.0.5
                    
NuGet\Install-Package Mxd.DtoGenerator -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mxd.DtoGenerator" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mxd.DtoGenerator" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Mxd.DtoGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mxd.DtoGenerator --version 1.0.5
                    
#r "nuget: Mxd.DtoGenerator, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mxd.DtoGenerator@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mxd.DtoGenerator&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Mxd.DtoGenerator&version=1.0.5
                    
Install as a Cake Tool

中文文档 NuGet NuGet Downloads

Roslyn DTO Generator

A high-performance, compile-time Source Generator for .NET that automatically creates DTO classes and mapping methods based on your entity definitions.

🚀 Zero Reflection | ⚡ Compile-Time Safety | 🛠 Highly Customizable

✨ Features

  • Auto DTO Generation: Automatically generates public partial class {Name}Dto from your entities.
  • Built-in Mapper: Generates FromEntity() and ToEntity() methods automatically.
  • Bidirectional Mapping: Supports Entity ↔ DTO conversion.
  • Zero Overhead: No runtime reflection (System.Reflection); code is generated at compile time.
  • Flexible Configuration:
    • [DtoName]: Rename properties in the DTO.
    • [DtoIgnore]: Exclude sensitive properties (e.g., PasswordHash).
    • [DtoVirtualProperty]: Add calculated/aggregated properties (e.g., FirstName + LastName → FullName).
  • Custom Logic Hooks: Support for partial methods or enforced interfaces (IDtoMapperHooks) for custom mapping logic.
  • Modern C# Support: Supports required modifiers, init properties, and nullable reference types.

📦 Installation

(If you plan to publish to NuGet, add command here. For now, referencing locally:)

  1. Add the DtoGenerator project reference to your project.
  2. Add the DtoGenerator.Source project as an Analyzer.

🚀 Quick Start

1. Define your Entity

Simply add [GenerateDto] to your class.

using DtoGenerator;

namespace MyApp.Models;

[GenerateDto]
public class User
{
    public int Id { get; set; }
    public required string Username { get; set; }
    
    [DtoIgnore] // Won't appear in DTO
    public string PasswordHash { get; set; } 
}

2. Use the Generated DTO

The generator creates UserDto in the background immediately.

var user = new User { Id = 1, Username = "admin", PasswordHash = "###" };

// Entity -> DTO
var dto = UserDto.FromEntity(user);
Console.WriteLine(dto.Username); // "admin"

// DTO -> Entity
var newUser = dto.ToEntity();

📚 Advanced Usage

1. Inheriting Base Class Properties

By default, only properties declared in the current class are included.

To include all public properties from the entire inheritance chain, use:

[GenerateDto(IncludeBaseProperties = true)]
public class AdminUser : User
{
    public string Role { get; set; }
}

This will generate a AdminUserDto containing:

  • Id, Username (from base User)
  • Role (from AdminUser)

🔹 Note: Only public instance properties from base classes are included. Private, protected, or static members are ignored.

2. Renaming Properties

Map UserEntity.UserEmail to UserDto.Email.

[DtoName("Email")]
public string UserEmail { get; set; }

3. Virtual / Calculated Properties

Combine fields into a new property in the DTO. Use entity to refer to the source object.

[DtoVirtualProperty("FullName", typeof(string), "entity.FirstName + \" \" + entity.LastName")]
public class User { ... }

// Or it can be written like this:
[DtoVirtualProperty("FullName", typeof(string), ExpressionMemberName=nameof(FullNameExpression))]
public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Expression<Func<User, string>> FullNameExpression => a => $"{a.FirstName} {a.LastName}";
}

4. Custom Logic & Hooks

You can hook into the mapping process to handle complex scenarios (e.g., splitting a string back into two fields during ToEntity).

Option A: Optional Partial Methods (Default)

Simply create a partial class file for your DTO.

// UserDto.Custom.cs
public partial class UserDto
{
    partial void OnEntityCreated(User targetEntity)
    {
        // Custom reverse mapping logic
        Console.WriteLine("Mapping finished!");
    }
}
Option B: Enforced Interface (Strict Mode)

Force the implementation of hooks using EnforceHooks = true.

[GenerateDto(EnforceHooks = true)]
public class User { ... }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 304 12/17/2025
1.0.4 126 12/13/2025
1.0.3 434 12/11/2025
1.0.1 507 12/1/2025
1.0.0 276 11/30/2025