InjectionFactory configurative?

Jul 15, 2010 at 12:17 PM

What is the configurative equivalent to:

 unityContainer.RegisterType<IMyInterface>(new InjectionFactory(MyClassFactory.CreateInstance));
The documentation does not give any hints on this.

Thanks,

Tobias

Jul 16, 2010 at 2:02 AM
You cannot currently use the config file to set up factories. It is possible to extend config to do that if you want for some subset of factory method signatures.
Jul 16, 2010 at 7:38 AM
Edited Jul 16, 2010 at 7:38 AM

Hmm, for the scenario I intended to use Unity 2 this is a knock out. Another thing is the missing configuration editor component in Enterprise Lib. 5.

Jul 16, 2010 at 6:56 PM

The config is extensible. What do you want your factory methods to look like? Are they always static methods that take no parameters (or takes the container)? If so, it's pretty easy to knock up a config extension. It's just that the "put any arbitrary code in config" case is very, very difficult to do.

As for the config tool, well, it was wait another 3 - 4 months before shipping anything or cut it. We had other bugs to fix in the rest of Entlib (and the rest of the config tool) and neither the time nor the budget to do both. Personally, I've found having the XML intellisense in VS and the simplified schema has already made it easier enough that typing config in by hand is, I personally think, actually faster / easier than clicking buttons in the gui.

I do hope to ship the graphical config tool one of these days.

 

 

Jul 17, 2010 at 12:08 AM

And just to prove the point, I just threw together a sample implementation of such an extension. The source is available from my BitBucket account.

This is just from me, not an official p&p deliverable, not part of the package, no guarantee, it may delete your hard drive and tie dye your cat.

But what it does do is let you do this:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <sectionExtension type="Unity.FactoryConfig.FactoryConfigExtension, Unity.FactoryConfig"/>
  <container>
    <register type="IMyInterface">
      <factory type="MyClassFactory" method="CreateInstance" />
    </register>
  </container>
</unity>

As long as MyClassFactory.CreateInstance is a public, static method wiith one of these signatures:

  • no parameters
  • IUnityContainer
  • IUnityContainer, Type, string

it should work just fine.

I'll write up a blog post later on how this works, but just wanted to share the code out ahead of time.

Jul 19, 2010 at 7:32 AM

Great, I will look into it and give you feedback.