Implementing Fluent Builder Pattern Using .NET and C#

Learn How to Create Complex Objects and Simplify Code with Fluent Builder Pattern for C# Developers

Abdelmajid BACO
3 min readFeb 20, 2023
Photo by Edgar Chaparro on Unsplash

The Fluent Builder Pattern is a popular design pattern in object-oriented programming. It is a variation of the Builder Pattern that focuses on making the process of creating complex objects easier and more intuitive for developers.

In this article, we will explore the Fluent Builder Pattern and provide a practical example in C#.

What is the Fluent Builder Pattern?

The Fluent Builder Pattern is a design pattern that allows developers to create complex objects by chaining together a series of method calls that set the values of individual properties.

The Fluent Builder Pattern can be used to create objects with a large number of properties, or objects with complex relationships between their properties. This pattern can also be used to create objects that are composed of other objects, or to create objects with dynamic properties.

The Fluent Builder Pattern is built on top of the Builder Pattern, which is a pattern that is used to separate the construction of a complex object from its representation. The Builder Pattern is useful when the construction of an object requires a large number of steps, or when the order of those steps matters.

Implementing the Fluent Builder Pattern in C#

To implement the Fluent Builder Pattern in C#, we will create a simple example that demonstrates how the pattern can be used to create a complex object. In this example, we will create an object that represents a person.

Here is the code for our Person class:

public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
}

To create an instance of this class using the Fluent Builder Pattern, we will create a separate builder class that contains a series of methods that can be used to set the values of each property. Here is the code for our PersonBuilder class:

public class PersonBuilder
{
private Person person = new Person();

public PersonBuilder WithFirstName(string firstName)
{
person.FirstName = firstName;
return this;
}

public PersonBuilder WithLastName(string lastName)
{
person.LastName = lastName;
return this;
}

public PersonBuilder WithAge(int age)
{
person.Age = age;
return this;
}

public PersonBuilder WithAddress(string address)
{
person.Address = address;
return this;
}

public PersonBuilder WithCity(string city)
{
person.City = city;
return this;
}

public PersonBuilder WithState(string state)
{
person.State = state;
return this;
}

public PersonBuilder WithZipCode(string zipCode)
{
person.ZipCode = zipCode;
return this;
}

public Person Build()
{
return person;
}
}

As you can see, the PersonBuilder class contains a series of methods that can be used to set the values of each property of the Person object. Each method returns the instance of the PersonBuilder class, which allows the methods to be chained together.

To create an instance of the Person object using the Fluent Builder Pattern, we can use the following code:

var person = new PersonBuilder()
.WithFirstName("John")
.WithLastName("Doe")
.Build();

Conclusion

Overall, the Fluent Builder pattern is a powerful tool for creating complex objects in an intuitive and maintainable way, and can be a valuable addition to any software developer’s toolkit.

Thank you so much for reading! If you found this information useful, please consider following me for more helpful tips and resources.

--

--

Abdelmajid BACO

Senior Full Stack .Net / Angular Developer, Cloud & Azure DevOps, Carrier Manager, Husband, and Father.