A source-generator-based implementation of the Builder pattern
MIT License
This is a .Net Source Generator designed to add "Builders" to your projects. Builders are an object creation pattern, similar to the Object Mother pattern. Object Mothers and Builders are most commonly used to create objects for testing, but they can be used anywhere you want "canned" objects.
For more complete documentation, please see the documentation site or the raw documentation source.
This project has moved to the .Net 6 version of source generators, which unfortuntely means that it's incompatible with Visual Studio 2019. It's also breaking the GitHub build pipeline at the moment. It all seems to work just fine in VS2022 though. If you're stuck on .Net 5 and VS2019, you can always use the v1.x series, although its usage is different.
BuilderGenerator is installed as an analyzer via NuGet package (https://www.nuget.org/packages/BuilderGenerator/). You can find it through the "Manage NuGet Packages" dialog in Visual Studio, or from the command line.
Install-Package BuilderGenerator
After installation, create a partial class to define your builder in. Decorate it with the BuilderFor
attribute, specifying the type of class that the builder is meant to build (e.g. [BuilderFor(typeof(Foo))]
. Define any factory and helper methods in this partial class. Meanwhile, another partial class definition will be auto-generated which contains all the "boring" parts such as the backing fields and "with" methods.
v2.4.0
v2.3.0
v2.2.0
v2.0.7
v2.0.6
v2.0.5
v2.0.3
v2.0.2
v2.0.1
v2.0.0
v1.2
v1.0
v0.5
The BuilderGenerator logo includes tools by John Caserta from the Noun Project.