SweetBuilders 3.2.0

This package has a SemVer 2.0.0 package version: 3.2.0+build.70.
dotnet add package SweetBuilders --version 3.2.0
NuGet\Install-Package SweetBuilders -Version 3.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="SweetBuilders" Version="3.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SweetBuilders --version 3.2.0
#r "nuget: SweetBuilders, 3.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.
// Install SweetBuilders as a Cake Addin
#addin nuget:?package=SweetBuilders&version=3.2.0

// Install SweetBuilders as a Cake Tool
#tool nuget:?package=SweetBuilders&version=3.2.0

SweetBuilders

SweetBuilders NuGet Package SweetBuilders NuGet Package Downloads GitHub Actions Status codecov

A generic implementation of the Builder Pattern for C#, for those who love syntactic sugar. Built on the top of AutoFixture.

This library encapsulates AutoFixture's Fixture.Build<T>() method and gives you a straightforward API for you to build your objects exactly how you want. It also provides common, generic object factories and the ability to set private fields and properties.

Getting started

Given the following classes Foo and Bar, see examples below:

public class Foo {
  
  public Foo() {
    Id = Guid.NewGuid().ToString();
  }

  public string Id { get; set; }
  public string? Prop1 { get; set; }
  public string? Prop2 { get; set; }
}

public class Bar {
  private string id;
  
  public Bar() {
    id = Guid.NewGuid().ToString();
  }

  public Foo? Foo { get; set; }
  public string? FooId { get; set; }
}

Using Builder<T>

This is the fastest way to go. This class inherits the abstract class BuilderBase<TObject, TBuilder> and exposes 4 start points:

  1. Builder<T>.New uses the default constructor of T (even if it's private)
var foo = Builder<Foo>.New
  .With(x => x.Prop1)
  .Create();

Console.WriteLine(foo.Id); // (Guid generated by the constructor)
Console.WriteLine(foo.Prop1); // (random value generated by AutoFixture)
Console.WriteLine(foo.Prop2); // null
  1. Builder<T>.Auto enables auto properties and works just like calling Fixture.Build<T>()
var foo = Builder<Foo>.Auto
  .With(x => x.Prop1, "abc")
  .Create();

Console.WriteLine(foo.Id); // (Guid generated by the constructor)
Console.WriteLine(foo.Prop1); // abc
Console.WriteLine(foo.Prop2); // (random value generated by AutoFixture)
  1. Builder<T>.Uninitialized will create an instance of T without using its constructors
var foo = Builder<Foo>.Uninitialized
  .With(x => x.Prop1, "abc")
  .Create();

Console.WriteLine(foo.Id); // null
Console.WriteLine(foo.Prop1); // abc
Console.WriteLine(foo.Prop2); // null
  1. Builder<T>.From(Func<T> factory) will use the specified factory to create an instance of T
var foo = Builder<Foo>.From(() => new Foo())
  .With(x => x.Prop1, "abc")
  .Create();

Console.WriteLine(foo.Id); // (Guid generated by the constructor)
Console.WriteLine(foo.Prop1); // abc
Console.WriteLine(foo.Prop2); // (random value generated by AutoFixture)

Setting private properties and fields

This feature is not supported by AutoFixture, but it's very useful in many scenarios.

  1. Public properties with private sets
var foo = Builder<Foo>.Empty
  .WithPrivate(x => x.Id, "abc")
  .Create();

Console.WriteLine(foo.Id); // abc
  1. Private properties and fields
var bar = Builder<Bar>.Empty
  .WithPrivate("id", "xyz")
  .Create();

// bar.id will be "abc"

Creating a custom builder

You can inherit BuilderBase<TObject, TBuilder> to create your own custom builders with some default specific behaviors:

public class BarBuilder : BuilderBase<Bar, BarBuilder> {
  
  public BarBuilder()
    : base(Factories.Uninitialized<Bar>) { }

  public BarBuilder WithFoo(Foo foo) {
    With(x => x.Foo, foo);
    With(x => x.FooId, foo?.Id);
    return this;
  }
}

var foo = Builder<Foo>.New
  .With(x => x.Prop1, "abc")
  .Create();

var bar = new BarBuilder()
  .WithFoo(foo)
  .Create();

// bar.id will be null because it uses the uninitialized factory
Console.WriteLine(bar.Foo.Prop1); //abc
Console.WriteLine(bar.FooId); // (Guid generated by Foo's constructor)
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. 
.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
3.2.0 2,088 5/29/2022
3.1.0 100 4/14/2022
3.0.0 96 4/11/2022
2.0.0 100 4/11/2022
1.1.0 104 4/10/2022
1.0.0 97 4/10/2022