<?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>Agile at Work &#187; Fluent Interface</title>
	<atom:link href="http://www.agileatwork.com/tag/fluent-interface/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.agileatwork.com</link>
	<description>by Michael Valenty</description>
	<lastBuildDate>Sat, 10 Sep 2011 14:35:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Console application with IoC</title>
		<link>http://www.agileatwork.com/console-application-with-ioc/</link>
		<comments>http://www.agileatwork.com/console-application-with-ioc/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 03:41:43 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fluent Interface]]></category>
		<category><![CDATA[Inversion of Control]]></category>

		<guid isPermaLink="false">http://www.agileatwork.com/?p=203</guid>
		<description><![CDATA[I like to think of the Main method in a Console application like the Global.asax. Its single responsibility is to wire-up things to be run in a console context. Each environment has unique configuration requirements. For example, when using NHiberate in a web application the lifetime of the ISession is attached to the HttpRequest, however [...]]]></description>
			<content:encoded><![CDATA[<p>I like to think of the <font size="2" face="Courier New">Main</font> method in a Console application like the Global.asax. Its single responsibility is to wire-up things to be run in a console context. Each environment has unique configuration requirements. For example, when using NHiberate in a web application the lifetime of the <font size="2" face="Courier New">ISession</font> is attached to the <font size="2" face="Courier New">HttpRequest</font>, however in the context of a console application it may be a singleton or “per job” in a windows service. I like this:</p>
<pre class="prettyprint">
<code>class Program
{
    static void Main(string[] args)
    {
        using (var container = new UnityContainer())
        {
            container
                .AddExtension(new ConfigureForConsole(args))
                .Resolve&lt;MyApplication&gt;()
                .Execute();
        }
    }
}</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/console-application-with-ioc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Order Processing Pipeline in ASP.NET MVC</title>
		<link>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/</link>
		<comments>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/#comments</comments>
		<pubDate>Sun, 03 May 2009 07:57:28 +0000</pubDate>
		<dc:creator>Michael Valenty</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[Fluent Interface]]></category>

		<guid isPermaLink="false">http://blog.agileatwork.com/?p=8</guid>
		<description><![CDATA[Lately, I can’t seem to shake the pipeline pattern. It keeps popping up in all my applications. It’s like when I think about buying a new car and then I start seeing them everywhere on road and think – have there always been that many out there?
So, here it is in action. Below is a [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I can’t seem to shake the pipeline pattern. It keeps popping up in all my applications. It’s like when I think about buying a new car and then I start seeing them everywhere on road and think – have there always been that many out there?</p>
<p>So, here it is in action. Below is a controller from an ASP.NET MVC application. The method accepts an order in the form of xml for provisioning products in our system.</p>
<pre class="prettyprint">
<code>public ActionResult EnqueueOrders(string orderXml)
{
    var context = new OrderContext(orderXml);

    var pipeline = new FilterPipeline&lt;OrderContext&gt;()
        .Add(new RewriteLegacyUsernameFeature(productFinder))
        .Add(new ValidateOrderXml())
        .Add(new ValidateReferenceCode(customerFinder))
        .Add(new IgnoreOnException(new ValidateRemoteAccountUsernames(packageRepository)))
        .Add(new IgnoreOnException(new ValidateRemoteAccountUniqueEmail(packageRepository)))
        .Add(new EnqueueOrders(customerFacade)).When(new OrderHasNoErrors())
        .Execute(context);

    var xml = BuildResponseFromOrderContext(context);

    return new XmlResult(xml);
}</code>
</pre>
<p>Most of it is just validation logic, but there are a few meatier pieces. Take a look a the first filter – RewriteLegacyUsernameFeature. If you’ve ever published an API that accepts xml, then you’ve probably wanted to change your schema 5 minutes later. The pipeline is great way to deal with transforming legacy xml on the way in.</p>
<p>Next is the implementation of the FilterPipeline which is essentially a chain of responsibility with a few convenience features.</p>
<pre class="prettyprint">
<code>public class FilterPipeline&lt;T&gt; : IFilter&lt;T&gt;
{
    private IFilterLink&lt;T&gt; head;

    public void Execute(T input)
    {
        head.Execute(input);
    }

    public IFilterConfiguration&lt;T&gt; Add(IFilterLink&lt;T&gt; link)
    {
        var specificationLink = new SpecificationFilterLink&lt;T&gt;(link);

        AppendToChain(specificationLink);

        return specificationLink;
    }

    public IFilterConfiguration&lt;T&gt; Add(IFilter&lt;T&gt; filter)
    {
        return Add(new FilterLinkAdapter&lt;T&gt;(filter));
    }

    public void AppendToChain(IFilterLink&lt;T&gt; link)
    {
        if (head == null)
        {
            head = link;
            return;
        }

        var successor = head;

        while (successor.Successor != null)
        {
            successor = successor.Successor;
        }

        successor.Successor = link;
    }
}</code>
</pre>
<p>You might be scratching your head and wondering what this business is with both IFilter&lt;T&gt; and IFilterLink&lt;T&gt;. IFilter&lt;T&gt; is just a simpler version of IFilterLink&lt;T&gt; that doesn’t require the implementer to deal with calling the next link in the chain. The subject will always pass through, no short-circuiting, hence the pipeline.</p>
<p>My favorite part  is the SpecificationFilterLink&lt;T&gt; which is a decorator that uses a Specification to decide if the filter should be invoked. So you can do little readable snippets like:</p>
<pre class="prettyprint">
<code>pipeline.Add(new EnqueueOrders(customerFacade)).When(new OrderHasNoErrors());</code>
</pre>
<p>Maybe this post will get it out of my system and I can move on to other solutions. It’s just so handy…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.agileatwork.com/an-order-processing-pipeline-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

