2. Domain Specific Languages
0 Created specifically to solve problems in a particular
domain
0 Layer of abstraction on top of an existing domain model
0 Can be used by domain experts
4. Fluent Interface
0 Term coined by Eric Evens and Martin Fowler
http://martinfowler.com/bliki/FluentInterface.html
0 Implementation of an object oriented API that aims
to provide for more readable code.
0 Fluent Interface != Method Chaining
5. Why should I care?
0 Discoverable – easy to use
0 Create readable, maintainable code
0 Usage directions for users/teammates
0 Easy to implement
0 No additional libraries needed
7. Extension Methods - Example
var cheapItmes = Items.Where(item => item.Price < 100)
.Select(item => item.Name);
public static IEnumerable<TSource> Where(
this IEnumerable<TSource> source, Func<TSource, bool>
predicate)
public static IEnumerable<TResult> Select <TSource, TResult>
(this IEnumerable<TSource> source, Func<TSource,
TResult> selector)
8. Method chaining
“Make modifier methods return the host
object, so that multiple modifiers can be
invoked in a single expression”
0 Context defined by return value of a called method
0 terminated through the return of a void context.
9. Expression builder
“An object, or family of objects, that provide a
fluent interface over a normal command-query
API”
var cow =
new Animal().Name(“Cow”).Legs(4).Color(Color.Red).Create();
10. Method chaining in practice
0 Use different interfaces to set “stages”
0 Use Generics to change return type
0 The end of expression problem
11. Point of entry consideration
NUnit:
Assert.That (2 + 2, Is.EqualTo(4));
Fluent Assertions:
(2 + 2).Should().Be(4);
12. Nested function
0 If you need a new instance - use constructors
0 Use lambdas for custom operations
0 Use expression