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.

namespace FluentAPiExample
{
    public class Employee
    {
        public int ID;
        public string Name;
        public string Location;
        public int age;
    }
}

Next, we will build the Interface for the Fluent API class as below.

using System.Collections.Generic;
namespace FluentAPiExample
{
    public interface IEmpDetails
    {
        IEmpDetails LoadEmployees();
        IEmpDetails FilterByLocation(string location);
        IEmpDetails FilterByAge(int age);
        List<Employee> GetData();
        List<Employee> Employees { get; set; }
    }
}

Finally, we implement the Interface as below.

using System.Collections.Generic;
using System.Linq;
namespace FluentAPiExample
{
    public class EmpDetails : IEmpDetails
    {
        public List<Employee> Employees { get; set; }
        public IEmpDetails LoadEmployees()
        {
            Employees = new List<Employee>()
            {
                new Employee { ID = 1, Name = "John Doe", age = 25, Location = "Toronto" },
                new Employee { ID = 2, Name = "Jane Doe", age = 30, Location = "Toronto" },
                new Employee { ID = 3, Name = "Jana Doe", age = 35, Location = "Montreal" },
                new Employee { ID = 4, Name = "Mike Doe", age = 40, Location = "Montreal" },
                new Employee { ID = 5, Name = "Sam Doe", age = 45, Location = "Toronto" }
            };
            return this;
        }
        public IEmpDetails FilterByLocation(string location)
        {
            Employees = Employees.Where(s => s.Location == location).ToList();
            return this;
        }
        public IEmpDetails FilterByAge(int age)
        {
            Employees = Employees.Where(s => s.age <= age).ToList();
            return this;
        }
        public List<Employee> GetData()
        {
            return Employees;
        }
    }
}

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.

using System;
namespace FluentAPiExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Using all filter options
            var emps = new EmpDetails()
                .LoadEmployees()
                .FilterByLocation("Montreal")
                .FilterByAge(50)
                .GetData();
            foreach (var emp in emps)
            {
                Console.WriteLine($"Employee details are {emp.ID},{emp.Name},{emp.Location},{emp.age}");
            }
            // Using one filter option
            var empsCityOnly = new EmpDetails()
                .LoadEmployees()
                .FilterByLocation("Toronto")
                .GetData();
            foreach (var emp in empsCityOnly)
            {
                Console.WriteLine($"Employee details are {emp.ID},{emp.Name},{emp.Location},{emp.age}");
            }

            Console.ReadKey();
        }
    }
}

In the above example, you can see that we can use the filters as per our requirement and get the below output.

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.


Similar Articles