BinaryCoffee.GenericDto
1.0.1
dotnet add package BinaryCoffee.GenericDto --version 1.0.1
NuGet\Install-Package BinaryCoffee.GenericDto -Version 1.0.1
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="BinaryCoffee.GenericDto" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BinaryCoffee.GenericDto" Version="1.0.1" />
<PackageReference Include="BinaryCoffee.GenericDto" />
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 BinaryCoffee.GenericDto --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: BinaryCoffee.GenericDto, 1.0.1"
#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 BinaryCoffee.GenericDto@1.0.1
#: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=BinaryCoffee.GenericDto&version=1.0.1
#tool nuget:?package=BinaryCoffee.GenericDto&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
GenericDtoSourceGenerator
A powerful C# source generator that automatically generates Data Transfer Objects (DTOs) from your domain models at compile time.
Features
- 🚀 Zero Runtime Overhead: DTOs are generated at compile time using Roslyn source generators
- 🔄 Automatic Mapping: Generates
ToDto(),ToEntity(), andUpdateFrom()extension methods - 📦 Collection Support: Built-in support for mapping collections with
ToDtoList()andToEntityList() - 🎯 Flexible Configuration: Customize DTO names, namespaces, and property behavior
- ✅ Validation Ready: Optional
IValidatableObjectimplementation - 🔒 Type Safe: Full nullable reference type support
- 📝 Records Support: Generate immutable record DTOs
Installation
NuGet Package
dotnet add package BinaryCoffee.GenericDto --version 1.0.1
Quick Start
1. Mark your entity with [GenericDto]
using GenericDto.Core.Attributes;
[GenericDto]
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
[DtoIgnore]
public string InternalCode { get; set; } // Won't appear in DTO
}
2. Use the generated DTO and mappings
// A CustomerDto class is automatically generated in YourNamespace.Dto
var customer = new Customer { Id = 1, Name = "John", Email = "john@example.com" };
// Convert to DTO
var dto = customer.ToDto();
// Convert back to entity
var entity = dto.ToEntity();
// Update existing entity
existingCustomer.UpdateFrom(dto);
// Map collections
var dtos = customers.ToDtoList();
var entities = dtos.ToEntityList();
Configuration Options
[GenericDto] Attribute
| Property | Type | Default | Description |
|---|---|---|---|
DtoName |
string? |
{ClassName}Dto |
Name of the generated DTO |
Namespace |
string? |
{Namespace}.Dto |
Namespace for the generated DTO |
UseRecord |
bool |
false |
Generate a record instead of a class |
AccessModifier |
string |
"public" |
Access modifier for the DTO |
GenerateParameterlessConstructor |
bool |
true |
Generate parameterless constructor |
ImplementIEquatable |
bool |
false |
Implement IEquatable<T> |
ImplementIValidatableObject |
bool |
false |
Implement IValidatableObject |
AdditionalUsings |
string[] |
[] |
Additional using statements |
IncludeInheritedProperties |
bool |
true |
Include properties from base classes |
GenerateMappers |
bool |
true |
Generate mapping extension methods |
[DtoProperty] Attribute
| Property | Type | Default | Description |
|---|---|---|---|
Name |
string? |
Property name | Custom name for the DTO property |
Ignore |
bool |
false |
Exclude property from DTO |
Type |
Type? |
Original type | Custom type for the property |
DefaultValue |
string? |
None | Default value expression |
ForceNullable |
bool? |
Inherited | Force nullable/non-nullable |
Order |
int |
0 |
Property order in DTO |
Description |
string? |
Auto | Custom XML documentation |
MaxLength |
int |
-1 |
Maximum length validation |
MinLength |
int |
-1 |
Minimum length validation |
Pattern |
string? |
None | Regex pattern validation |
MinValue |
double |
double.MinValue |
Minimum value for range |
MaxValue |
double |
double.MaxValue |
Maximum value for range |
[DtoIgnore] Attribute
Shorthand for [DtoProperty(Ignore = true)] - excludes the property from the generated DTO.
[DtoValidation] Attribute
| Property | Type | Description |
|---|---|---|
Required |
bool |
Adds [Required] validation |
EmailAddress |
bool |
Adds [EmailAddress] validation |
Phone |
bool |
Adds [Phone] validation |
Url |
bool |
Adds [Url] validation |
CreditCard |
bool |
Adds [CreditCard] validation |
ErrorMessage |
string? |
Custom error message |
CompareProperty |
string? |
Property to compare against |
Advanced Examples
Using Records
[GenericDto(UseRecord = true)]
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Generates: public record ProductDto { ... }
Custom Namespace and Name
[GenericDto(
DtoName = "OrderResponse",
Namespace = "MyApi.Responses")]
public class Order
{
public Guid OrderId { get; set; }
public decimal TotalAmount { get; set; }
}
// Generates: MyApi.Responses.OrderResponse
With Validation
[GenericDto(ImplementIValidatableObject = true)]
public class User
{
public string Username { get; set; }
[DtoProperty(MaxLength = 100, MinLength = 5)]
public string Password { get; set; }
[DtoValidation(EmailAddress = true, Required = true)]
public string Email { get; set; }
}
Property Customization
[GenericDto]
public class Employee
{
public int Id { get; set; }
[DtoProperty(Name = "FullName")]
public string Name { get; set; }
[DtoProperty(DefaultValue = "DateTime.UtcNow")]
public DateTime HireDate { get; set; }
[DtoIgnore]
public decimal Salary { get; set; } // Sensitive data excluded
}
Generated Code
For a class like:
[GenericDto]
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
The generator produces:
// CustomerDto.g.cs
namespace YourNamespace.Dto
{
public partial class CustomerDto
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public CustomerDto() { }
public CustomerDto(int id, string name)
{
Id = id;
Name = name;
}
}
}
// CustomerDtoMapper.g.cs
namespace YourNamespace.Dto
{
public static class CustomerDtoMapperExtensions
{
public static CustomerDto ToDto(this Customer source) { ... }
public static Customer ToEntity(this CustomerDto dto) { ... }
public static List<CustomerDto> ToDtoList(this IEnumerable<Customer> source) { ... }
public static Customer UpdateFrom(this Customer entity, CustomerDto dto) { ... }
}
}
Requirements
- .NET 10.0+ (for the example project)
- .NET Standard 2.0 compatible (for the analyzers and core attributes)
- Visual Studio 2022 or later / Rider / VS Code with C# extension
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Acknowledgments
- Built with Roslyn Source Generators
- Inspired by modern DTO mapping patterns
| 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. |
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.
Updated documentation and README.