Updated on 3 min read Manikandan Design Patterns

Builder Design Pattern in .NET Core API

A beginner-friendly introduction to the Builder pattern in C# with a simple example, three common subtypes, and UML.

A beginner-friendly introduction to the Builder pattern in C# with a simple example, three common subtypes, and UML.

Intro

Builder is a creational design pattern used to create complex objects step by step. It helps you avoid large constructors with many parameters and keeps object creation readable.

What Is the Builder Pattern?

The Builder pattern separates object construction from the final object representation.

In simple terms:

  • You call small methods to set parts of an object.
  • You call Build() at the end to get the final object.
  • You can create different object variations using the same builder.

Simple Builder Example

Suppose we want to build a House object with optional fields.

public class House
{
public string RoofType { get; set; } = string.Empty;
public int Rooms { get; set; }
public bool HasGarage { get; set; }
}
public class HouseBuilder
{
private readonly House _house = new();
public HouseBuilder SetRoof(string roofType)
{
_house.RoofType = roofType;
return this;
}
public HouseBuilder SetRooms(int rooms)
{
_house.Rooms = rooms;
return this;
}
public HouseBuilder AddGarage()
{
_house.HasGarage = true;
return this;
}
public House Build() => _house;
}

Usage

var house = new HouseBuilder()
.SetRoof("Concrete")
.SetRooms(3)
.AddGarage()
.Build();

Why Use Builder?

  • Improves readability when many optional fields exist.
  • Avoids constructor parameter overload.
  • Makes object creation easier to maintain.
  • Supports fluent method chaining.

When to Use It

  • When object construction is complex and involves many optional steps or configurations.
  • When you want to avoid constructor telescoping (many overloaded constructors).
  • When you want cleaner and more readable creation code.

Core Components

PartPurposeExample in this article
BuilderStep-by-step constructionHouseBuilder
ProductThe object being builtHouse
ClientCalls builder methodsUsage code above

Sub Types of Builder

01-FluentBuilder

This subtype returns the builder instance from each method, so you can chain calls in a readable flow.

Why it matters in implementation:

  • Clean and concise object construction code.
  • Easy to add optional steps without changing caller style.
  • Great fit for APIs that build one object from many optional values.

02-StepBuilder

This subtype enforces the order of build steps using interfaces. The caller must follow the defined sequence.

Why it matters in implementation:

  • Prevents invalid object states at compile time.
  • Useful when some steps are mandatory.
  • Improves correctness in complex construction flows.

03-DirectorBasedBuilder

This subtype uses a Director class to control and reuse construction sequences for common object variants.

Why it matters in implementation:

  • Reuses standard build recipes across the app.
  • Keeps repeated construction flow out of controllers/services.
  • Helps when you need multiple predefined object configurations.

Advantages and Disadvantages

Advantages

  • Simplifies building complex objects.
  • Keeps code readable and clean.
  • Reduces constructor complexity.
  • Makes optional fields easier to manage.

Disadvantages

  • Can introduce extra classes and interfaces if overused.
  • May be overkill for simple objects.

UML Diagram

classDiagram
class Client
class Product
class Builder {
+Build() Product
}
class ConcreteBuilder
ConcreteBuilder --|> Builder
Builder --> Product
Client --> Builder

Summary

The Builder pattern is useful when object creation has many steps or optional values. It improves readability, keeps constructors simple, and helps you create objects in a clean, maintainable way.

Share:
Back to Blog

Related Posts

View All Posts »