DnBuilder 1.0.2
dotnet add package DnBuilder --version 1.0.2
NuGet\Install-Package DnBuilder -Version 1.0.2
<PackageReference Include="DnBuilder" Version="1.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="DnBuilder" Version="1.0.2" />
<PackageReference Include="DnBuilder"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add DnBuilder --version 1.0.2
#r "nuget: DnBuilder, 1.0.2"
#:package DnBuilder@1.0.2
#addin nuget:?package=DnBuilder&version=1.0.2
#tool nuget:?package=DnBuilder&version=1.0.2
DnBuilder
Source-generated fluent builders for C# classes — just add [Builder].
No reflection, no runtime cost. DnBuilder uses a Roslyn Source Generator to write the builder code for you at compile time.
Install
dotnet add package DnBuilder
Usage
Mark any class as partial and add [Builder]:
using DnBuilder;
[Builder]
public partial class ApiResponse<T>
{
public bool Success { get; set; }
public string? Message { get; set; }
public T? Result { get; set; }
public int StatusCode { get; set; }
}
That's it. A nested Builder class with With{Property}(...) methods is generated automatically:
var response = ApiResponse<string>.CreateBuilder()
.WithSuccess(true)
.WithMessage("OK")
.WithResult("hello")
.WithStatusCode(200)
.Build();
How it works
At compile time, DnBuilder scans for classes marked with [Builder] and generates:
- A static
CreateBuilder()factory method - A nested
Builderclass with oneWith{PropertyName}(...)method per public settable property - A
Build()method that returns the fully constructed object
The generated code lives in obj/generated/ and is visible in your IDE if you want to inspect it.
Requirements
- The target class must be
partial— the generator adds a nested class to it, which requirespartial. - Only public properties with a setter are included in the builder.
- Works with generic classes (e.g.
ApiResponse<T>).
Why partial?
Roslyn source generators add code to your class rather than replacing it. Without partial, the compiler has no way to merge your hand-written class with the generated Builder nested type.
Example
[Builder]
public partial class User
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public string? Email { get; set; }
}
var user = User.CreateBuilder()
.WithName("Sary")
.WithAge(25)
.WithEmail("sary@example.com")
.Build();
FAQ
Can I use .Builder() instead of .CreateBuilder()?
Not currently — Builder is the name of the generated nested class, so a method can't share that name in C#. CreateBuilder() is the entry point.
Does this work with records? Not yet — the generator currently targets classes with settable properties. Record support may come in a future version.
Does this add anything to my published assembly?
No. DnBuilder is a development-time-only dependency (DevelopmentDependency=true); it does not ship a runtime DLL with your app.
License
MIT
Contributing
Issues and PRs welcome at github.com/SaryChanPichhh/DnBuilder.
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.