StaticFactoryExtension Configuration Through Config File

Mar 9, 2010 at 8:45 PM


Does anyone know the best way to configure a StaticFactoryExtension through the .config file, that is, not from code?

I would like to have a static method create new instances of registered types, but only through configuration. Any ideas?



Mar 10, 2010 at 12:24 AM

This is currently not supported as there's no way to specify a delegate in configuration file.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Mar 10, 2010 at 12:16 PM

Hello, Sarah, thanks for the quick response!

I know it is not natively possible; however, there must be a way to do this. Of course, I could write my own custom section, register a bunch of elements containing StaticFactoryTypeName and StaticFactoryMethodName attributes, and do it all manually, but I would like to do this in a more Unity-friendlier way, such as with extensions (actually I think extensions won't work), or any other Unity mechanism.

Any ideas?



Mar 11, 2010 at 5:18 AM

The Unity config schema is itself extensible, you just need to derive from the right type and then plug it in. The extensibility mechanism has changed (i.e. been rewritten) completely with Unity 2.0, so I'd suggest looking at that. It would be pretty straightforward to plug in a new element to configure a factory.

So you're looking for something along these lines?


<register type="IMyType" mapTo="MyTypeImpl">
    <factory type="MyTypeImplFactory" method="CreateMyType" />

That'd be pretty easy to do, but before I put any time in on it I need to get Unity 2.0 out the door. In the meantime, take a look at the code for the Unity.Configuration assembly, particularly the ConstructorElement - that's the kind of thing you'd need to implement. Documentation will be forthcoming on this, promise. :-)