In my Prism app, the MEF container is available from within the Bootstrapper class via the Container property.
But it isn't from within the class module (IModule). I may only import the container through IServiceLocator.
Why? I thought that it made sense to use a common interface against concrete technology, but the Prism 4.1 guideline ask us to not use IServiceLocator (in Considerations for Using IServiceLocator).
I think it corresponds not to Prism or MEF, but to Dependency Injection principle and best practices in general. (Yeah, I knot that MEF isn't DI container, but here it's used almost as DI container, so I suppose to use the same practices here).
In best practices of DI (this book is very cool, I highly recommend it) it's good to have such steps in DI "workflow":
Ideally, you should NOT use DI container anymore. Your code should NOT know about existance of DI container (from this side Unity is really DI container, because you can write code which will not know about using DI container). If your code knows about it - it DEPENDS on DI container, and it's bad thing.
PS. If you want to use MEF container in your module anyway (for example, because you're not very familiar with DI paradigm or you have some very specific tasks), you can try something like:
[ModuleExport(typeof(YourModule))]
public class YourModule : IModule
{
public static CompositionContainer CompositionContainer;
[ImportingConstructor]
public void YourModule(CompositionContainer container)
{
this.CompositionContainer = container;
}
}
Don't forget to register your MEF container itself in your Boostrapper:
public class YourBootstrapper: MefBootstrapper
{
protected override CompositionContainer CreateContainer()
{
var container = base.CreateContainer();
container.ComposeExportedValue(container);
return container;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With