Fluent Interfaces in C# – Introduction
But not all languages are created equal in the world of DSLs, there is a branch of languages called âInternal DSLsâ â that any developer worth his salt can leverage to create better, more readable code.
Internal DSLs and fluent interfaces
The basic idea of a domain specific language (DSL) is a computer language that’s targeted to a particular kind of problem, rather than a general purpose language that’s aimed at any kind of software problem. Domain specific languages have been talked about, and used for almost as long as computing has been done.
Martin Fowler â DomainSpecificLanguage
And so internal DSL (a.k.a âEmbedded DSL) is a domain specific language created using an existing (host) language.
Within the group of internal DSLs there is sub group known as âfluent interfacesâ â basically using the host language to create code that can be easily read â and look (after a fashion) just like plain English.
The term âFluent Interfacesâ was coined by Eric Evens and Martin Fowler and it refers to an implementation of object oriented API on top of existing functionality in order to provider more readable code.
Why should you care?
There are many benefits of using fluent interfaces â you can create a easy to use and understand API that does not require any additional libraries to implement.
That API provides directions on how the code should be used and can help your users use your software or you can use it internally in order to make a complicated process easy to code by your teammates.
There is one catch â creating a good API is always difficult and creating fluent interface requires understanding of certain tricks in the language that can be leveraged to create the desired code.
C# just like any other object oriented language has the capability to create fluent API in fact certain feature of C# makes it extremely easy to create such API.
What’s next
In the upcoming posts Iâm going to show a few tricks that Iâve used in the past to create fluent interfaces all of which you probably already know.
So until next time â happy codingâŠ
Reference: Fluent Interfaces in C#âIntroduction from our NCG partner Dror Helper at the Helper Code blog.