I think that this does not correspond to Prism or MEF, but to the principle of dependency injection and the best methods in general. (Yes, I am tying that MEF is not a DI container, but here it is used almost like a DI container, so I suggest using the same methods here).
In DI best practices ( this book is very cool, I highly recommend it), itβs good to have these steps in the DI "workflow":
- register all necessary types (in Prism mode - by Bootstrapper.ConfigureCatalog ())
- allow the root object (with all nested objects). In Prism, by the Bootstrapper.CreateShell () method)
- use your root object
- free root object
Ideally, you should NOT use the DI container anymore. Your code should not know about the existence of a DI container (on this side, Unity really is a DI container, because you can write code that does not know about using a DI container). If your code knows about this, it DEPENDS on the DI container, and this is bad.
PS. If you still want to use the MEF container in your module (for example, because you are not very familiar with the DI paradigm or you have some 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; } }
Remember to register your MEF container in your Boostrapper:
public class YourBootstrapper: MefBootstrapper { protected override CompositionContainer CreateContainer() { var container = base.CreateContainer(); container.ComposeExportedValue(container); return container; } }
source share