简体   繁体   中英

How do I pass the UnityContainer as a parameter to a registration in a Unity IOC xml configuration file

I have an implementation of a factory interface that uses an IOC container to create it's objects.

The container is a automatic property on the Factory, and ideally I would like to inject this property with same container where the factory is being registered.

For clarity I have described what I would like it to look like below.

<container name="MyContainer">
    <register type="IControllerFactory" mapTo="UnityControllerFactory">        
        <property name="UnityContainer" value="MyContainer" />
    </register>
</container>    

Of course I have registered a class after this that automatically injects this registration into it's IControllerFactory property.

Is there a way to do this?

The IUnityContainer registered with itself by default. You can just set up your constructor like this:

public UnityControllerFactory(IUnityContainer unityContainer) 
{
   this.unityContainer = unityContainer;
}

No addition configuration is needed.... the dependency will be resolved hassle-free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM