Unity application block and Factory

Jan 18, 2011 at 12:10 PM


I want to use Unity application block where I want to specify which factory object Unity should use and want to specify which method of my factory should be used to create that object.
e.g. I want to create new WCF Channel using WCF ChannelFactory class and by using CreateChannel() method of the ChannelFactory class. We were using spring.net in which it was easily possible and configuration was like 
<object id="IUltrasoundService"    type="ServiceContracts.IUltrasoundService, Service.Core"  factory-object="UltrasoundServiceChannelFactory" factory-method="CreateChannel" />      
<object id="UltrasoundServiceChannelFactory"   type="CustomChannelFactory&lt;ServiceContracts.IUltrasoundService&gt;, My.Framework">        
<constructor-arg name="endpointConfigurationName" value="basicHttpBinding_IUltrasoundService" />        

I want to do the same thing with Unity application block. is it possible - if YES then who?

Thanks in advance.


Jan 19, 2011 at 5:50 AM

There's nothing built into configuration to do this. Out of the box, you need to use the container API directly. It's pretty straightforward:

    new InjectionFactory(c => {
        factory = c.Resolve<SomeFactory>(); 
        return factory.CreateMethod(); 

If it has to be in the configuration file, I have an example project up on BitBucket that demonstrates how to extend Unity configuration, specifically to let you configure a static method as a factory. I'm pretty sure it doesn't do exactly what you want (I'm not familiar with Spring so I have no idea what that config you posted actually does) but it should give you some ideas.



Jan 21, 2011 at 6:33 AM

I thank you very much. your sample helped (an example project up on BitBucket) helped me a lot. But I need this for Entlib 4.1 (e.g Unity 1.2). This sample doesn't works with Entlib 4.1 because we don't have class SectionExtension in Unlity 1.2 and even InjectionMemberElement class abstract method is changed. So to achieve same thing (an example project up on BitBucket) with Entlib 4.1/unity 1.2?

Thanks in advance.



Jan 21, 2011 at 5:39 PM

Sorry, I don't have a Unity 1.2 version of the project. It's possible, but a lot more difficult and a lot harder to use.