A Generic Builder Pattern Implementation in C#

The builder pattern stands out as a design pattern that is a particularly useful solution to object creation problems in object-oriented programming. It is particularly suited to dynamically creating objects in a step-by-step process.

Typically, this pattern begins with either a default or a required state, then offers a series of options through setter methods to update the default. These setter methods uniquely return the in-progress instance of the builder, rather than the final target object being built. Then, only at the end is there a Build() method used for materializing the target object.

Let’s consider the following simple example to get a better understanding:

public class Car
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
}

public class CarBuilder
{
    private Car car = new Car();

    public CarBuilder WithMake(string make)
    {
        car.Make = make;
        return this;
    }

    public CarBuilder WithModel(string model)
    {
        car.Model = model;
        return this;
    }

    public CarBuilder WithYear(int year)
    {
        car.Year = year;
        return this;
    }

    public Car Build()
    {
        return car;
    }
}

Then we can create an instance of Car through a series of consecutive (or fluent) method calls, like:

var myCar = new CarBuilder()
    .WithMake("Toyota")
    .WithModel("Corolla")
    .WithYear(2021)
    .Build();

Of course, on the web, you can find a lot of variations on this pattern, maybe following different naming conventions, but maintaining common features like that fluent style.

A Generic Approach

To avoid implementing one builder class per target class, let’s look at how we can leverage generics in C# to solve this problem. My generics example is also very simplistic as it’s just for demo purposes. In the real world, you can find much longer implementations or a set of multiple collaborating builders working together.

This approach could be used for virtually any object type as long as it exposes some property setters, which you typically find in POCOs (Plain-Old CLR Objects), maybe working as DTOs (Data Transfer Objects) or models being mapped between the different layers in an application.

Let’s dive into a simplified solution that embodies these principles. Check out the function below:

public static T Create<T>(Action<T>? perform = null) where T : new()
{
    var newObject = new T();
    perform?.Invoke(newObject);

    return newObject;
}

It’s just a function, but you get every essential purpose for it and this corresponding calling code:

var myCar = Create<Car>(with =>
    {
        with.Make = "Toyota";
        with.Model = "Corolla";
        with.Year = 2021;
    });

This is a very flexible solution. For example, notice this simple variation for supporting different potential use cases:

// Just leave the default values for all properties
var myDefaultCar = Create<Car>();

// Set partial state
var myPartialCar = Create<Car>(with =>
    {
        with.Make = "Toyota";
        with.Year = 2021;
    });

This generic approach excels in testing scenarios, where the pattern is better known more specifically as the Test Data Builder pattern. Creating diverse test cases is crucial to confirm that a particular functionality behaves as expected under various scenarios. In this context, stub objects, typically derived from input model objects, are employed. Therefore, it is essential to have a rapid and efficient method for constructing these variations, as facilitated by the generic builder implementation.

By now, you should have a good grasp of the concept, so it’s your turn to extend or adapt these insights to fit your needs.

For instance, suppose that now our target class (Car) has a constructor requiring a parameter value intended for a property (e.g. ‘Make’), also removing the public accessor for its setter. This would make our function ‘Create<T>’ unuseful for this specific case. However, again, this is not about suggesting a specific implementation but a guideline instead. In this case, you could create an extension method like this:

public static T With<T>(this T target, Action<T> with)
{
    with(target);

    return target;
}

For being used as:

var myCar = new Car("Toyota").With(x =>
    {
        x.Model = "Corolla";
        x.Year = 2021;
    });

Though this is a different implementation, it accomplished the same thing. That is a very pluggable solution that can be ported to the implementation of other patterns and approaches. It requires minimal code as is very reusable code, allowing you to be focused on the real thing you’re developing like business logic or testing logic.

Now that you’ve seen it, how would you use this pattern?

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.