StepwiseBuilderGenerator 2.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package StepwiseBuilderGenerator --version 2.2.0
                    
NuGet\Install-Package StepwiseBuilderGenerator -Version 2.2.0
                    
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="StepwiseBuilderGenerator" Version="2.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="StepwiseBuilderGenerator" Version="2.2.0" />
                    
Directory.Packages.props
<PackageReference Include="StepwiseBuilderGenerator" />
                    
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 StepwiseBuilderGenerator --version 2.2.0
                    
#r "nuget: StepwiseBuilderGenerator, 2.2.0"
                    
#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 StepwiseBuilderGenerator@2.2.0
                    
#: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=StepwiseBuilderGenerator&version=2.2.0
                    
Install as a Cake Addin
#tool nuget:?package=StepwiseBuilderGenerator&version=2.2.0
                    
Install as a Cake Tool

Stepwise Builder Generator 🚀

Lightweight, compile-time-safe C# source generator for fluent stepwise builders.
Forces correct data collection without runtime cost or reflection.

NuGet


✨ Overview

Building complex objects safely often requires enforcing a strict order of steps.
Stepwise Builder Generator produces fluent builder APIs automatically from simple annotations.

It generates strongly-typed interfaces, fields, default values, and overloads,
ensuring you can't forget required data or call steps out of order.

You retain full control over how the final object is built — the Build method always delegates to your provided logic. You define exactly how the collected data is transformed, allowing uses beyond object construction,
such as building complex data pipelines, aggregators, or workflows.

No runtime reflection. No manual boilerplate.


🚀 Quick Start

Define your target type:

public class Order
{
    public int Id { get; init; }
    public string Customer { get; init; }
    public decimal Total { get; init; }
}

Create a builder:

[StepwiseBuilder]
public partial class OrderBuilder
{
    public OrderBuilder()
    {
        GenerateStepwiseBuilder
            .AddStep<int>("SetId", "Id")
            .AddStep<string>("SetCustomer")
            .AddStep<decimal>("SetTotal")
            .CreateBuilderFor<Order>(b => new Order
            {
                Id = b.Id,
                Customer = b.SetCustomerValue,
                Total = b.SetTotalValue
            });
    }
}

Use it:

var order = StepwiseBuilders.OrderBuilder()
    .SetId(1001)
    .SetCustomer("Acme Co.")
    .SetTotal(250.75m)
    .Build();

🛠️ Key Features

  • Stepwise typing: Force caller to supply each field in order
  • Default values: Define steps with default factories
  • Overloads: Accept alternative types with mapping
  • Branching: Extend another builder at a specific step
  • Static factories: Entry points like StepwiseBuilders.OrderBuilder()
  • Enum of steps: Generated Steps enum for diagnostics or logging
  • User-defined Build: You fully control how data is assembled
  • Zero runtime cost: Pure compile-time source generation

📚 Examples

1️⃣ Default-Value Step

public class ReportConfig
{
    public string Title { get; init; }
    public bool IncludeCharts { get; init; }
}
[StepwiseBuilder]
public partial class ReportConfigBuilder
{
    public ReportConfigBuilder()
    {
        GenerateStepwiseBuilder
            .AddStep<string>("WithTitle")
            .AddStep<bool>("IncludeCharts", () => true)
            .CreateBuilderFor<ReportConfig>(b => new ReportConfig
            {
                Title = b.WithTitleValue,
                IncludeCharts = b.IncludeChartsValue
            });
    }
}

Usage:

var report = StepwiseBuilders.ReportConfigBuilder()
    .WithTitle("Annual Report")
    .IncludeCharts() // uses default true
    .Build();        // uses provided Build factory

2️⃣ Overload Mapping

[StepwiseBuilder]
public partial class SimpleBuilder
{
    public SimpleBuilder()
    {
        GenerateStepwiseBuilder
            .AddStep<int>("SetValue")
            .AndOverload<string, int>(valueAsString => int.Parse(valueAsString))
            // or int.Parse could be used directly as delegate.
            .CreateBuilderFor<int>();
    }
}

Usage:

var value = StepwiseBuilders.SimpleBuilder()
    .SetValue("123")  // maps string to int
    .Build(b => b.SetValueValue);

3️⃣ Branching Between Builders

Base builder:

[StepwiseBuilder]
public partial class UserBuilder
{
    public UserBuilder()
    {
        GenerateStepwiseBuilder
            .AddStep<string>("SetName")
            .AddStep<int>("SetAge")
            .CreateBuilderFor<User>();
    }
}

Branching builder:

[StepwiseBuilder]
public partial class VipUserBuilder
{
    public VipUserBuilder()
    {
        GenerateStepwiseBuilder
            .BranchFrom<UserBuilder>("SetAge")
            .AddStep<string>("SetMembershipLevel")
            .CreateBuilderFor<VipUser>();
    }
}

Usage:

var vipUser = StepwiseBuilders.UserBuilder()
    .SetName("Bob")
    .SetAge(45)
    .SetMembershipLevel("Gold") //After SetAge(), control transfers into VipUserBuilder steps via extension method.
    .Build(b => new VipUser
    {
        Name = b.OriginalBuilder.SetNameValue,
        Age = b.OriginalBuilder.SetAgeValue,
        MembershipLevel = b.SetMembershipLevelValue
    });

❓ FAQ

Q: Can I inject dependencies into builders?
A: Yes. Builders are partial classes. You can define additional constructors in another partial file.

Q: What happens if I omit fieldName in AddStep()?
A: A field named {StepName}Value is automatically created.

Q: Can I customize the Build() process?
A: Yes. You must define Build() logic. No Build method is auto-generated without your code.

Q: Can it be used for things beyond object construction?
A: Absolutely. Builders can be used for data aggregation, pipelines, workflows, or any ordered data collection.

Q: Does this add any runtime cost?
A: No. Everything happens at compile-time. No reflection or dynamic code at runtime.


📄 License

Licensed under the MIT License.


Product 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.

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
4.0.0 138 6/16/2025
3.0.0 241 5/14/2025
2.2.2 180 4/28/2025
2.2.1 99 4/26/2025
2.2.0 93 4/26/2025
2.1.3 106 4/25/2025
2.1.2 104 4/25/2025
2.1.1 117 4/25/2025
2.1.0 161 4/24/2025
2.0.2 160 4/24/2025
2.0.1 164 4/24/2025
2.0.0 169 4/23/2025
1.0.6 486 3/25/2025
1.0.5 480 3/25/2025
1.0.4 465 3/24/2025
1.0.3 111 1/13/2025
1.0.2 93 1/13/2025
1.0.1 106 1/9/2025
1.0.0 105 1/9/2025