SweetBuilders 3.2.0
dotnet add package SweetBuilders --version 3.2.0
NuGet\Install-Package SweetBuilders -Version 3.2.0
<PackageReference Include="SweetBuilders" Version="3.2.0" />
paket add SweetBuilders --version 3.2.0
#r "nuget: SweetBuilders, 3.2.0"
// 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
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:
Builder<T>.New
uses the default constructor ofT
(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
Builder<T>.Auto
enables auto properties and works just like callingFixture.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)
Builder<T>.Uninitialized
will create an instance ofT
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
Builder<T>.From(Func<T> factory)
will use the specified factory to create an instance ofT
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.
- Public properties with private sets
var foo = Builder<Foo>.Empty
.WithPrivate(x => x.Id, "abc")
.Create();
Console.WriteLine(foo.Id); // abc
- 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 | Versions 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. |
-
.NETStandard 2.0
- AutoFixture (>= 4.17.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.