Over a million developers have joined DZone.

Fluent Interface for More Readable Code

DZone's Guide to

Fluent Interface for More Readable Code

· DevOps Zone ·
Free Resource

Learn how integrating security into DevOps to deliver "DevSecOps" requires changing mindsets, processes and technology.

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

Learn how enterprises are using tools to automate security in their DevOps toolchain with these DevSecOps Reference Architectures.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}