Maitrog.DtoSrcGen
1.0.1
dotnet add package Maitrog.DtoSrcGen --version 1.0.1
NuGet\Install-Package Maitrog.DtoSrcGen -Version 1.0.1
<PackageReference Include="Maitrog.DtoSrcGen" Version="1.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Maitrog.DtoSrcGen" Version="1.0.1" />
<PackageReference Include="Maitrog.DtoSrcGen"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Maitrog.DtoSrcGen --version 1.0.1
#r "nuget: Maitrog.DtoSrcGen, 1.0.1"
#:package Maitrog.DtoSrcGen@1.0.1
#addin nuget:?package=Maitrog.DtoSrcGen&version=1.0.1
#tool nuget:?package=Maitrog.DtoSrcGen&version=1.0.1
DtoSrcGen
This project is inspired by Utility Types from Typescript.
Source generator that builds DTO partial classes from existing models using attributes.
What it does
- Generates constructors and properties for partial classes annotated with attributes from
DtoSrcGen.Attributes. - Supports
Pick,Omit,Readonly,Required,Partial,Record, andUnionpatterns to shape DTOs without hand-written boilerplate. - Supports stacking multiple attributes on a single DTO, including several attributes of the same type (e.g., two
[Pick]attributes). - Supports nested DTO classes (a partial DTO declared inside another class).
- Supports
GenerateDefaultCtoroption on all attributes to control whether an empty DTO constructor is generated. - Emits diagnostics when members are missing/duplicated and when language features (e.g.,
required) are unavailable.
NuGet packages
Maitrog.DtoSrcGen— the source generator. Install from nuget.org withdotnet add package Maitrog.DtoSrcGen.Maitrog.DtoSrcGen.Attributes— the attributes. Install from nuget.org withdotnet add package Maitrog.DtoSrcGen.Attributes.
Both packages are also published to GitHub Packages (see the Packages section on this page). Restoring from GitHub Packages requires authentication — add a nuget.config next to your project with a personal access token (with read:packages scope):
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="github" value="https://nuget.pkg.github.com/Maitrog/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="YOUR_GITHUB_USERNAME" />
<add key="ClearTextPassword" value="YOUR_PAT" />
</github>
</packageSourceCredentials>
</configuration>
Getting started
- Add the packages to your project file:
<ItemGroup>
<PackageReference Include="Maitrog.DtoSrcGen.Attributes" Version="1.0.1" />
<PackageReference Include="Maitrog.DtoSrcGen" Version="1.0.1"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
The generator must be wired as an analyzer (
OutputItemType="Analyzer",ReferenceOutputAssembly="false") — a plain package reference will not generate anything.
When developing inside this repository, use project references instead:
<ItemGroup>
<ProjectReference Include="..\src\DtoSrcGen.Attributes\DtoSrcGen.Attributes.csproj" />
<ProjectReference Include="..\src\DtoSrcGen\DtoSrcGen.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
- Annotate partial DTOs:
using DtoSrcGen;
[Omit(typeof(User), nameof(User.Id), nameof(User.Age))]
public partial class DtoAge { }
[Pick(typeof(User), nameof(User.Id), nameof(User.Name))]
public partial class DtoName { }
// Rename a member with `as`
[Pick(typeof(User), $"{nameof(User.Id)} as UserId")]
public partial class UserIdDto { }
[Readonly(typeof(User))]
public partial class ReadonlyUser { }
[Required(typeof(User))] // requires C# 11+
public partial class RequiredUser { }
[Union(typeof(User), typeof(Chat))]
public partial class UserChat { }
// All members as nullable get/set properties
[Partial(typeof(User))]
public partial class PartialUser { }
// One property per enum member, typed from the second argument
[Record(typeof(Gender), typeof(User))]
public partial class RecordGender { }
// Optional: disable generated empty ctor
[Pick(typeof(User), nameof(User.Id), nameof(User.Name), GenerateDefaultCtor = false)]
public partial class UserSummaryNoDefaultCtor { }
// using multiple attributes of the same type (GenerateDefaultCtor = false is required for all except one)
[Pick(typeof(User), nameof(User.Id), nameof(User.Name), GenerateDefaultCtor = false)]
[Pick(typeof(Chat), nameof(Chat.Title))]
public partial class UserWithChatTitle { }
// mixing different attribute types works too
[Readonly(typeof(User))]
[Omit(typeof(Chat), nameof(Chat.Id), GenerateDefaultCtor = false)]
public partial class ReadonlyUserWithChat { }
// nested DTO classes are supported
[Omit(typeof(User))]
public partial class UserDto
{
[Omit(typeof(User))]
public partial class NestedUserDto { }
}
- Use generated DTOs:
var summary = new UserSummaryNoDefaultCtor(user);
// var summary2 = new UserSummaryNoDefaultCtor(); // no parameterless ctor when GenerateDefaultCtor = false
Attribute behavior
- Pick: include only the listed fields/properties from the source type; generated ctor copies those members. A member can be renamed with
"Member as NewName"syntax (e.g.,Pick(typeof(User), "Id as UserId")). - Omit: include all eligible members except the listed ones.
- Readonly: include all eligible members with getters only.
- Required: includes public members and marks them
required; internal/protected-internal members are included only when the source type itself is internal to the assembly (effective accessibility is checked, so nested types are handled correctly) — otherwise they are skipped with a warning. Emits an error if language version < C# 11. - Union: merges members from multiple types; warns on duplicate names with same type, errors when types differ.
- Partial: includes all eligible members as nullable (
T?) get/set properties. - Record: given an enum and a type, generates one property per enum member name, each of the given type (e.g.,
[Record(typeof(Gender), typeof(User))]producespublic User Male { get; set; }for eachGendermember). No copy constructor is generated;GenerateDefaultCtorstill controls the empty ctor. Emits an error if the first argument is not an enum type.
Common option (GenerateDefaultCtor) available on every attribute. Default is true. Set GenerateDefaultCtor = false to skip generating public DtoName() { }.
Eligible members are public/internal/protected-internal fields or properties that are non-static and not compiler-generated.
Sample project
samples/DtoSrcGenSampledemonstrates all attributes. Rundotnet build samples/DtoSrcGenSampleto see generated output and basic usage.
Development notes
- Keep your consumer project language version at least 9; C# 11 is needed for
RequiredAttribute. - Warnings:
DSG2000(duplicate union member),DSG2001(internal members ignored byRequiredwhen the source type is visible outside the assembly). - Errors:
DSG3000(missing member),DSG3001(type mismatch in union),DSG3002(Requiredunsupported language version),DSG3003(Recordfirst argument is not an enum type).
Contributing
- Open issues or PRs with reproducible scenarios. Add samples that cover new behaviors.
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.1 | 125 | 9/26/2026 |