Want to simplify your class consumption code by making your code more simple, readable and discoverable ? Want to really please your people who consume a component with interfaces which are more signified than this, what will you do about it ? Let’s start with understanding Fluent Interface and Method Chaining.
“Fluent interface” is a specialized, self-referencing form of method chaining where the context is maintained through the chain.
“Method Chaining” is a common technique where each method returns an object and all these methods can be chained together to form a single simple statement.
These concepts are very much related to each other, one is concept and other is an Implementation to that concept.
Oops is a most accepted way of designing and programming in c# and these oop has two aspects, One aspect is where developer goes and creates the class and actually exposes properties and the other aspect is where a consumer actually creates the object of the class and tries to use that class.
Ex: simple student class, these part can be created by some developer using oops principles like abstraction, encapsulation and ensure interfaces are simple.
internal class Student
{
public string FullName { get; set; }
public DateTime Dob { get; set; }
public string Address { get; set; }
}
Most of the time creator and consumer both are the same persons, same developer creates and uses it, but there can be situations where consumer and creator are different people (UI developer and backend), like component selling companies like Telerik, Infragestics etc. creator and consumer are different.
Student student = new Student(); student.FullName = "Jhon"; student.Dob = Convert.ToDateTime("1/1/2010"); student.Address = "Bangalore";
Setting properties like Full Name Last name if you ask any oop C# developer, he will say no this is not complicated this am doing day and night.
Suppose think about if the consumer of this call is unit test developer, a person is doing unit test on your class and he really does not understand c#, or think you are a component seller and you really want to please your consumers or you want to really please your people who consume a component with interfaces which are more signified than this, what will you do about it. That’s where exactly fluent interfaces come into picture
“Fluent interfaces simplify your class consumption code by making your code more simple, readable and discoverable.”
For instance over here where we are creating a student object by using the new keyword and you know doing lot of things if can do something like this say like this customer dot Name()
//Student.NameOfTheStudent(“xyz”).BornOn (“1/1/2010”).StaysAt(“CityName”)
So If I can make or create such kind of Interface where It is like a speaking a sentence that would really make my class consumption code more simplified and more readable.
To achieve this thing we have something called as 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 simple statement.
Ex: Student.NameOfThePerson ().Borm.Stat etc.
So inorder to implement method chaining what will do is we will create a wrapper class around this customer class and will hide those complicated properties and that wrapper class will emit out fluent interface and the consumer can interact with via the Fluent Interface.
Let me go ahead and create a class the “StudentFluent” and wrap the Student class. Next step is to create a fluent Interface, Let start with first fluent interface method name that is name of the student,
public class StudentFluent
{
private Student obj = new Student();
public StudentrFluent NameOfTheStudent(string Name)
{
obj.FullName = Name;
return this;
}
public StudentFluent Bornon(string Dob)
{
obj.Dob = Convert.ToDateTime(Dob);
return this;
}
//final method should be of type void
public void StaysAt(string Address)
{
obj.Address = Address;
}
}
let’s go and create a method here called NameOfTheStudent and this NameOfTheStudent will take studenName, Now in order to ensure this method actually becomes a part of the method chain we need to ensure that this method actually exposes out StudentFluent class, why because we would like to have a method chain like this NameOfThePerson, Borns, Stays at etc..
When to use this Fluent Interface and method chaining,
- During Unit testing when the developers are not full fledged programmers,
- You want your code to be readable by non-programmers so that they can understand if the code is satisfies their domain logic.
- You are component seller and you want to stand out in the market as compared to the others by making your interfaces simpler.
- You are creating a DSL language is mapped with fluent interfaces statements.
Overall, we have an effective pipelining in C#. Pipelining is a powerful technique that lets data flow through the system in a declarative manner. By leveraging higher-order extension methods we can improve upon existing fluent interfaces or provide chaining capabilities throughout C#. We can even use higher-order static methods to convert some of the language’s statements into expressions thus extending the chaining capabilities even further. This approach doesn’t get us to the capabilities of what “true” functional languages like F# provide but it certainly makes working in the often imperative world of C# a bit more bearable to those of us used to having those tools at our disposal.