<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Unity and MVC–Resolving types challenge</title><link>http://blogs.msdn.com/b/eugeniop/archive/2011/01/26/unity-and-mvc-resolving-types-challenge.aspx</link><description>Imagine you have a situation like this: 2 MVC controllers (A and B) have a dependency on Svc A . SvcA has a dependency on SvcB and a component implementing IX . SvcB has a dependency on something implementing IY . Every time the system resolves CtrlA</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Unity and MVC–Resolving types challenge</title><link>http://blogs.msdn.com/b/eugeniop/archive/2011/01/26/unity-and-mvc-resolving-types-challenge.aspx#10121815</link><pubDate>Sat, 29 Jan 2011 06:07:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121815</guid><dc:creator>Eugenio</dc:creator><description>&lt;p&gt;Thanks Nick! I&amp;#39;ve never tried Autofac, but heard great things about it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121815" width="1" height="1"&gt;</description></item><item><title>re: Unity and MVC–Resolving types challenge</title><link>http://blogs.msdn.com/b/eugeniop/archive/2011/01/26/unity-and-mvc-resolving-types-challenge.aspx#10121808</link><pubDate>Sat, 29 Jan 2011 05:15:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121808</guid><dc:creator>Nicholas Blumhardt</dc:creator><description>&lt;p&gt;Hi Eugenio,&lt;/p&gt;
&lt;p&gt;This is pretty straightforward in Autofac, I guess that the same approach can be used in Unity.&lt;/p&gt;
&lt;p&gt;I would be inclined to suggest that it is not good practice to try to dynamically choose between Y1 and Y2 at runtime based on which controller is being instantiated further up the graph. This will be fragile and is best avoided when possible.&lt;/p&gt;
&lt;p&gt;There are several ways to accomplish this, ranging from parameter passing to Multitenancy.&lt;/p&gt;
&lt;p&gt;The simplest solution is to think about components as an abstraction on top of types, so the same type can be used to implement several components. We can distinguish between different components implemented by the same type by giving them names.&lt;/p&gt;
&lt;p&gt;First, we register the &amp;#39;default&amp;#39; configuration that will be used by controller A. That is, we just register Controller A, Service A, Service B and Y1 as if controller B&amp;#39;s configuration is irrelevant. Controller A can just use autowiring, and we can forget about it from hereon in.&lt;/p&gt;
&lt;p&gt;Then, we create named registrations for Service A and Service B, which we&amp;#39;ll call &amp;quot;alternate&amp;quot; and configure these to use Y2:&lt;/p&gt;
&lt;p&gt;builder.RegisterType&amp;lt;ServiceA&amp;gt;()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.Named&amp;lt;ServiceA&amp;gt;(&amp;quot;alternate&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.WithParameter(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; pi.ParameterType == typeof(ServiceB),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; c.ResolveNamed&amp;lt;ServiceB&amp;gt;(&amp;quot;alternate&amp;quot;);&lt;/p&gt;
&lt;p&gt;builder.RegisterType&amp;lt;ServiceB&amp;gt;()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.Named&amp;lt;ServiceB&amp;gt;(&amp;quot;alternate&amp;quot;)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.WithParameter(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; pi.ParameterType == typeof(IY),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; c.ResolveNamed&amp;lt;IY&amp;gt;(&amp;quot;alternate&amp;quot;);&lt;/p&gt;
&lt;p&gt;builder.RegisterType&amp;lt;Y2&amp;gt;()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.Named&amp;lt;IY&amp;gt;(&amp;quot;alternate&amp;quot;);&lt;/p&gt;
&lt;p&gt;Named registrations like this aren&amp;#39;t picked up in normal dependency wiring, so for all intents and purposes they&amp;#39;re invisible to the rest of the container.&lt;/p&gt;
&lt;p&gt;Now, when we register the controllers, we set controller B to use the alternate implementation of service A:&lt;/p&gt;
&lt;p&gt;builder.RegisterControllers()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;.Except&amp;lt;ControllerB&amp;gt;(b =&amp;gt; b&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.WithParameter(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; pi.ParameterType == typeof(ServiceA),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(pi, c) =&amp;gt; c.ResolveNamed&amp;lt;ServiceA&amp;gt;(&amp;quot;alternate&amp;quot;)));&lt;/p&gt;
&lt;p&gt;Seems fairly long-winded, but this should be a rare situation. If at all possible, in the &amp;quot;real world&amp;quot; I&amp;#39;d be trying to eliminate this situation from the design, but I realise that isn&amp;#39;t always possible or desirable.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;Nick&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121808" width="1" height="1"&gt;</description></item><item><title>re: Unity and MVC–Resolving types challenge</title><link>http://blogs.msdn.com/b/eugeniop/archive/2011/01/26/unity-and-mvc-resolving-types-challenge.aspx#10121043</link><pubDate>Thu, 27 Jan 2011 15:39:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121043</guid><dc:creator>Eugenio</dc:creator><description>&lt;p&gt;I thought about subcontainers too, but the code seemed to complicated. It could work though, there&amp;#39;s no single (correct) answer. Thanks for posting yours!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121043" width="1" height="1"&gt;</description></item><item><title>re: Unity and MVC–Resolving types challenge</title><link>http://blogs.msdn.com/b/eugeniop/archive/2011/01/26/unity-and-mvc-resolving-types-challenge.aspx#10121001</link><pubDate>Thu, 27 Jan 2011 14:37:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121001</guid><dc:creator>Abc</dc:creator><description>&lt;p&gt;IF (ControllerFactory to have no knowledge) THEN&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Write custom extension that will have this &amp;quot;logic&amp;quot; (those IFs for different CTRLs) and will return desired type/instance in special cases, otherwise will allow normal resolution of dependencies.&lt;/p&gt;
&lt;p&gt;ELSE&lt;/p&gt;
&lt;p&gt; &amp;nbsp;use child contaner per each such drawn group class&lt;/p&gt;
&lt;p&gt;Somewhere you you will have to place those if (or translate ifs into some kind of hierarchy (interface/object), but in this case if will exist inside of unity during resolution)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121001" width="1" height="1"&gt;</description></item></channel></rss>