Creating A Fluent API In C#.NET

Introduction

 
In this article we will look at creating a Fluent API class in C#.NET. In a Fluent API, we can link together different functionalities to get a particular result. We see this used many times in Entity Framework Core and when manipulating List<T> type of items using lambda expressions.

Creating the Fluent API class

 
We will create a simple employee class using Visual Studio 2019 Community edition as below,
  1. namespace FluentAPiExample  
  2. {  
  3.     public class Employee  
  4.     {  
  5.         public int ID;  
  6.         public string Name;  
  7.         public string Location;  
  8.         public int age;  
  9.     }  
  10. }  
Next, we will build the Interface for the Fluent API class as below,
  1. using System.Collections.Generic;  
  2.   
  3. namespace FluentAPiExample  
  4. {  
  5.     public interface IEmpDetails  
  6.     {  
  7.         IEmpDetails LoadEmployees();  
  8.         IEmpDetails FilterByLocation(string location);  
  9.         IEmpDetails FilterByAge(int age);  
  10.         List<Employee> GetData();  
  11.         List<Employee> Employees { getset; }  
  12.     }  
  13. }  
Finally, we implement the Interface as below,
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3.   
  4. namespace FluentAPiExample  
  5. {  
  6.     public class EmpDetails : IEmpDetails  
  7.     {  
  8.         public List<Employee> Employees { getset; }  
  9.   
  10.         public IEmpDetails LoadEmployees()  
  11.         {  
  12.             Employees = new List<Employee>() {  new Employee { ID = 1, Name = "John Doe", age = 25, Location = "Toronto" },  
  13.                                                 new Employee { ID = 2, Name = "Jane Doe", age = 30, Location = "Toronto" },  
  14.                                                 new Employee { ID = 3, Name = "Jana Doe", age = 35, Location = "Montreal" },  
  15.                                                 new Employee { ID = 4, Name = "Mike Doe", age = 40, Location = "Montreal" },  
  16.                                                 new Employee { ID = 5, Name = "Sam Doe", age = 45, Location = "Toronto" }  
  17.             };  
  18.             return this;  
  19.   
  20.         }  
  21.         public IEmpDetails FilterByLocation(string location)  
  22.         {  
  23.             Employees = Employees.Where(s => s.Location == location).ToList();  
  24.             return this;  
  25.         }  
  26.         public IEmpDetails FilterByAge(int age)  
  27.         {  
  28.             Employees = Employees.Where(s => s.age <= age).ToList();  
  29.             return this;  
  30.         }  
  31.   
  32.         public List<Employee> GetData()  
  33.         {  
  34.             return Employees;  
  35.         }  
  36.   
  37.     }  
  38. }  
Here you can see that every function returns the existing class in order to create a Fluent API chain. However, the final function “GetData” returns the main expected value.
 

Creating the Client function

 
Now we can use the Fluent API class as below,
  1. using System;  
  2.   
  3. namespace FluentAPiExample  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.               
  10.             // Using all filter options  
  11.             var emps = new EmpDetails().LoadEmployees().FilterByLocation("Montreal").FilterByAge(50).GetData();  
  12.   
  13.             foreach(var emp in emps)  
  14.             {  
  15.                 Console.WriteLine($"Employee details are {emp.ID},{emp.Name},{emp.Location},{emp.age}");  
  16.             }  
  17.   
  18.             // Using one filter option  
  19.             var empsCityOnly = new EmpDetails().LoadEmployees().FilterByLocation("Toronto").GetData();  
  20.   
  21.             foreach (var emp in empsCityOnly)  
  22.             {  
  23.                 Console.WriteLine($"Employee details are {emp.ID},{emp.Name},{emp.Location},{emp.age}");  
  24.             }  
  25.   
  26.             Console.ReadKey();  
  27.         }  
  28.     }  
  29. }  
In the above example, you can see that we can use the filters as per our requirement and get the below output,
 
 

Summary

 
In this article, we have looked at creating a Fluent API class. This is especially useful when we want to create a chain of commands and allow the client to apply these as required.