Beginner - Fluent Interfaces
Let us define a simple fluent interface to allow smart method chaining, continued from Method Chaining
So, let’s continue from our last post method chaining with C#!
To recap, Jim ๐ง found a cleaner, more efficient method of building his sandwich, however he’s discovered there should be a specific order to the ingredients.
With method chaining we can’t enforce a specific order of methods called. Let’s illustrate this problem.
class SandwichFactory
{
public SandwichFactory AddBread()
{
// implementation omitted for brevity
return this;
}
public SandwichFactory AddCheese()
{
// implementation omitted for brevity
return this;
}
}
We, as sandwich construction experts, want to enforce the following pattern
- ๐ Bread
- ๐ง Cheese
- ๐ Bread
However, a consumers of our SandwichFactory
class could do the following:
var createSandwich = new SandwichFactory()
.AddCheese()
.AddBread();
Or something worse! Let’s fix this problem.
Coming up with a nice fluent API requires a good bit of thought - Martin Fowler
Depending on the API we want to provide, complexity can rapidly escalate. For our example, we’re going to keep this small to illustrate the concepts more than application. Lets provide our solution below and go through the reasoning behind decisions made in regards to fluent interfaces:
public sealed class SandwichFactory : ISandwichBread, ISandwichCheese
{
private SandwichFactory()
{
}
public static ISandwichBread Create => new SandwichFactory();
public ISandwichCheese AddBread()
{
throw new NotImplementedException();
}
public ISandwichBread AddCheese()
{
throw new NotImplementedException();
}
}
public interface ISandwichBread
{
ISandwichCheese AddBread();
}
public interface ISandwichCheese
{
ISandwichBread AddCheese();
}
- The constructor needs to be private - We don’t want a direct instance of our class to be created, since this would provide access to the
AddBread
andAddCheese
methods (which is exactly what we’re trying to avoid)! - Static Entry point - our entry point, in this case
Create
should return the interface we want to start with which isISandwichBread
and an instance of our class - Keep parameters low - we don’t want to use tons of parameters per method, in our example we’re using 0, but it’s something to be mindful of
Consuming the SandwichFactory
API, we now can’t add cheese ๐ง before bread ๐, since our design enforces a bread > cheese hierarchy.
SandwichFactory.Create
.AddBread()
.AddCheese()
.AddBread();
Simples! ๐ช