Mxd.DtoGenerator
1.0.5
dotnet add package Mxd.DtoGenerator --version 1.0.5
NuGet\Install-Package Mxd.DtoGenerator -Version 1.0.5
<PackageReference Include="Mxd.DtoGenerator" Version="1.0.5" />
<PackageVersion Include="Mxd.DtoGenerator" Version="1.0.5" />
<PackageReference Include="Mxd.DtoGenerator" />
paket add Mxd.DtoGenerator --version 1.0.5
#r "nuget: Mxd.DtoGenerator, 1.0.5"
#:package Mxd.DtoGenerator@1.0.5
#addin nuget:?package=Mxd.DtoGenerator&version=1.0.5
#tool nuget:?package=Mxd.DtoGenerator&version=1.0.5
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}Dtofrom your entities. - Built-in Mapper: Generates
FromEntity()andToEntity()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
requiredmodifiers,initproperties, and nullable reference types.
📦 Installation
(If you plan to publish to NuGet, add command here. For now, referencing locally:)
- Add the
DtoGeneratorproject reference to your project. - Add the
DtoGenerator.Sourceproject 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 baseUser)Role(fromAdminUser)
🔹 Note: Only
public instance propertiesfrom 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 | Versions 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. |
-
.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.