PRISM + Unity - Singleton per IModule

Apr 23, 2009 at 11:06 AM

I'd like to be able to configure Unity so that if a class within a module requires an instance of, say, IMyInterface via constructor injection, it gets a new singleton instance for that module. So, if another module also has a class with a constructor signature including IMyInterface, it gets a different one to the first module. Possible?

The reason behind this is that I have several modules which need module-singleton instances of classes which connect to backend servers. I could create named instances but that would mean I couldn't use constructor injection.

Has anyone encountered a similar requirement?

Many thanks
Apr 23, 2009 at 12:57 PM
I've come up with a solution;

1. Declare an IUnityContainer as an arg of the module's constructor (you'll get the default container injected)
2. Create a child container from this default container (defaultContainer.CreateChildContainer())
3. Register anything you need in this child container
4. Use the child container to resolve all your types - anything not registered in the child will come from the default container

It would be nice to be able to configure a child container from a config file, but still working on how to do that.

It would also be nice if PRISM either gave you a module-specific container by default, or allowed you to specify what you prefer such as in the <modules> section entries in App.config.