Safalin.BuilderGenerator
2.0.5
dotnet add package Safalin.BuilderGenerator --version 2.0.5
NuGet\Install-Package Safalin.BuilderGenerator -Version 2.0.5
<PackageReference Include="Safalin.BuilderGenerator" Version="2.0.5" />
paket add Safalin.BuilderGenerator --version 2.0.5
#r "nuget: Safalin.BuilderGenerator, 2.0.5"
// Install Safalin.BuilderGenerator as a Cake Addin #addin nuget:?package=Safalin.BuilderGenerator&version=2.0.5 // Install Safalin.BuilderGenerator as a Cake Tool #tool nuget:?package=Safalin.BuilderGenerator&version=2.0.5
A fork of BuilderGenerator, a .NET Source Generator that is designed to add fluent builder classes to your projects. Save time by automating the builder pattern.
What are Builders?
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.
Example Usage
Create a new partial class that will hold your builder methods - for this example, we'll call it
FooBuilder
, as the class we want to work with is calledFoo
.Decorate it with the
BuilderFor
attribute, specifying the type of class that the builder is meant to build. For example:[BuilderFor(typeof(Foo))] public partial class FooBuilder { }
Rebuild your project. The source generator will run and autogenerate methods in a separate partial class file, for each property in the type you specified in Step 2.
You can also add factory methods to your partial class which can craft specific data scenarios:
[BuilderFor(typeof(Foo))] public partial class FooBuilder { public static FooBuilder Bar() { return new FooBuilder() .WithBar(true); } public static FooBuilder NotBar() { return new FooBuilder() .WithBar(false); } }
Features
Record types
With this fork, you can use the [BuilderFor]
attribute on C# record types:
public record MyRecord(bool Hello);
[BuilderFor(typeof(MyRecord)]
public partial class MyRecordBuilder
{
}
var builder = new MyRecordBuilder().WithHello(true).Build();
WithValuesFrom
The new WithValuesFrom
method allows you to copy values from an existing instance into your builder:
new MyBuilder().WithValuesFrom(anotherObject)
Documentation & Source Code
For more information, please visit the GitHub repository.
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
- Microsoft.CodeAnalysis.CSharp (>= 4.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.