<?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>Ryan Cromwell &#187; Windsor</title>
	<atom:link href="http://blog.cromwellhaus.com/index.php/category/windsor/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cromwellhaus.com</link>
	<description>Improving my craft...</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Strategy Pattern with Castle Windsor</title>
		<link>http://blog.cromwellhaus.com/2009/10/strategy-pattern-with-castle-windsor/</link>
		<comments>http://blog.cromwellhaus.com/2009/10/strategy-pattern-with-castle-windsor/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 19:37:56 +0000</pubDate>
		<dc:creator>cromwellryan</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">http://blog.cromwellhaus.com/index.php/2009/10/strategy-pattern-with-castle-windsor/</guid>
		<description><![CDATA[One of the tangent patterns associated with isolation or Single Responsibility is the Strategy Pattern.  I use Castle Windsor as my IoC of choice and I had hoped there was some black magic built in to make the Strategy Pattern dead simple.  Turns out there is and there isn’t. What is the Strategy Pattern The [...]]]></description>
			<content:encoded><![CDATA[<p>One of the tangent patterns associated with isolation or <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank">Single Responsibility</a> is the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" target="_blank">Strategy Pattern</a>.  I use <a href="http://www.castleproject.org/container/index.html" target="_blank">Castle Windsor</a> as my <a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">IoC</a> of choice and I had hoped there was some black magic built in to make the Strategy Pattern dead simple.  Turns out there is and there isn’t.</p>
<p><strong>What is the Strategy Pattern</strong></p>
<p><span style="color: #2e2e2e;">The strategy pattern is a way of isolating what would often be found in a case statement.  The bland example being calculating Sales tax on an Order.  Each state and/or country has its own tax calculation and rather than</span></p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Process(Order order) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">switch</span>(order.Country) {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"USA"</span>:</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>             order.Total = order.Subtotal * .07;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>             <span style="color: #0000ff">break</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>         <span style="color: #0000ff">case</span> <span style="color: #006080">"Canada"</span>:</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>             order.Total = order.Subtotal * .0985;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">break</span>;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     <span style="color: #008000">// ...</span></pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span> }</pre>
</div>
</div>
<p>you should be breaking each calculation out into different ITaxStrategy implementations and consuming them like so:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Process(Order order) {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     var strategy = _taxStrategyFactory.GetStrategyForOrder(order);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>     var salesTax = strategy.CalculateSalesTax(order);</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     order.SalesTax = salesTax;</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>     order.Total = order.SubTotal + order.SalesTax;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span> }</pre>
</div>
</div>
<p>The question remains, how to eliminate the conditional logic in TaxStrategyFactory determining <em>which</em> strategy is chosen.  The solution is to set up a convention for discovering the available strategies and delegating the criteria to the Strategy itself.</p>
<p><strong>Discovery</strong></p>
<p><span style="color: #2e2e2e;">The concept here is that we don’t have to “hook up” any new Tax Strategy to our Processing mechanism, we just “publish” new ones in a known way and they are picked up simply based on their existence.  This is often referred to as <a href="http://en.wikipedia.org/wiki/Convention_over_configuration" target="_blank">Convention Over Configuration</a>.  We accomplish this with Castle Windsor with the <a href="http://using.castleproject.org/display/IoC/Fluent+Registration+API" target="_blank">Fluent Registration API</a>:</span></p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> container</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     .Register(AllTypes</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>                   .FromAssembly(Assembly.GetExecutingAssembly())</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>                   .Where(x =&gt; x.Name.EndsWith(<span style="color: #006080">"Strategy"</span>))</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>                   .WithService.FirstInterface());</pre>
</div>
</div>
<p>This will register all classes found in the executing assembly which have the suffix “Strategy” as implementing the ITaxStrategy interface.  The second half of convention based discovery is consuming the ITaxStrategy implementations.  We can want to get all of these into the TaxStrategyFactory via a constructor array dependency:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> TaxStrategyFactory(<span style="color: #0000ff">params</span> ITaxStrategy[] taxStrategies)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     _taxStrategies = taxStrategies;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> }</pre>
</div>
</div>
<p>Unfortunately, Windsor doesn’t support arrays as dependencies by default.  Windsor has the concept of Resolvers (a strategy implementation itself) and we are going to have to tell it about the ArrayResolver which knows how to, err, resolve array dependencies:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> container</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     .Kernel.Resolver.AddSubResolver(<span style="color: #0000ff">new</span> ArrayResolver(container.Kernel));</pre>
</div>
</div>
<p>Cool, so put these all together in our registration code and we get:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> container</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     .Register(Component</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>                   .For&lt;IOrderProcessor&gt;()</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>                   .ImplementedBy&lt;OrderProcessor&gt;())</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     .Register(Component</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>                   .For&lt;ITaxStrategyFactory&gt;()</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>                   .ImplementedBy&lt;TaxStrategyFactory&gt;())</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     .Register(AllTypes</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>                   .FromAssembly(Assembly.GetExecutingAssembly())</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>                   .Where(x =&gt; x.Name.EndsWith(<span style="color: #006080">"Strategy"</span>))</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>                   .WithService.FirstInterface());</pre>
</div>
</div>
<p>At this point we can kick of the true discovery chain of events through:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> var processor = container.Resolve&lt;IOrderProcessor&gt;();</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> processor.Process(order);</pre>
</div>
</div>
<p><strong>Delegating Criteria</strong></p>
<p>So in OrderProcessor.Process we are asked the TaxStrategyFactory for the applicable ITaxStrategy implementation:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> var strategy = _taxStrategyFactory.GetStrategyForOrder(order);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span> var salesTax = strategy.CalculateSalesTax(order);</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span> //...</pre>
</div>
</div>
<p>  The question is, how did the factory determine which of the boundless implementations applies?  That’s the job of each ITaxStrategy itself.  Here’s the implementation of our factory:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">class</span> TaxStrategyFactory : ITaxStrategyFactory</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span> {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> ITaxStrategy[] _taxStrategies;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> TaxStrategyFactory(<span style="color: #0000ff">params</span> ITaxStrategy[] taxStrategies)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>         _taxStrategies = taxStrategies;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>     }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>     <span style="color: #0000ff">public</span> ITaxStrategy GetStrategyForOrder(Order order)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span>     {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>         <span style="color: #0000ff">return</span> _taxStrategies.FirstOrDefault(x =&gt; x.IsApplicable(order));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>     }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span> }</pre>
</div>
</div>
<p>Notice how the TaxStrategyFactory depends on all of the ITaxStrategy implementations.  It then uses the ITaxStrategy.IsApplicable(Order) method as criteria for a FirstOrDefault call.  Here’s an example TaxStrategy for any Order with country “USA”:</p>
<div style="line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: gray 1px solid; padding: 4px;">
<div style="line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> USTaxStrategy : ITaxStrategy</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   2:</span>     {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   3:</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">readonly</span> IDictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">double</span>&gt; TaxRates = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">double</span>&gt;()</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   4:</span>                                                                    {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   5:</span>                                                                        {<span style="color: #006080">"OH"</span>, 7.0},</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   6:</span>                                                                        {<span style="color: #006080">"MI"</span>, 6.25},</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   7:</span>                                                                    };</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   8:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">double</span> CalculateSalesTax(Order order)</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">   9:</span>         {</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  10:</span>             var rate = TaxRates[order.State];</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  11:</span> </pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  12:</span>             <span style="color: #0000ff">return</span> (order.SubTotal * (rate / 100));</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  13:</span>         }</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  14:</span> </pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  15:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsApplicable(Order order)</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  16:</span>         {</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  17:</span>             <span style="color: #0000ff">return</span> order.Country == <span style="color: #006080">"USA"</span>;</pre>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  18:</span>         }</pre>
<pre style="line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: consolas, &quot;Courier New&quot;, courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #606060">  19:</span>     }</pre>
</div>
</div>
<p><span style="color: #242626;"> </span></p>
<p><span style="color: #242626;">You can download the sample project that goes along with this <a href="http://code.cromwellhaus.com/WindsorStrategySample.zip" target="_self">here</a>.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cromwellhaus.com/2009/10/strategy-pattern-with-castle-windsor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Injecting a WCF Channel as Dependency via Windsor</title>
		<link>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/</link>
		<comments>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 15:45:41 +0000</pubDate>
		<dc:creator>cromwellryan</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">/blogs/ryanc/archive/2009/08/18/injecting-a-wcf-channel-as-dependency-via-windsor.aspx</guid>
		<description><![CDATA[When working within a closed system which uses WCF and an IoC container you will often find the following pattern: &#160; What happens is the SomeAppService is often, but not always, a very thin wrapper over the WCF Service.&#160; If you own both ends of this scenario its often nice to remove the WCF layer [...]]]></description>
			<content:encoded><![CDATA[<p>When working within a closed system which uses WCF and an IoC container you will often find the following pattern:</p>
<p><a href="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/dependency20of.3EAppCode20SomeAppServ.3EWCF20Service20Proxy20SomeAppService20implis20injected20into1_5F00_73E9A3C9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="dependency%20of-.-%3E[AppCode],%20[SomeAppService%20Impl]delegates%20to-.-%3E[WCF%20Service%20Proxy],%20[SomeAppService%20impl]is%20injected%20into-[1]" border="0" alt="dependency%20of-.-%3E[AppCode],%20[SomeAppService%20Impl]delegates%20to-.-%3E[WCF%20Service%20Proxy],%20[SomeAppService%20impl]is%20injected%20into-[1]" src="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/dependency20of.3EAppCode20SomeA.3EWCF20Service20Proxy20SomeAppService20implis20injected20into1_5F00_thumb_5F00_6A413593.png" width="376" height="296" /></a> </p>
<p>&#160;</p>
<p>What happens is the SomeAppService is often, but not always, a very thin wrapper over the WCF Service.&#160; If you own both ends of this scenario its often nice to remove the WCF layer between.&#160; Maybe you want a different deployment model in some situations such that the WCF service itself can be an in-proc reference.&#160; Other times the front end is in a DMZ and the WCF services needs to be inside the corporate firewall.&#160; Maybe its just easier to run on a dev machine.&#160; Whatever the reason, you don’t always want to be tunneling through WCF.&#160; Plus, its just cool to see it work.</p>
<p>To do this, you can use Windsor’s <a href="http://using.castleproject.org/display/IoC/Fluent+Registration+API" target="_blank">FactorySupportFacility</a> to return a <a href="http://msdn.microsoft.com/en-us/library/aa344730.aspx" target="_blank">ClientBase&lt;T&gt;.Channel</a> as the dependency instance.&#160; </p>
<p>First, you’ll need a little class to expose the ClientBase&lt;T&gt;.Channel since it’s protected:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ServiceClient&lt;T&gt; : ClientBase&lt;T&gt; where T : <span style="color: #0000ff">class</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">{
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">new</span> T Channel
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    {
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.Channel;}
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    }
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">}</pre>
</pre>
<p>Second, add the FactorySupportFacility to your WindsorContainer:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container.AddFacility&lt;FactorySupportFacility&gt;();</pre>
</pre>
<p>Lastly, register your implementation as either the WCF channel:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 580px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    .Register(Component.For&lt;ISomeAppService&gt;()
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">                       .UsingFactoryMethod(() =&gt; <span style="color: #0000ff">new</span> ServiceClient&lt;IWCFService&gt;().Channel))
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
</pre>
<p>…or the actual implementation:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 580px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    .Register(Component.For&lt;ISomeAppService&gt;()
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">                  .ImplementedBy&lt;WCFServiceImpl&gt;())</pre>
<p>&#160;</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

