Fluent Interface0 Term coined by Eric Evens and Martin Fowler http://martinfowler.com/bliki/FluentInterface.html0 Implementation of an object oriented API that aims to provide for more readable code.0 Fluent Interface != Method Chaining
Why should I care?0 Discoverable – easy to use0 Create readable, maintainable code0 Usage directions for users/teammates0 Easy to implement0 No additional libraries needed
Extension Methods0 Quick and easy0 Can be used on 3rd party libraries0 Keep the old API
Method chaining“Make modifier methods return the hostobject, so that multiple modifiers can beinvoked in a single expression”0 Context defined by return value of a called method0 terminated through the return of a void context.
Expression builder“An object, or family of objects, that provide afluent interface over a normal command-queryAPI”var cow = new Animal().Name(“Cow”).Legs(4).Color(Color.Red).Create();
Method chaining in practice0 Use different interfaces to set “stages”0 Use Generics to change return type0 The end of expression problem
Point of entry considerationNUnit:Assert.That (2 + 2, Is.EqualTo(4));Fluent Assertions:(2 + 2).Should().Be(4);
Nested function0 If you need a new instance - use constructors0 Use lambdas for custom operations0 Use expression