DevOps Zone is brought to you in partnership with:

Hello I'm yunus emre.I'm a 24 year old.I have been living in istanbul,Turkey.I'm incredibly passionate about what I do and enjoy working with other open minded, enthusiastic people. I also like to share my experiences with others and blog regularly here. Buzz Words : .NET 2.0 and upwards C# ASP.NET (Web Forms, MVC, Web API) HTML/CSS MSSQL RavenDB Various Data Access libraries - from Disconnected and connected for SqlClient, NHibernate and Entity Framework I currently have been working as a software engineer in intertech.I am developing software to functional requirements and fixing bugs on banking platform using microsoft technologies in here. I'm interested in backend software development and enterprise software organizations.I'm keen on mostly about software architectures , design patterns , object oriented programming and enterprise software development.I have wrote articles in my own blog and communities about these topics.Also i like drawing software diagrams To become a good software engineer requires a good theoretical background as well as practical experiences.Therefore I will try to improve myself daily.New technologies and experiences raises new ideas and so we can change the world with these ideas.I believe that. I want to develop software on international projects in a international atmosphere Yunus Emre has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Fluent Interface for More Readable Code

  • submit to reddit

Code confusion and complexity is a problem in most software projects. Especially we can clearly see this in enterprise projects. The reason for this confusion might be fast and unplanned developing, badly-designed structure and undefined architecture. But we have to know this. Code is inherently complex. We can write code with fluent interface principle to make more readable and intelligible in this complex structure and codes in some cases.

Fluent interface

  • More discoverable and readable codes
  • There is business domain information in code (we are actually composing mini-DSL )
  • If we want to create a domain-specific language, we can use it. And so business analysts can comment it when they look at code
  • it provides an easy structure to use

In other words, it provides meaning and fluency (ordered or unordered) for code clients. We’ll see the difference between traditional coding and coding with fluent interface.

Fluent Interface consist of two significant principles

  • Method chaining
  • More readable API (Domain Spesific Language-DSL)

Note: Method chaining and fluent interface is completely different thing. Fluent interface is a method-chaining application using to composing mini-DSL or readable APIs

Which case can we use this?

  • Framework when developing; Consider a company that creates software infrastructure. This infrastructure will be used by all applications that the company will develop an application framework. A lot of software engineers that developed this application frameworks. and this will be the best new applications to be produced through the roof. In this case, the team developed infrastructure, taking into account these principles, they can develop a library.
  • In classes that we use frequently
  • We want to hide legacy codes or third-party library’s codes with Gateway pattern

Forms of using

We can create instances of classes that use in test automation applications in this way.

Class properties defined as readonly and are not set operation. We added separate methods for set operation and this method was named more understandable with regard to business domain. Then we make itself class type as return value of these method to create method-chaining as you can see in above

Let’s take a look:

We can use a process when we want to carry out any process step by step. For instance, you are developing a test automation application and you’ll need to run other platforms and environments tests in it. You’ll develop a class library for this. Here we should develop our code with fluent interface in this case. In fact, this library is code client and we should provide more readable classes it.

public class TestProcessorOutput
    public string ScreenTestOutputMessage { get; set; }
    public string StoredProcedureTestOutputMessage { get; set; }
    public string RunOutPutFileValidationTestOutputMessage { get; set; }
    public string RunInputFilevalidationTestOutputMessage { get; set; }

public interface ITestProcessor
    ITestProcessor RunScreenTests();
    ITestProcessor RunStoredProcedureTests();
    ITestProcessor RunOutPutFileValidationTests();
    ITestProcessor RunInputFilevalidationTests();
    TestProcessorOutput TakeResults();

public class TestProcessor
        : ITestProcessor
    public TestProcessorOutput TestOutput { get; private set; }
    public TestProcessor(TestProcessorOutput testOutput)
        this.TestOutput = testOutput;

    public ITestProcessor RunScreenTests()
        // testleri çalıştırır
        this.TestOutput.ScreenTestOutputMessage = "";
        return this;

    public ITestProcessor RunStoredProcedureTests()
        // testleri çalıştırır
        this.TestOutput.StoredProcedureTestOutputMessage = "";
        return this;

    public ITestProcessor RunOutPutFileValidationTests()
        // testleri çalıştırır
        this.TestOutput.RunOutPutFileValidationTestOutputMessage = "";
        return this;

    public ITestProcessor RunInputFilevalidationTests()
        // testleri çalıştırır
        this.TestOutput.RunInputFilevalidationTestOutputMessage = "";
        return this;

    public TestProcessorOutput TakeResults()
        return this.TestOutput;

public static class FluentTestFactory
    public static ITestProcessor Init()
        return new TestProcessor(new TestProcessorOutput());

// using of it
var testOutputs = FluentTestFactory

Let’s see class diagram of our sample:

Popular libraries that used fluent interface principle;  Moq, Fluent NHibernate, FluentData, Nbuilder, Automapper, StructureMap. You’ll see similar code samples in the below

As a result, Fluent Interface was used to create a better codebase and also it is the best way that you can write more readable code.

You can find help on this topic in the following links

It contains code samples in github about this topic. I would recommend review it.

Have a good coding

Published at DZone with permission of its author, Yunus Emre Keskin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)