具有多个相互依赖的部分的MEF组合物

时间:2015-06-24 19:58:58

标签: c# inversion-of-control mef

设计基于MEF的插件架构的最佳方法是什么?插件之间存在依赖关系?例如:

  class MainForm
  {
    CompositionContainer container;

    [ImportMany]
    IEnumerable<Lazy<IWindow>> windows;

    public MainForm()
    {
      this.container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
      this.container.ComposeParts(this);
    }

    public void DoSomething()
    {
      foreach (var winPart in windows)
      {
        Debug.WriteLine(winPart.Value.Name);
      }
    }
  }


  interface IWindow
  {
    string Name { get; }
  }

  delegate void AnEvent(object sender, EventArgs e);
  interface IEventManager
  {
    event AnEvent AnEvent;
    void OnAnEvent(object sender, EventArgs eventArgs);
  }

  [Export(typeof(IEventManager))]
  class EventManager : IEventManager
  {
    public event AnEvent AnEvent = delegate { };
    public void OnAnEvent(object sender, EventArgs eventArgs) { AnEvent(sender, eventArgs); }
  }

  [Export(typeof(IWindow))]
  class Window1 : IWindow
  {
    [Import]
    IEventManager eventMgr;

    public Window1() { }

    public string Name
    {
      get
      {
        eventMgr.OnAnEvent(this, new EventArgs());
        return "Window1";
      }
    }
  }

  [Export(typeof(IWindow))]
  class Window2 : IWindow
  {
    [Import]
    IEventManager eventMgr;

    public Window2()  {  this.eventMgr.AnEvent += eventMgr_AnEvent; }

    void eventMgr_AnEvent(object sender, EventArgs e)
    {
      Debug.WriteLine("Event from Window 2");
    }

    public string Name { get { return "Window2"; } }
  }

Window2构造函数this.eventMgr为空。我希望它由MainForm组成。一种方法是拥有多个CompositionContainers,但如果这样做,则有两个{​​{1}}实例而不是一个共享实例。这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:0)

在更多地探索这个问题时,完全允许相互依赖。关键是将目录中的程序集传递给用于组成部件的CompositionContainer。然后,以下代码帮助允许一个插件访问另一个插件:

  [Export(typeof(IWindow))]
  class Window2 : IWindow
  {
    IEventManager eventMgr;

    [ImportingConstructor]
    public Window2([Import(typeof(IEventManager))]IEventManager mgr)
    {
      this.eventMgr.AnEvent += eventMgr_AnEvent;
    }

    void eventMgr_AnEvent(object sender, EventArgs e)
    {
      Debug.WriteLine("Event from Window 2");
    }

    public string Name { get { return "Window2"; } }
  }