SlideShare a Scribd company logo
Fluent Interface:
Hands On
Tomaš Maconko
https://github.com/tmaconko/FluentInterface
https://github.com/BalticAmadeus
SAD STORY
• Huge domain
• Legacy code
• Complex tests
• Mocks, fakes, stubs
3
SAD STORY
WHATS WRONG?
• Monstrous ARRANGE step
• Non-readable code
• Mocks
• And…
4
FIRST STEP: GRAB THE MAGIC STICK!
5
THE MAGIC HAPPENS: WE REFACTORED IT!
6
7
ALL THE SH*T WENT TO ANOTHER PLACE
BUT… WHAT NEXT?
• What will happen when create more and more stuff that depend on
unique data (e.g. ~3k tests)?
• What about specific parameters/cases?
8
COPY-PASTE
9
SECOND STEP: GRAB THE MAGIC STICK!
10
EXTRACT CLASSES AND SPLIT THE STUFF
11
12
THINK ABOUT PARAMETRIZATION
• PrepareOrderWithClient
• PrepareOrderWithClientWithoutAddress
• PrepareOrderWithClientWithoutShop
• PrepareOrder…
THINK ABOUT PARAMETRIZATION
13
THIRD STEP: APPLY DESIGN PATTERNS!
• Method chaining
• Builder
• Facade
• Internal DSL
14
METHOD CHAINING
15
BUILDER PATTERN
16
FACADE PATTERN
17
MYSTERIOUS THINGS
HAPPEN HERE
INTERNAL DSL
18
Note: Care about using the extensions. It is important to
track the usage scope.
RESULT: FLUENT INTERFACE
19
RESULT: FLUENT INTERFACE
20
LIKE NUnit
21
LIKE FluentValidation
22
LIKE FluentNHibernate
23
WHAT IS FLUENT INTERFACE?
• A flavor of API
• Used with Object Oriented Programming
• Enables better code readability and discoverability
• Simplifies usage of complex APIs
• Allows to express domain concepts more clearly
24
WHEN TO USE IT?
• To hide legacy code
• Third-party libraries, that may need
configuration
• Complex unit tests’ data arrangement
• When we want code to be more readable
25
STATE MUTATING
26
27
STATE PATCHING
28
EXAMPLES: BaContainer
Sources: http://github.com/BalticAmadeus
29
EXAMPLES: Fluent MDX
EXAMPLES: POEMS
30
CONS: REQUIRES ADDITIONAL EFFORT AND CODE
31
CONS: DEBUGGING BECOMES MORE COMPLICATED
32
33
CONS: BAD API CAN CAUSE DATA ERRORS
CONCLUSIONS
• Fluent API can be applied at any level
• Improves code readability
• May not follow OOP principles
• Can be used to restrict users to follow the rules of domain flow
• Requires developers to think from user perspective
34
SOURCES
• Domain-Specific Languages
– Martin Fowler. Addison-Wesley Professional, 1 edition, 2010.
• Fluent Interfaces
– Erik Schierboom. Link: http://www.erikschierboom.com/2014/10/08/fluent-interfaces/
• Fluent Interface for More Readable Code.
– Yunus Emre Keskin. Link: https://dzone.com/articles/fluent-interface-more-readable-0
• When To Build Fluent Interfaces for Re-Use and Clarity.
– Peter Vogel. Link: https://visualstudiomagazine.com/articles/2013/11/01/when-to-build-fluent-
interfaces-for-re-use-and-clarity.aspx
35
QUESTIONS?
36

More Related Content

PPTX
how_does_ddd_work
PPTX
Bullet: A Real Time Data Query Engine
PPTX
5 Popular Choices for NoSQL on a Microsoft Platform - Tulsa - July 2018
PPTX
5 Popular Choices for NoSQL on a Microsoft Platform
PPTX
5 Popular Choices for NoSQL on a Microsoft Platform - All Things Open - Octob...
PDF
Microservices pros and cons - houston tech fest
PDF
Modern software architectures - PHP UK Conference 2015
PPTX
Intro to Big Data and NoSQL
how_does_ddd_work
Bullet: A Real Time Data Query Engine
5 Popular Choices for NoSQL on a Microsoft Platform - Tulsa - July 2018
5 Popular Choices for NoSQL on a Microsoft Platform
5 Popular Choices for NoSQL on a Microsoft Platform - All Things Open - Octob...
Microservices pros and cons - houston tech fest
Modern software architectures - PHP UK Conference 2015
Intro to Big Data and NoSQL

Similar to FluentInterfaceHandsOn (20)

PPTX
Microservices pros and cons dark
PPT
assem.ppt
PPTX
The world is not black and white – Impact of decisions over the lifetime of a...
PDF
Why and How to use Onion Networking - #EMFCamp2018
PPTX
5 NoSQL Options - Toronto - May 2018
PPTX
Writing code for others
PDF
The Open Chemistry Project
PPTX
Old code doesn't stink
PDF
Clean Code V2
PDF
Ch 18: Source Code Auditing
PDF
Behat bdd training (php) course slides pdf
PPTX
Clean code, Feb 2012
PPTX
Adopting OpenTelemetry
PPTX
lecture03_EmbeddedSoftware for Beginners
PPTX
Art of refactoring - Code Smells and Microservices Antipatterns
PDF
Sullivan white boxcrypto-baythreat-2013
PDF
Big Data! Great! Now What? #SymfonyCon 2014
PDF
RESTful web
PDF
What is octohost?
PPTX
ODSC East 2017 - Reproducible Research at Scale with Apache Zeppelin and Spark
Microservices pros and cons dark
assem.ppt
The world is not black and white – Impact of decisions over the lifetime of a...
Why and How to use Onion Networking - #EMFCamp2018
5 NoSQL Options - Toronto - May 2018
Writing code for others
The Open Chemistry Project
Old code doesn't stink
Clean Code V2
Ch 18: Source Code Auditing
Behat bdd training (php) course slides pdf
Clean code, Feb 2012
Adopting OpenTelemetry
lecture03_EmbeddedSoftware for Beginners
Art of refactoring - Code Smells and Microservices Antipatterns
Sullivan white boxcrypto-baythreat-2013
Big Data! Great! Now What? #SymfonyCon 2014
RESTful web
What is octohost?
ODSC East 2017 - Reproducible Research at Scale with Apache Zeppelin and Spark
Ad

FluentInterfaceHandsOn