When comparing to classic properties, what's the big gain of using it ?
I know the repeating of the instance name is gone, but that's all ?
public class PropClass
{
public Object1 object1 { get; set; }
public Object2 object2 { get; set; }
}
PropClass propClass = new PropClass();
propClass.object1 = o1;
propClass.object2 = o2;
public class FluentClass
{
public Object1 object1 { get; private set; }
public Object2 object2 { get; private set; }
public FluentClass SetObject1(Object1 o1)
{
object1 = o1;
return this;
}
public FluentClass SetObject2(Object1 o2)
{
object1 = o2;
return this;
}
}
FluentClass fluentClass = new FluentClass().SetObject1(o1).SetObject1(o2);
IMHO there's no big gain of setting properties with fluent interface, especially with C# 3.0 class initializers. Fluent interfaces become more interesting when you start chaining methods and operations.
It depends on how it's used. In your example, there's not much point in using a fluent interface.
On the other hand, fluent interface works really well for things like builders, especially when you chain multiple fluent builders together (e.g. car builder / engine builder). I've used Test Data Builders quite extensively and they work really well. You can do the same thing without a fluent interface, but it's not so nice to use.
Furthermore, there is the Domain Specific Language angle that Martin Fowler explains here.
The only problem is that people sometimes go a bit crazy with fluent interfaces and create overly verbose APIs, but that's less of a fluent interface problem and more of an application/implementation problem, in my opinion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With