<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Holsee&#039;s Blog &#187; DSL</title>
	<atom:link href="http://www.blog.holsee.com/tag/dsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.holsee.com</link>
	<description>Adventures in Entrepreneurship, Code, Coffee and Photography..</description>
	<lastBuildDate>Fri, 02 Jul 2010 10:48:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An Introduction to Fluent Interfaces &amp; Internal DSLs in C#</title>
		<link>http://www.blog.holsee.com/2009/10/fluent-interfaces-internal-domain-specific-languages/</link>
		<comments>http://www.blog.holsee.com/2009/10/fluent-interfaces-internal-domain-specific-languages/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 13:41:55 +0000</pubDate>
		<dc:creator>holsee</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DSL]]></category>
		<category><![CDATA[Fluent Interface]]></category>

		<guid isPermaLink="false">http://www.blog.holsee.com/2009/10/fluent-interfaces-internal-domain-specific-languages/</guid>
		<description><![CDATA[I like to learn to write code by writing software, I like to learn about writing software by writing about it.&#160; My goal is to build a series of posts which will reflect the knowledge I have gained regarding this topic of Fluent Interfaces and Internal DSLs.
Disclaimer: Authorship is not equal to Authority.&#160; I don’t [...]]]></description>
			<content:encoded><![CDATA[<p>I like to learn to write code by writing software, I like to learn about writing software by writing about it.&#160; My goal is to build a series of posts which will reflect the knowledge I have gained regarding this topic of Fluent Interfaces and Internal DSLs.</p>
<p><strong>Disclaimer</strong>: Authorship is <strong>not</strong> equal to Authority.&#160; I don’t claim to be an expert in this domain.&#160; I’m merely learning by doing so take what I write with a pinch of salt!</p>
<p><strong>Fluent Interfaces &amp; Fluent APIs</strong></p>
<p>A fluent interface to me is a the way you interact with a class in a fluent fashion.&#160; A fluent API is how you interact with a framework of classes in a fluent fashion.&#160; In all reality the terms are interchangeable with one another.</p>
<p>Over the last month I have been thinking a lot about fluent interface design in C# to create a DSL for an “Fluent” API I will be working on.&#160; I am starting to see more and more “Fluent” APIs emerging in the .NET space.</p>
<p>I am currently researching quite heavily in this area and I will be blogging about the different approaches that can be taken to develop an effective Fluent API.&#160; I want to know first hand how to create structured and compiler controlled sentences, the positives and negatives of doing so by experiencing the process of developing and developing with my own Fluent API.</p>
<p><strong>Definitions</strong></p>
<blockquote><p>“In software engineering, a <strong>fluent interface</strong> (as first coined by <a href="http://en.wikipedia.org/w/index.php?title=Eric_Evans_%28technologist%29&amp;action=edit&amp;redlink=1">Eric Evans</a> and <a href="http://en.wikipedia.org/wiki/Martin_Fowler">Martin Fowler</a>) is a way of implementing an object oriented API in a way that aims to provide for more readable code.&#160; A fluent interface is normally implemented by using method chaining to relay the instruction context of a subsequent call (but a fluent interface entails more than just method chaining).” Wikipedia: Fluent Interface</p>
</blockquote>
<blockquote><p>“Typically, method chaining simply consists of many methods on a class, each of which return the current object itself. This enables invoking another method on the same object immediately, leading to code that may look something like:” Wikipedia: Method Chaining</p>
</blockquote>
<p><strong>Simple Fluent Interface</strong></p>
<p>This is a simple example, my goal is to elaborate into more complex scenarios in later posts.</p>
<p>Here is an example of a fluent interface for creating a Coffee, it could be said we are creating a ‘internal’ DSL&#160; for creating coffee.</p>
<p>The first thing to note is that all public methods return ‘this’ in turn allowing us to chain methods together.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CoffeeItem <span style="color: #008000;">:</span> ICoffeeItem
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> HasMilk <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> HasCream <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> CoffeeSize Size <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeBlend Blend <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeItem SetSizeTo<span style="color: #000000;">&#40;</span>CoffeeSize size<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Size <span style="color: #008000;">=</span> size<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeItem WithCream<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        HasCream <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeItem WithMilk<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        HasMilk <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeItem SetBlendTo<span style="color: #000000;">&#40;</span>ICoffeeBlend blend<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Blend <span style="color: #008000;">=</span> blend<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> this<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
  <br />Lets create an order and test that it works with a unit test.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> CoffeeItem_OrderAJavaSumutraWithCreamOfSizeEpic_ShouldContainSaidAttributes<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var coffee <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CoffeeItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">//Set Order</span>
    coffee.<span style="color: #0000FF;">SetBlendTo</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> JavaSumutra<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithCream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">SetSizeTo</span><span style="color: #000000;">&#40;</span>CoffeeSize.<span style="color: #0000FF;">Epic</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">//Assert that order was issued correctly</span>
    Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>coffee.<span style="color: #0000FF;">HasCream</span>, <span style="color: #008000;">Is</span>.<span style="color: #0600FF;">True</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>coffee.<span style="color: #0000FF;">Blend</span>, <span style="color: #008000;">Is</span>.<span style="color: #008000;">TypeOf</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>JavaSumutra<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>coffee.<span style="color: #0000FF;">Size</span>, <span style="color: #008000;">Is</span>.<span style="color: #0000FF;">EqualTo</span><span style="color: #000000;">&#40;</span>CoffeeSize.<span style="color: #0000FF;">Epic</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
  <br />I find this a nice way to write software, and it lends well to test driven development.&#160; Now lets create a Coffee Order which will contain many coffees.</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CoffeeOrder
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> CoffeeOrder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        CoffeeItems <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> List CoffeeItems <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> ICoffeeItem AddCoffee<span style="color: #000000;">&#40;</span>ICoffeeItem coffee<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        CoffeeItems.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>coffee<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> coffee<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
  <br />So now we can create a coffee order which contains the a coffee with its attributes being set in a a sentence.&#160; We can run this inside a unit test like so:</p>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddCoffee_WhenPassedACoffee_ShouldContainCoffee<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var order <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CoffeeOrder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var coffee <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CoffeeItem<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    order.<span style="color: #0000FF;">AddCoffee</span><span style="color: #000000;">&#40;</span>coffee<span style="color: #000000;">&#41;</span>
        .<span style="color: #0000FF;">SetBlendTo</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> JavaSumutra<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        .<span style="color: #0000FF;">WithCream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        .<span style="color: #0000FF;">SetSizeTo</span><span style="color: #000000;">&#40;</span>CoffeeSize.<span style="color: #0000FF;">Small</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Assert.<span style="color: #0000FF;">That</span><span style="color: #000000;">&#40;</span>order.<span style="color: #0000FF;">CoffeeItems</span>, Has.<span style="color: #0000FF;">Member</span><span style="color: #000000;">&#40;</span>coffee<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So that concludes the simple Coffee example.&#160; It is quite contrived but hopefully gives a quick and dirty glimpse at what a Fluent Interface is.</p>
<p>Topics I wish to cover next in the near future:</p>
<ul>
<li>Returning Interfaces to Control sentence structure within Fluent Interfaces. </li>
<li>Using Extensions Methods (C# 3.0) within Fluent Interfaces. </li>
<li>An overview of Quality Open Source Fluent APIs in .NET and what you can learn. </li>
<li>Passing in predicates and utilizing lambda expressions in Fluent&#160; Interfaces. </li>
<li>Internal versus External DSLs. </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.holsee.com/2009/10/fluent-interfaces-internal-domain-specific-languages/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
