AbstractBuilder 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package AbstractBuilder --version 1.1.0
NuGet\Install-Package AbstractBuilder -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AbstractBuilder" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AbstractBuilder --version 1.1.0
#r "nuget: AbstractBuilder, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AbstractBuilder as a Cake Addin
#addin nuget:?package=AbstractBuilder&version=1.1.0

// Install AbstractBuilder as a Cake Tool
#tool nuget:?package=AbstractBuilder&version=1.1.0

AbstractBuilder

NuGet that contains a builder pattern for testing in C#

How to add it

Just install the NuGet AbstractBuilder. It doesn't have any kind of external dependencies.

dotnet add package AbstractBuilder

How does it works

We create a default builder and we request modifications to that builder, for every request we have a new builder (with all the previous modifications and the new ones). Finally when we call to Build method we create the object.


    MyBuilder builder = new MyBuilder()
        .WithArms(2)
        .WithLegs(2);

    Person john = builder.WithName("John").Build();
    Person peter = builder.WithName("Peter").Build();

In the previous example we share 2 modifications and we create 2 instances of the class Person.

How can I use it? - Way 1: Heritage

You just have to inherit from AbstractBuilder class where the generic type is the result. The build steps can be as simple as this example or as complex as you want.


    public MyBuilder : AbstractBuilder<Person>
    {
        // DEFAULT CONSTRUCTOR
        public MyBuilder() : this(CreateDefaultValue)
        {
        }

        // MANDATORY CONSTRUCTOR
        protected MyBuilder(Func<Person> seedFunc) : base(seedFunc)
        {
        }

        public MyBuilder WithName(string name)
        {
            return Set<MyBuilder>(x => x.Name = name);
        }

        private static Result CreateDefaultValue()
        {
            return new Person {
                IsAlive = true
            };
        }
    }

Your builder needs a public constructor (in the example it is the default constructor) and it always needs a constructor with the seed (the visibility is not important). In this example we can set the property "Name" with the method WithName. We way of creating new methods that affects to the builder should be using always the method Set.

How can I use it? - Way 2: Using the abstract builder itself

I don't want to be restrictive so you can use it directly. The problem is that you will have to declare everything and you will not reuse the builder.


    var builder = new AbstractBuilder<Person>()
        .Set(x => x.Arms = 2)
        .Set(x => x.Legs = 2);

    Person john = builder.Set(x => x.Name = "John").Build();
    Person peter = builder.Set(x => x.Name = "Peter").Build();

Could we aggregate some modifications in one call?

Yes, we can. We can call multiple times to the lambda action or surround with brackets.


    var builder1 = new AbstractBuilder<Person>().Set(x => x.Arms = 2, x => x.Legs = 2)

    var builder2 = new AbstractBuilder<Person>().Set(x => {
        x.Arms = 2;
        x.Legs = 2;
    });

Asyncronous build

The process is same but with the syncronous method BuildAsync. The cancellation token can be accesible passing the argument of type BuilderContext.


    var builderContext = new BuilderContext() { CancellationToken = myCancellationToken };

    var builder = new AbstractBuilder<Person>()
        .Set((x, ctx) => x.Arms = 2)
        .Set((x, ctx) => x.Legs = 2);

    Person john = await builder.Set(x => x.Name = "John").BuildAsync(builderContext);
    Person peter = await builder.Set(x => x.Name = "Peter").BuildAsync(builderContext);

BuilderContext

You can inherit from BuilderContext to pass you own arguments if you need them apart form the CancellationToken.

When there are more than one constructor in our builder, the priority is for the constructor with the BuilderContext.

You can use indistinctly the different version of the method Set, the builder internally converts them into the same operation. If you don't use the version with the context then it will not be accessibe just for that method.


    var builderContext = new MyBuilderContext() { Multiplier = 5 };

    var builder = new AbstractBuilder<Person>()
        .Set((x, ctx) => x.Arms = 2 * ((MyBuilderContext)ctx).Multiplier)
        .Set(x => x.Legs = 2);

    Person mutantJohn = await builder.Set(x => x.Name = "John").BuildAsync(builderContext);
    Person mutantPeter = await builder.Set(x => x.Name = "Peter").BuildAsync(builderContext);

Product 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 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.0 1,914 1/26/2024
1.5.0 8,958 1/25/2023
1.4.0 7,299 6/1/2022
1.3.0 11,301 1/26/2022
1.2.0 6,397 1/13/2021
1.1.0 843 5/26/2020
1.0.0 529 5/19/2020