top of page

Fluent Interfaces and Method Chaining using C#


Fluent Interface

A fluent interface is an object-oriented API that depends largely on method chaining. The goal of a fluent interface is to reduce code complexity, make the code readable, and create a domain specific language (DSL). It is a type of method chaining in which the context is maintained using a chain.

DeleteRowsFrom(“Account”).Where(“Level”).IsNotEqualTo(“Admin”).Where(“Balance”).IsEqualTo(0).RunNow();

Steps to build a fluent interface

There are three steps I use to create a fluent interface. They are:

  1. Define all possible combinations of the natural language syntax

  2. Create the interfaces that enforce the grammar rules

  3. Build the class, implementing the interfaces


Fluent Interface example in C#

Let say, we have the following Employee class.

public class Employee
{
    public string FullName {get; set;}
    public DateTime DateofBirth {get; set;}
    public string Department {get; set;}
    public string Address {get; set;}
}

If we want to consume the above Employee class, then we generally, create an instance of the Employee and set the respective properties as shown below.

Employee employee = new Employee();
employee.FullName = "Varun Kumar";
employee.DateOfbirth = Convert.ToDateTime ("10/08/1989");
employee.Department = "IT";
employee.Address = "Mumbai";

The Fluent interfaces simplify your object consumption code by making your code more simple, readable, and discoverable. Is not it nice to be set the object properties as shown below?

Employee employee = new Employee();

employee.NameOfTheEmployee ("Varun Kumar")
		.Born ("10/08/1989")
		.WorkingOn ("IT")
		StaysAt("Mumbai");

If 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.


Method Chaining

Method chaining is a technique in which methods are called on a sequence to form a chain and each of these methods return an instance of a class. These methods can then be chained together so that they form a single statement.


You might already be using method chaining in your applications, knowingly or unknowingly. The following code snippet illustrates how methods are chained.

var data = authorList.Where(a => a.Country == "USA")
                     .OrderBy(a => a.AuthodId)
                     .ToList();

In method chaining, when you call a method the context flows from the method called to another method, i.e., the next method in the chain. Hence the term “chaining” is used to describe this pattern.


Method chaining example in C#

The following code snippet provides a good example of method chaining.

public class Program 
{ 
    public static void Main(string[] args) 
    { 
         var host = new WebHostBuilder() 
        .UseKestrel() 
        .UseContentRoot(Directory.GetCurrentDirectory()) 
        .UseStartup<Startup>() 
        .Build(); 
        host.Run(); 
    } 
}


Difference Between Fluent Interfaces and Method Chaining

Fluent Interfaces

Method Chaining

Method chaining is a technique in which methods are called on a sequence to form a chain and each of these methods return an instance of a class.

A fluent interface is an object-oriented API that depends largely on method chaining.

In this, Method should return an instance of the same type

In this, methods may return instances of any class

Fluent interfaces are implemented using method chaining but not all uses of method chaining are fluent interfaces.

Method chaining works on simple set of data, fluent interfaces are used to modify a complex object.



The Tech Platform

0 comments
bottom of page