Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
---
layout: post
title: Internal DSLs, method chaining and discoverability
date: '2009-05-31T15:46:00.001+01:00'
tags: [design-patterns]
modified_time: '2009-05-31T15:46:34.830+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-6232867212464561365
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2009/05/internal-dsls-method-chaining-and.html
---
<p>Paul <a href="http://twitter.com/dagda1/statuses/1980507228">says</a> “The whole .NET space has gone fluent interface crazy”, and he is quite right. Everybody has their own fluent interfaces, and unless I’m missing the bigger picture, most of them seem to be about productivity over discoverability. The intent is clear, write more intent-revealing code in less time.</p> <p>More often than not however, you multiply the entry points needed to be known by developers. Let’s take an example with the criteria API in nHibernate.</p> <div class="container"> <div id="content"> <div id="mainbar"> <div id="question"> <table><tbody> <tr> <td> <div> <div class="post-text"> <pre class="prettyprint" jquery1243779122666="11"><code><span class="kwd">return</span><span class="pln"> </span><span class="typ">Session</span><span class="pun">.</span><span class="typ">CreateCriteria</span><span class="pun">(</span><span class="kwd">typeof</span><span class="pun">(</span><span class="typ">FundingCategory</span><span class="pun">),</span><span class="pln"> </span><span class="str">&quot;fc&quot;</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">CreateCriteria</span><span class="pun">(</span><span class="str">&quot;FundingPrograms&quot;</span><span class="pun">,</span><span class="pln"> </span><span class="str">&quot;fp&quot;</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">CreateCriteria</span><span class="pun">(</span><span class="str">&quot;Projects&quot;</span><span class="pun">,</span><span class="pln"> </span><span class="str">&quot;p&quot;</span><span class="pun">,</span><span class="pln"> </span><span class="typ">JoinType</span><span class="pun">.</span><span class="typ">LeftOuterJoin</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ"><font color="#ff0000"><strong>Restrictions</strong></font></span><span class="pun">.</span><span class="typ">Disjunction</span><span class="pun">()</span><span class="pln"><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ">Restrictions</span><span class="pun">.</span><span class="typ">Eq</span><span class="pun">(</span><span class="str">&quot;fp.Recipient.Id&quot;</span><span class="pun">,</span><span class="pln"> recipientId</span><span class="pun">))</span><span class="pln"><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ">Restrictions</span><span class="pun">.</span><span class="typ">Eq</span><span class="pun">(</span><span class="str">&quot;p.Recipient.Id&quot;</span><span class="pun">,</span><span class="pln"> recipientId</span><span class="pun">))</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">SetProjection</span><span class="pun">(</span><span class="typ">Projections</span><span class="pun">.</span><span class="typ">ProjectionList</span><span class="pun">()</span><span class="pln"><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ"><strong><font color="#ff0000">Projections</font></strong></span><span class="pun">.</span><span class="typ">GroupProperty</span><span class="pun">(</span><span class="str">&quot;fc.Name&quot;</span><span class="pun">),</span><span class="pln"> </span><span class="str">&quot;fcn&quot;</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ">Projections</span><span class="pun">.</span><span class="typ">Sum</span><span class="pun">(</span><span class="str">&quot;fp.ObligatedAmount&quot;</span><span class="pun">),</span><span class="pln"> </span><span class="str">&quot;fpo&quot;</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="typ">Projections</span><span class="pun">.</span><span class="typ">Sum</span><span class="pun">(</span><span class="str">&quot;p.ObligatedAmount&quot;</span><span class="pun">),</span><span class="pln"> </span><span class="str">&quot;po&quot;</span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">)</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">AddOrder</span><span class="pun">(</span><span class="typ"><strong><font color="#ff0000">Order</font></strong></span><span class="pun">.</span><span class="typ">Desc</span><span class="pun">(</span><span class="str">&quot;fpo&quot;</span><span class="pun">))</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">AddOrder</span><span class="pun">(</span><span class="typ">Order</span><span class="pun">.</span><span class="typ">Desc</span><span class="pun">(</span><span class="str">&quot;po&quot;</span><span class="pun">))</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">AddOrder</span><span class="pun">(</span><span class="typ">Order</span><span class="pun">.</span><span class="typ">Asc</span><span class="pun">(</span><span class="str">&quot;fcn&quot;</span><span class="pun">))</span><span class="pln"><br />&#160;&#160;&#160; </span><span class="pun">.</span><span class="typ">List</span><span class="pun">&lt;</span><span class="kwd">object</span><span class="pun">[]&gt;();</span><span class="pln"><br /></span></code></pre>
</div>
</div>
</td>
</tr>
</tbody></table>
<em>[nitpicker corner: no one ever said the criteria api was a fluent api, it’s at most method chaining.]</em></div>
</div>
</div>
</div>
<p>I’ve highlighted in red those entry points. Each of those method usually takes an instance of an interface. A static method is then used to create those objects. What’s the issue with that?</p>
<p>It all comes down to discoverability. One of the biggest issue I’ve always had with the criteria API is the low discoverability of those types. Take the <em>ICriterion</em> interface, for which the <em>Expression</em> class provides the simple criterions. The only way, once in a method, for me to know that where it expects ICriterion I could use Expression is by going to the documentation. This is for me a massive discoverability failure.</p>
<p>The second issue comes from the latest lambda-based APIs. Let’s have a bit of code from <a href="http://codebetter.com/blogs/dru.sellers/archive/2009/05/29/dropkick-an-idea-for-deployments.aspx">the original blog post from Dru that Paul was referring to</a>.</p>
<form id="aspnetForm" method="post" name="aspnetForm" action="http://codebetter.com/default.aspx">
<div class="Common">
<div id="CommonSidebarRight">
<div id="CommonContent">
<div id="CommonContentInner">
<div class="CommonContentArea">
<div class="CommonContent">
<div class="BlogPostList">
<div class="BlogPost">
<div class="BlogPostContent">
<div class="dp-highlighter">
<ol class="dp-c">
<li class="alt"><font face="Consolas"><span>&#160;</span><span class="keyword">static</span><span> TestDeployment()&#160;&#160; </span></span></font></li>
<li><span><font face="Consolas">&#160;&#160;&#160; {&#160;&#160; </font></span></li>
<li class="alt"><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Define(() =&gt;&#160;&#160; </font></span></li>
<li><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160; </font></span></li>
<li class="alt"><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; During(Web, (p) =&gt;&#160;&#160; </font></span></li>
<li><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {&#160;&#160; </font></span></li>
<li class="alt"><font face="Consolas"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; p.OnServer(</span><span class="string">&quot;WebServer&quot;</span><span>)&#160;&#160; </span></span></font></li>
<li><font face="Consolas"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .IisSite(</span><span class="string">&quot;Apps&quot;</span><span>)&#160;&#160; </span></span></font></li>
<li class="alt"><font face="Consolas"><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .VirtualDirectory(</span><span class="string">&quot;dashboard&quot;</span><span>)&#160;&#160; </span></span></font></li>
<li><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Verify()&#160;&#160; </font></span></li>
<li class="alt"><span><font face="Consolas">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CreateIfItDoesntExist();</font>&#160;&#160; </span></li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<p>I’ve not downloaded the code to have a play, but I assume that Define and During are actually properties on the base type. On a positive note, this does help with discoverability, at the cost of enforcing an inheritance hierarchy.</p>
<p>So why do I feel awkward with this syntax? The nesting of lambdas. The multiplication of p, x and other one-letter prefix make the code much less readable. The pen*s operator is not exactly my favourite addition to the C# compiler.</p>
<p>Here’s my checklist for what constitutes a nice usable fluent API. <em>[nitpicker corner: OpenRasta doesn’t do it that way everywhere. You didn’t say the same thing six months ago].</em></p>
<ul>
<li>No lambda-based chaining</li>
<li>No multi-line lambda expressions</li>
<li>At most one or two static class entry-points to discover</li>
<li>Method parameters should only be primitive types</li>
</ul>
<p>There is a way to make a fluent API while respecting those points, but I believe the result would be better. So here’s Dru’s example rewritten using those guidelines.</p>
<p><font face="Consolas">using(Definition)
<br />{
<br />&#160;&#160;&#160; During.Web
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Server(&quot;WebServer&quot;)
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Has.IisSite(&quot;Apps&quot;)
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .VirtualDirectory(&quot;dashboard&quot;)
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Verify()
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CreateIfItDoesntExist()
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .And
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Server(&quot;SvrTopeka19&quot;)
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .Has.Msmsq()
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .PrivateQueueNamed(&quot;mt_subscriptions&quot;)
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CreateIfItDoesntExist()
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .And
<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; .CopyFrom(@&quot;.\code_drop\dashboard\**\*.*&quot;).To(@&quot;\\webserver\apps\dashboard\&quot;);
<br />}</font></p>
<p>The main difference here is the introduction of scoping operators. The Server method returns two properties, And which closes the current Server scope, and Had Has which open a new service-specific scope.</p>
<p>I find this kind of API much more readable, and they introduce less syntax noise (aka no lambda operators or blocks). There is also no need to introduce external points of references, be them be properties or static classes, except for the original entry point. Arguably, they’re harder to write, but I think the effort is worth the benefits.</p>