StepwiseBuilderGenerator 2.0.2

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

Stepwise Builder Generator

A lightweight C# source generator that produces strongly-typed, stepwise “fluent” builders. Simply annotate a class and describe its steps; the generator emits interfaces and a partial class to guide callers through each required step.


Features

  • AddStep<T>(stepName, fieldName = null)
    Define each required input in order.

  • Default-value support
    Supply a factory for a step so callers can use .StepName() with no arguments.

  • BranchFrom(baseBuilder, baseStep)
    Insert an alternate path from one builder into another.

  • Enum of steps
    Every generated builder includes enum Steps { … } for logging or reflection.

  • Static factories
    StepwiseBuilders.YourBuilder() to kick off a chain.


Examples

1. Basic builder

Target type:

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

Builder declaration:

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

Usage:

var order = StepwiseBuilders.OrderBuilder()
    .SetId(123)
    .SetCustomer("Acme Co.")
    .SetTotal(99.95m)
    .Build(b => new Order
    {
        Id       = b.Id,
        Customer = b.SetCustomerValue,
        Total    = b.SetTotalValue
    });

2. Default-value steps

Target type:

public class ReportConfig
{
    public string Title        { get; init; }
    public bool   IncludeCharts{ get; init; }
}

Builder declaration:

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

Usage:

// uses default IncludeCharts = true
var config = StepwiseBuilders.ReportConfigBuilder()
    .WithTitle("Q1 Results")
    .IncludeCharts()   // no arg → defaultValueFactory invoked
    .Build();          // no arg → defaultValueFactory invoked

3. Branching between builders

Base builder (UserBuilder):

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

Branching builder (VipUserBuilder):

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

Usage:

// regular user
var u1 = StepwiseBuilders.UserBuilder()
    .SetName("Alice")
    .SetAge(30)
    .Build(b => 
    {
        Name = b.SetNameValue,
        Age = b.SetAgeValue
    });

// VIP user branches in after SetAge
var vip = StepwiseBuilders.UserBuilder()
    .SetName("Bob")
    .SetAge(45)
    .SetMembershipLevel("Gold")
    .Build(b 
    {
        Name = b.OriginalBuilder.SetNameValue,
        Age = b.OriginalBuilder.SetAgeValue,
        SetMembershipLevel = b.SetMembershipLevelValue
    });

FAQ

Q: Can I inject services or dependencies into a builder?
A: Yes—since the generated builders are partial classes, you’re free to add your own constructor(s) (e.g. taking ILogger, IRepository, etc.) in another partial file. Dependency-injected fields or properties will be available when the step methods run.

Q: What if I omit the fieldName in AddStep?
A: A field named {StepName}Value is generated automatically.

Q: How do I supply custom “Build” logic?
A: Call .Build(instance => /* your mapping to target */) or add extension methods on the final build interface for reusable patterns.

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 130 6/16/2025
3.0.0 234 5/14/2025
2.2.2 175 4/28/2025
2.2.1 93 4/26/2025
2.2.0 85 4/26/2025
2.1.3 101 4/25/2025
2.1.2 98 4/25/2025
2.1.1 111 4/25/2025
2.1.0 156 4/24/2025
2.0.2 154 4/24/2025
2.0.1 154 4/24/2025
2.0.0 164 4/23/2025
1.0.6 481 3/25/2025
1.0.5 475 3/25/2025
1.0.4 461 3/24/2025
1.0.3 107 1/13/2025
1.0.2 87 1/13/2025
1.0.1 100 1/9/2025
1.0.0 101 1/9/2025