Creating a generic builder pattern for classes with no parameterless constructor

Viewed 878

We are attempting to implement the builder pattern in an extensible way that does not rely on our devs coding the builder directly using the common With pattern (i.e. every public field has a WithX(...) method on the builder).

We have seen a potential pattern that uses lambda expressions to change reduce the builder to a common With function that then applies the lambdas on the invocation of the Build call.

However, all the implementation of such a pattern expect the object being created should have a parameterless constructor and public setters.

The objects we are attempting to build do no have neither public setters nor parameterless constructors.

For example, our classes look like:

public class Class1
{
    public int field { get; }

    public Class1(int field)
    {
        this.field = field;
    }
}

Our builder would normally be:

public class Class1Builder
{
    private int _field;

    public Class1Builder()
    {
        this._field = 1;
    }

    public Class1Builder WithField(int field)
    {
        _field = field;
        return this;
    }

    public Class1 Build()
    {
        return new Class1(_field);
    }
}

We would like to convert it With method to something that could allow the usage of:

var class1 = new Class1Builder.With(x => x.Field = 3).Build();

Is there a way of doing such a thing?

0 Answers
Related