<?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>out &#62;&#62; m_Conscientia; &#187; factory</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/factory/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Sat, 27 Aug 2011 19:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>C# Generic Factory</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/03/c-generic-factory/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/03/c-generic-factory/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 00:28:22 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[factory]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.blog.hypercomplex.co.uk/?p=48</guid>
		<description><![CDATA[Since I tend to think in C++, I&#8217;ve found that I program C# with a C++ accent (as I realised when I read C# in depth). In C++, the Loki library provides a solid foundation for most patterns I use (I started out with patterns after reading the excellent Modern C++ Design). I assumed that [...]]]></description>
			<content:encoded><![CDATA[<p>Since I tend to think in C++, I&#8217;ve found that I program C# with a C++ accent (as I realised when I read <a href="http://csharpindepth.com/" target="_blank">C# in depth</a>). In C++, the <a href="http://loki-lib.sourceforge.net/" target="_blank">Loki</a> library provides a solid foundation for most patterns I use (I started out with patterns after reading the excellent <a href="http://en.wikipedia.org/wiki/Modern_C%2B%2B_Design" target="_blank">Modern C++ Design</a>). I assumed that a similar library must exist for C# generics, but as yet haven&#8217;t found it.</p>
<p>When I wanted a factory yesterday I found <a href="http://tranxcoder.wordpress.com/2008/07/11/a-generic-factory-in-c/" target="_blank">this</a> method after a little searching, and I like the way it gets you close to the static scope initialisation of C++. I also like the fact that you don&#8217;t have to do anything special to use different constructors, and you can use the Type type instead of generating GUIDs to identify types COM style. I have two minor observations, however. Firstly, if you have a class hierarchy that spans multiple assemblies, the example code will miss some objects. Secondly, the ctor collects abstract types, which seems a little wasteful since you can&#8217;t instantiate them. So here&#8217;s my contribution:</p>

<div class="wp_codebox"><table><tr id="p482"><td class="code" id="p48code2"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Factory<span style="color: #008000;">&lt;</span>Product<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, Type<span style="color: #008000;">&gt;</span> map <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, Type<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Factory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Evaluate all types in all assemblies loaded by the current app</span>
            Assembly<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> assemblies <span style="color: #008000;">=</span> AppDomain<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentDomain</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetAssemblies</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Assembly assembly <span style="color: #0600FF; font-weight: bold;">in</span> assemblies<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Type<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> types <span style="color: #008000;">=</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetTypes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Type type <span style="color: #0600FF; font-weight: bold;">in</span> types<span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">// if Type is not derived from Product Type skip it and continue searching</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>Product<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsAssignableFrom</span><span style="color: #008000;">&#40;</span>type<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> type <span style="color: #008000;">==</span> <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>Product<span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">IsAbstract</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
                    <span style="color: #008000;">&#125;</span>
&nbsp;
                    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">map</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>type<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, type<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Product CreateObject<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> type, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>Product<span style="color: #008000;">&#41;</span>Activator<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateInstance</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">map</span><span style="color: #008000;">&#91;</span>type<span style="color: #008000;">&#93;</span>, args<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
     <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>At this point I was relatively pleased. Then in a code review a colleague pointed out my C++ accent (why did you call the dictionary a map? &#8211; because I copied and pasted), and suggested that with reflection you shouldn&#8217;t need to store such a map. I played around for about half an hour and couldn&#8217;t find an efficient way to do the type look up from a string (which is not to say that it&#8217;s impossible). I think if you need to evaluate types in all the loaded assemblies it&#8217;s probably going to be a relatively expensive operation, and you therefore want to cache the result.</p>
<p>The next thing to fix/handle is the possibility of name collisions when you define types in different namespaces.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/03/c-generic-factory/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

