Talk:Fluent interface
Computer science Stub‑class | |||||||||||||||||
|
Hello!
1) I tried the c#-Sample, but the compiler alerted 12 Syntax-Errors. Please give a sample with runnable code.
2) The "fluent-interface-benefit" (short intuitive code) is not demonstrated very convincing. I would prefer to see something like:
IConfiguration config = ConfigurationFluent.Create().Color("blue").Height(1).Length(2).Depth(3);
or
IConfiguration config = ConfigurationFluent.CreateColored("blue").Height(1).Length(2).Depth(3);
(Create() or CreateColored() would be static methods, returning a new instance as an "entry-Point" into the fluent interface.
That is no really good Sample too, because c#2008 provides the with-Keyword, so one could instantiate without fluent interface as well:
var config = new Configuration() with { Color = "blue", Height = 1, Length = 2, Depth = 3 };
3) A better sample would result in stuff like:
// List<SpecialItem> _SpecialItems = new List<SpecialItem>();
SpecialItem.CreateColored("blue").Height(1).Length(2).Depth(3).AddTo(_SpecialItems);
That would demonstrate, how fluent interface reduce nesting. Compare to:
// List<SpecialItem> _SpecialItems = new List<SpecialItem>();
_SpecialItems.Add(new SpecialItem() with { Color = "blue", Height = 1, Length = 2, Depth = 3 });
Unfortunately the benefit "help from Intellisense" (a fluent interface supports fluent writing code) cannot be shown in an article, or would you like to add screenshots?
ErfinderDesRades (talk) 11:11, 15 December 2008 (UTC)
IMO the published sample is garbage. Chaining in itself does not make for readability and the ALT.NET community is severely abusing mere chaining with this misunderstanding. There's nothing less readable about
myObject.SetValueX(1);
myObject.SetValueY(2);
than
myObject.SetValueX(1).SetValueY(2);
In my opinion, this actually makes it less readable because these are distinct statements that are being slurred together.
I agree that the samples from ErfinderDesRades are better. Jon (talk) 09:52, 30 September 2009 (UTC)