Skip to content

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

License

Notifications You must be signed in to change notification settings

victorsebrito/SweetBuilders

Repository files navigation

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)

About

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

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks