Fluent Interface Design Pattern in C# with Examples
In this article, I will talk about the Fluent Interface Design Pattern in C# with models. The Fluent Interface Design Pattern is one of the most often utilized plan designs progressively applications. The Fluent Interface Design Pattern in C# falls under the classification of Creational Design Pattern.
Note: The Fluent Interfaces and Method chaining are related to each other. Or we can say that one is a concept and the other one is its implementation. Here in this article, first, we will discuss fluent interfaces and then we will move towards method chaining.
What is the Fluent Interface Design Pattern?
The main objective of the Fluent Interface Design Pattern is that we can apply multiple properties (or methods) to an object by connecting them with dots (.) without having to re-specify the object name each time.
How to Implement Fluent Interface Design Pattern in C#?
public class Employee { public string
FullName { get; set; } public DateTime
DateOfBirth { get; set; } public string
Department { get; set; } public string
Address { get; set; } } |
Employee emp = new
Employee() { FullName =
"DotNet office", DateOfBirth =
Convert.ToDateTime("01/01/2022"), Department =
"Developement", Address =
"india" }; |
Employee emp = new Employee(); emp.NameOfTheEmployee("Dotnet
office")
.Born("01/01/2022")
.WorkingOn("Developement") .StaysAt("India"); |
f we create such kinds of interfaces, then it is like speaking a sentence that would really make the class consumption code more simple and more readable. Now the next thing is how to achieve this. To achieve this, we have something called method chaining.
What is Method Chaining?
Method chaining is a common technique where each method returns an object and all these methods can be chained together to form a single statement. In order to achieve this, first, we need to create a wrapper class around the Employee class as shown below.
public class FluentEmployee { private Employee
employee = new Employee(); public
FluentEmployee NameOfTheEmployee(string FullName) {
employee.FullName = FullName; return this; } public
FluentEmployee Born(string DateOfBirth) {
employee.DateOfBirth = Convert.ToDateTime(DateOfBirth); return this; } public
FluentEmployee WorkingOn(string Department) {
employee.Department = Department; return this; } public
FluentEmployee StaysAt(string Address) {
employee.Address = Address; return this; } } |
static void
Main(string[] args) { FluentEmployee
obj = new FluentEmployee();
obj.NameOfTheEmployee("DotNet office") .Born("01/01/2022")
.WorkingOn("Developement") .StaysAt("India");
Console.ReadLine(); } |
When do we need to use the Fluent Interface Design Pattern in C#?
- During UNIT testing when the developers are not full-fledged programmers.
- When you want your code to be readable by non-programmers so that they can understand if the code is satisfied with their business logic or not.
- If you are a component seller and you want to stand out in the market as compared to the others by making your interface simpler.
I have seen fluent interfaces are used extensively in LINQ Queries: Searching, Sorting, pagination, grouping with a blend of LINQ are some of the real-world usages of the fluent interface in combination with the builder design pattern.
EmoticonEmoticon