The Fluent Interface Pattern

Lars-Erik Kindblad
Senior Consultant
Blog: kindblad.com
A common problem in most software projects

 Technical debts increases over time and the code gets harder and
  harder to maintain and add features to




                                                          | Sector, Alliance, Offering
Feature delivery for most projects

                              Technical debt


                                                Complex and messy code
                                               Code and logic is duplicated
                                               No common coding standard
Features Delivered




                                                   No strict architecture
                                                           ++




                                       Time




                                                                              | Sector, Alliance, Offering
Ideal Feature Delivery
Features Delivered




                               Time




                                              | Sector, Alliance, Offering
How to reach ideal delivery?
 Software development is complex
 Not a single solution but a mix of many different things
  •   Write a lot of code
  •   Important to understand the business model
  •   Readable & maintainable code over fancy code
  •   Simplicity over complexity
  •   Decide on a common coding standard
  •   Reuse code instead of code duplication
  •   Create a project framework with common code and logic
  •   Design Patterns – follow generic solutions to commonly occurring problems
      o 100s of design patterns exists
         – Factory Pattern, MVC Pattern, Command Pattern, Fluent Interface Pattern etc.
  • ++

                                                                                     | Sector, Alliance, Offering
The Fluent Interface Pattern

 One of many design patterns that can contribute to a better code
  base
 Fluent Interfaces makes the code easier to read and easier to use
  due to a simpler API that requires less code
 Consists of 2 principles:
  • Method chaining - Traditional code requires one line per command, method
    chaining allows multiple commands per line
  • More readable API – The code can be read like a sentence. Optional as it
    requires more typing due to longer names and more code to write
 Popular to use in frameworks: Moq, Fluent Nhibernate, FluentData
  etc.


                                                                    | Sector, Alliance, Offering
Example – Find a person
Traditional Code:




Fluent Interface Code:



Or:




                                                   | Sector, Alliance, Offering
A more readable Fluent Interface




Or:




                                         | Sector, Alliance, Offering
Designing Fluent Interfaces - Method vs. Property



                               Use a method when having to
                               accept one or more values


                               Use a property when not having
                               to accept any values


                               Use a method to finish the call




                                                      | Sector, Alliance, Offering
Traditional Code




                   | Sector, Alliance, Offering
Fluent Interface Code




                        | Sector, Alliance, Offering
Example #2 – A List of Persons
Traditional Code:




Fluent Code:




                                                     | Sector, Alliance, Offering
Traditional Code




                   | Sector, Alliance, Offering
Fluent Interface Step 1: Add Gateway Pattern




                                               | Sector, Alliance, Offering
Step 2: Add Fluent Interface Pattern




                                       | Sector, Alliance, Offering
Examples from other Frameworks
Moq:




FluentData:




                                               | Sector, Alliance, Offering
When to use Fluent Interfaces

 Anywhere where it makes the code easier to read and easier to use
 Typically in methods that are often invoked
 Together with the Gateway Pattern to hide complex 3rd party code or
  legacy code
 Frameworks




                                                          | Sector, Alliance, Offering
QUESTIONS?

             | Sector, Alliance, Offering
www.capgemini.com



The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved

The Fluent Interface Pattern

  • 1.
    The Fluent InterfacePattern Lars-Erik Kindblad Senior Consultant Blog: kindblad.com
  • 2.
    A common problemin most software projects  Technical debts increases over time and the code gets harder and harder to maintain and add features to | Sector, Alliance, Offering
  • 3.
    Feature delivery formost projects Technical debt Complex and messy code Code and logic is duplicated No common coding standard Features Delivered No strict architecture ++ Time | Sector, Alliance, Offering
  • 4.
    Ideal Feature Delivery FeaturesDelivered Time | Sector, Alliance, Offering
  • 5.
    How to reachideal delivery?  Software development is complex  Not a single solution but a mix of many different things • Write a lot of code • Important to understand the business model • Readable & maintainable code over fancy code • Simplicity over complexity • Decide on a common coding standard • Reuse code instead of code duplication • Create a project framework with common code and logic • Design Patterns – follow generic solutions to commonly occurring problems o 100s of design patterns exists – Factory Pattern, MVC Pattern, Command Pattern, Fluent Interface Pattern etc. • ++ | Sector, Alliance, Offering
  • 6.
    The Fluent InterfacePattern  One of many design patterns that can contribute to a better code base  Fluent Interfaces makes the code easier to read and easier to use due to a simpler API that requires less code  Consists of 2 principles: • Method chaining - Traditional code requires one line per command, method chaining allows multiple commands per line • More readable API – The code can be read like a sentence. Optional as it requires more typing due to longer names and more code to write  Popular to use in frameworks: Moq, Fluent Nhibernate, FluentData etc. | Sector, Alliance, Offering
  • 7.
    Example – Finda person Traditional Code: Fluent Interface Code: Or: | Sector, Alliance, Offering
  • 8.
    A more readableFluent Interface Or: | Sector, Alliance, Offering
  • 9.
    Designing Fluent Interfaces- Method vs. Property Use a method when having to accept one or more values Use a property when not having to accept any values Use a method to finish the call | Sector, Alliance, Offering
  • 10.
    Traditional Code | Sector, Alliance, Offering
  • 11.
    Fluent Interface Code | Sector, Alliance, Offering
  • 12.
    Example #2 –A List of Persons Traditional Code: Fluent Code: | Sector, Alliance, Offering
  • 13.
    Traditional Code | Sector, Alliance, Offering
  • 14.
    Fluent Interface Step1: Add Gateway Pattern | Sector, Alliance, Offering
  • 15.
    Step 2: AddFluent Interface Pattern | Sector, Alliance, Offering
  • 16.
    Examples from otherFrameworks Moq: FluentData: | Sector, Alliance, Offering
  • 17.
    When to useFluent Interfaces  Anywhere where it makes the code easier to read and easier to use  Typically in methods that are often invoked  Together with the Gateway Pattern to hide complex 3rd party code or legacy code  Frameworks | Sector, Alliance, Offering
  • 18.
    QUESTIONS? | Sector, Alliance, Offering
  • 19.
    www.capgemini.com The information containedin this presentation is proprietary. ©2010 Capgemini. All rights reserved