I really don't get all the excitement about Fluent Interfaces.
Glenn pointed to a post on
Fluent Refactoring. He takes some code that looks like this:
Owner owner = GetAccountOwner();
Lookup to = new Lookup(EntityName.queue, Settings.Default.ControlQueueID);
Lookup from = new Lookup(EntityName.systemuser, owner.Value);
Guid regarding = PostState.new_parentpolicy.Value;
Picklist priority = PickLists.EmailPriority.Type.High;
SendMail(from, to, title, body, owner, regarding, priority);
And refactors the api to look like this:
Owner owner = GetAccountOwner(); // not sure why this line is here
Email.Owner(GetAccountOwner())
.From(EntityName.systemuser, owner.Value)
.To(EntityName.queue, Settings.Default.ControlQueueID)
.Regarding(PostState.new_parentpolicy.Value)
.Priority(PickLists.EmailPriority.Type.High)
.Title(title)
.Body(body)
.Send();
I'm sure they have a place but people just seem to see opportunity for a fluent interface everywhere they look at the moment. I also have a problem with the readability of some fluent interfaces. For example, does the .Title method change a property on the Priority object or does it change the base e-mail? Intuitively, because I understand e-mails it's more likely to be the latter but it isn't totally clear.
I figure your Email object would have to have properties as well so why go to the trouble of mapping all this to a fluent interface?
What's wrong with doing it the OO way? Have I missed something fundamental here?
Mail mail = new Mail();
mail.Owner = GetAccountOwner();
mail.From = new Lookup(EntityName.queue, Settings.Default.ControlQueueID);
mail.To = new Lookup(EntityName.systemuser, owner.Value);
mail.Regarding = PostState.new_parentpolicy.Value;
mail.Priority = PickLists.EmailPriority.Type.High;
mail.Title = title;
mail.Body = body;
mail.Send();
Better still, you could make sure you take any mandatory parameters in the constuctor so you're guaranteed to have an object in a valid state (as we discussed in this post:
Initialisation using constructors).

Post By
Josh Twist
01:23
24 Sep 2007
» Next Post:
Streamlining build and deploy for sharepoint workflows
« Previous Post:
Exporting VSTS load test results to a new database with PowerShell
Comments are closed for this post.
Posted by
Inferis
@
24 Sep 2007
02:16
It sort of reminds me of the mess TurboVision was. Not exactly the same, but I didn't like it anyway.