question about instantiating unityhttp://forums.asp.net/t/1650182.aspx/1?question+about+instantiating+unitySun, 06 Feb 2011 04:22:03 -050016501824289598http://forums.asp.net/p/1650182/4289598.aspx/1?question+about+instantiating+unityquestion about instantiating unity <p style="margin:0px"><span style="color:#2b91af"><font color="#000000">Here is some simple code that configures a UnityContainer, registers a few different types of loggers and then gets a handle to one of the logger types:</font></span></p> <p style="margin:0px"><span style="color:#2b91af"><font color="#000000"></font></span>&nbsp;</p> <p style="margin:0px"><span style="color:#2b91af"><font color="#000000"></font></span>&nbsp;</p> <p style="margin:0px"><span style="color:#2b91af"><font color="#000000">&nbsp;&nbsp; </font> IUnityContainer</span> container = <span style="color:blue">new</span> <span style="color:#2b91af"> UnityContainer</span>();</p> <p style="margin:0px">&nbsp;</p> <p style="margin:0px">&nbsp;&nbsp; container.RegisterType&lt;<span style="color:#2b91af">ILogger</span>, <span style="color:#2b91af">FileLogger</span>&gt;(<span style="color:#a31515">&quot;FileLogger&quot;</span>);</p> <p style="margin:0px">&nbsp;&nbsp; container.RegisterType&lt;<span style="color:#2b91af">ILogger</span>, <span style="color:#2b91af">DatabaseLogger</span>&gt;(<span style="color:#a31515">&quot;DBLogger&quot;</span>);</p> <p style="margin:0px">&nbsp;</p> <p style="margin:0px">&nbsp;&nbsp; <span style="color:blue">var</span> logger = container.Resolve&lt;<span style="color:#2b91af">ILogger</span>&gt;(<span style="color:#a31515">&quot;FileLogger&quot;</span>);&nbsp;<br> &nbsp;</p> <p style="margin:0px">&nbsp;</p> <p style="margin:0px">I think this standalone code is fairly straightforward.&nbsp; However, I am curious about the best way to access this code in an application.</p> <p style="margin:0px">&nbsp;</p> <p style="margin:0px">Seems like it might be easiest to include this config code in an AppConfig.cs class and then reference the AppConfig.cs class as a Singleton throughout the application.</p> <p style="margin:0px">&nbsp;</p> <p style="margin:0px">I need to implement Unity for the first time in a business app.&nbsp; How do you normally choose to implement Unity in your business apps?</p> <p style="margin:0px">&nbsp;</p> 2011-02-06T04:22:03-05:00