DnBuilder 1.0.2

dotnet add package DnBuilder --version 1.0.2
                    
NuGet\Install-Package DnBuilder -Version 1.0.2
                    
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="DnBuilder" Version="1.0.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DnBuilder" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="DnBuilder">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 DnBuilder --version 1.0.2
                    
#r "nuget: DnBuilder, 1.0.2"
                    
#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 DnBuilder@1.0.2
                    
#: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=DnBuilder&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=DnBuilder&version=1.0.2
                    
Install as a Cake Tool

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 Builder class with one With{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 requires partial.
  • 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.

There are no supported framework assets in this 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.2 107 7/30/2026
1.0.1 148 7/30/2026
1.0.0 149 7/30/2026