Don't forget to register your MEF container itself in your Boostrapper:

来源:互联网 发布:软件研发设备清单 编辑:程序博客网 时间:2024/06/16 09:38

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;    }}
0 0
原创粉丝点击