the fluent interface pattern
DESCRIPTION
TRANSCRIPT
![Page 1: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/1.jpg)
Lars-Erik KindbladSenior ConsultantBlog: kindblad.com
The Fluent Interface Pattern
![Page 2: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/2.jpg)
| Sector, Alliance, Offering
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
![Page 3: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/3.jpg)
| Sector, Alliance, Offering
Feature delivery for most projects
Time
Feat
ures
Del
iver
ed
Technical debt
Complex and messy codeCode and logic is duplicatedNo common coding standard
No strict architecture
++
![Page 4: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/4.jpg)
| Sector, Alliance, Offering
Ideal Feature Delivery
Time
Feat
ures
Del
iver
ed
![Page 5: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/5.jpg)
| 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 problemso 100s of design patterns exists
– Factory Pattern, MVC Pattern, Command Pattern, Fluent Interface Pattern etc.
• ++
![Page 6: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/6.jpg)
| 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.
![Page 7: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/7.jpg)
| Sector, Alliance, Offering
Fluent Interface Code:
Example – Find a person
Traditional Code:
Or:
![Page 8: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/8.jpg)
| Sector, Alliance, Offering
A more readable Fluent Interface
Or:
![Page 9: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/9.jpg)
| Sector, Alliance, Offering
Designing Fluent Interfaces - Method vs. Property
Use a method when having toaccept one or more values
Use a property when not havingto accept any values
Use a method to finish the call
![Page 10: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/10.jpg)
| Sector, Alliance, Offering
Traditional Code
![Page 11: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/11.jpg)
| Sector, Alliance, Offering
Fluent Interface Code
![Page 12: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/12.jpg)
| Sector, Alliance, Offering
Example #2 – A List of Persons
Traditional Code:
Fluent Code:
![Page 13: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/13.jpg)
| Sector, Alliance, Offering
Traditional Code
![Page 14: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/14.jpg)
| Sector, Alliance, Offering
Fluent Interface Step 1: Add Gateway Pattern
![Page 15: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/15.jpg)
| Sector, Alliance, Offering
Step 2: Add Fluent Interface Pattern
![Page 16: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/16.jpg)
| Sector, Alliance, Offering
Examples from other Frameworks
Moq:
FluentData:
![Page 17: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/17.jpg)
| 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
![Page 18: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/18.jpg)
| Sector, Alliance, Offering
QUESTIONS?
![Page 19: The Fluent Interface Pattern](https://reader033.vdocuments.net/reader033/viewer/2022061214/54993f36b479597c128b45dc/html5/thumbnails/19.jpg)
www.capgemini.com
The information contained in this presentation is proprietary. ©2010 Capgemini. All rights reserved