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.
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; } }This is the fastest way to go. This class inherits the abstract class BuilderBase<TObject, TBuilder> and exposes 4 start points:
Builder<T>.Newuses 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); // nullBuilder<T>.Autoenables 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>.Uninitializedwill create an instance ofTwithout 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); // nullBuilder<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)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"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)