<?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>Jason Rowe &#187; Windsor</title>
	<atom:link href="http://jasonrowe.com/tag/windsor/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonrowe.com</link>
	<description>enjoying the web</description>
	<lastBuildDate>Tue, 03 Jan 2012 02:03:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Factory Pattern using Windsor and MVC</title>
		<link>http://jasonrowe.com/2011/07/15/factory-pattern-using-windsor-and-mvc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=factory-pattern-using-windsor-and-mvc</link>
		<comments>http://jasonrowe.com/2011/07/15/factory-pattern-using-windsor-and-mvc/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 21:56:56 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Named]]></category>
		<category><![CDATA[Typed Factory]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1477</guid>
		<description><![CDATA[I ran into a situation where I wanted to return a dynamically drawn image based on the file name . For example, if someone requested somedomain.com/image/legacypage42.foo the &#8220;legacypage42.foo&#8221; would be the name of the Windsor component used to draw the&#8230;]]></description>
			<content:encoded><![CDATA[I ran into a situation where I wanted to return a dynamically drawn image based on the file name . For example, if someone requested somedomain.com/image/legacypage42.foo the &#8220;legacypage42.foo&#8221; would be the name of the Windsor component used to draw the image. The first thing I needed to do was change that page into a parameter which is very simple with MVC .net routing:

<pre class="brush: c#; ">
  
routes.MapRoute(&quot;MyImageRoute&quot;,
                &quot;image/{filename}&quot;,
                new { controller = &quot;Images&quot;, action = &quot;Index&quot; });

</pre>

So now filename becomes the parameter in the controller. Now comes the tricky part. How to resolve a component in Windsor by name. Windsor does allow you to set the name using this syntax:

<pre class="brush: c#; ">
  
kernel.Register(
    Component.For&lt;IMyImageComponent&gt;()
        .ImplementedBy&lt;MyImageComponent&gt;()
        .Named(&quot;SomeImage&quot;));

</pre>

Although, using that syntax is going to make for a nightmare if I have many different image components so I&#8217;ll use the built in <a href="http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx">Windsor Type Factory</a>. The first step is to cleanly register all my image components setting the name. To do that I created an interface all my images will use so I can easily set the named value.

<pre class="brush: c#; ">
  
Interface example:

    public interface IMyImageComponent
    {
        Bitmap BuildImage();

        string GetFileName();
    }

</pre>

Here is how I use the interface to setup my Windsor components and named values.

<pre class="brush: c#; ">
  
var type = typeof(IMyImageComponent);
            var styles = AppDomain.CurrentDomain.GetAssemblies().ToList()
                .SelectMany(s =&gt; s.GetTypes())
                .Where(type.IsAssignableFrom);

            //find all classes that implement IMyImageComponent and register using name from GetFileName()
            foreach (var style in styles)
            {
                if (!style.IsClass || style.IsNotPublic || style.IsAbstract)
                    continue;

                object o = Activator.CreateInstance(style);
                var propertyInfo = style.GetMethod(&quot;GetFileName&quot;);
                var name = (string)propertyInfo.Invoke(o, null);
                container.Register(Component.For&lt;IMyImageComponent&gt;().ImplementedBy(o.GetType()).Named(name));

</pre>

Here is how the factory was initialized:

<pre class="brush: c#; ">
  
            container.Register(Component.For&lt;ITypedFactoryComponentSelector&gt;().ImplementedBy&lt;ImageStyleFactorySelector&gt;().Named(&quot;ImageStyleFactorySelector&quot;));

            container.Register(Component.For&lt;IMyImageTemplateFactory&gt;()
                .AsFactory(c =&gt; c.SelectedWith(&quot;ImageStyleFactorySelector&quot;)));

</pre>

Notice I used a custom selector in the factory. This is used when the factory is looking up the components. Here is what the FactorySelector looks like.

<pre class="brush: c#; ">
  
public class ImageStyleFactorySelector : DefaultTypedFactoryComponentSelector
    {
        protected override string GetComponentName(MethodInfo method, object[] arguments)
        {
            if (method.Name == &quot;GetByFileName&quot; &amp;&amp; arguments.Length == 1 &amp;&amp; arguments[0] is string)
            {
                return arguments[0].ToString();
            }
            return base.GetComponentName(method, arguments);
        }
    }

</pre>

Now inside the controller I use the factory with the function &#8220;GetByFileName&#8221; which uses the custom selector to find the component.

<pre class="brush: c#; ">
  
var imageComponent = _ImageTemplateFactory.GetByFileName(fileName);

</pre>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2011/07/15/factory-pattern-using-windsor-and-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

