【问题标题】:MEF & optional plugin dependencies - best practice?MEF 和可选插件依赖项 - 最佳实践?
【发布时间】:2016-02-28 13:01:41
【问题描述】:

所以我有一个相当愚蠢的问题,但我想不出一个真正好的方法来解决这个“困境”。

我正在使用 MEF 开发一个应用程序,并试图提出一种处理可选依赖项的好方法。

随机示例,假设有两个插件; “MyPlugin”,以及一个状态栏插件,可能会或可能不会被主应用程序加载。 MyPlugin 应该对状态栏管理器有一个可选的依赖项;如果该插件由 MEF 加载,它将使用它将某些内容输出到状态栏;如果没有,它根本不会。

[ModuleExport(typeof(MyPlugin))]
public class MyPlugin : IModule
{
  [ImportingConstructor]
  public MyPlugin([Import(AllowDefault = true)] StatusBarManager optionalStatusBarManager OptionalStatusBar)
  {
    if(optionalStatusBarManager != null)
      optionalStatusBarManager.Display("Hi from MyPlugin!");
  }
}

为了了解“StatusBarManager”类型,MyPlugin 项目需要对 StatusBarManager 项目/它的 dll 的引用。

现在从我的直觉来看,如果 MyPlugin 需要 StatusBarManager 的 dll 仍然可以访问,因为它依赖于其公开的类型,那么具有可选依赖项几乎没有意义。

但是我有什么选择?有没有什么体面的方法可以拥有这样的可选依赖项,而无需主应用程序访问可选依赖项的 dll?

到目前为止,我只能想出使用“动态”类型按合同名称导入这些内容。但是我显然会在 MyPlugin 中失去所有编译时安全性。

或者我可以创建另一个项目,例如仅包含接口定义的 StatusBarManagerAbstractions。那么至少 MyPlugin 只会对相对较小的接口项目有硬依赖,而不需要(可能很大的)实现库。总比没有好,但我仍然认为它不是一个完全“可选”的依赖项。

我是不是想多了?!是否有任何最佳实践来处理此类问题?

【问题讨论】:

    标签: c# plugins mef


    【解决方案1】:

    tldr;你可能想多了:)

    加长版

    我正在使用 MEF 开发一个应用程序,并试图提出一种处理可选依赖项的好方法。

    由于依赖项是可选的,因此当前的示例代码就足够了。但是,作为一般的最佳实践,所有依赖项都将通过接口引用/编码,而不是具体类型。在 MainModule 的依赖解析过程中,CommonModule 中定义的接口的实现由 MEF 容器通过程序集引用、文件目录扫描等方式进行解析,并根据需要构建依赖图。

    有没有什么体面的方法可以拥有这样的可选依赖项,而无需主应用程序访问可选依赖项的 dll?

    MainModule 不需要有StatusBarModule 的引用,只要将dll 部署到MainModule bin;然后可以构建一个 DirectoryCatalog 来组成 MEF 容器。当您使用“插件”时,无论如何您都希望使用 DirectoryCatalog 以便可以在运行时发现 dll。

    例如,根据给定的场景,解决方案结构应该类似于以下内容,它删除了除 CommonModule 之外的所有对 MainModule 的引用:

    Solution
    - MainModule
        - starts the app (console, winform, wpf, etc...)
        - references: CommonModule (no other project references)
    - CommonModule
        - references: no other modules
        - purpose: provides a set of common interfaces all projects can reference
        - note: the actual implementations will be in other projects
    - MyPluginModule
        - references: CommonModule
        - purpose: provides plugins for the app
        - Implements interfaces from CommonModule
        - note: uses dependencies defined by interfaces in CommonModule and implemented by other modules
        - build: build step should copy the dll to the MainModule's bin
    - StatusBarModule
        - references: CommonModule
        - purpose: provides plugin dependencies required by the application
        - note: uses dependencies defined by interfaces in CommonModule and implemented by other modules
        - build: build step should copy the dll to the MainModule's bin
    

    MyPluginModule 和 StatusBarModule 几乎相同,两者都没有相互引用;相反,它们通过 CommonModule 接口定义共享接口。依赖项/具体实现在运行时通过 DirectoryCatalog 解决。

    底层代码/实现如下。请注意,可选依赖项有 2 个选项,其中一个 (MyPlugin) 使用 .ctor 注入,而另一个 (MyOtherPlugin) 使用属性注入:

    主模块

    class Program
    {
        static void Main()
        {
            // will output 'Hi from MyPlugin!' when resolved.
            var myPlugin = MefFactory.Create<IMyPlugin>().Value;
    
            // will output 'Hi from MyOtherPlugin!' when resolved.
            var myOtherPlugin = MefFactory.Create<IMyOtherPlugin>().Value;
    
            Console.ReadLine();
        }
    }
    
    public static class MefFactory
    {
        private static readonly CompositionContainer Container = CreateContainer();
    
        public static Lazy<T> Create<T>()
        {
            return Container.GetExport<T>();
        }
    
        private static CompositionContainer CreateContainer()
        {
            // directory where all the dll's reside.
            string directory = AppDomain.CurrentDomain.BaseDirectory;
    
            var container = new CompositionContainer( new DirectoryCatalog( directory ) );
    
            return container;
        }
    }
    

    通用模块

    public interface IModule { }
    public interface IMyPlugin { }
    public interface IMyOtherPlugin { }
    public interface IStatusBarManager
    {
        void Display( string msg );
    }
    

    MyPluginModule

    [Export( typeof( IMyPlugin ) )]
    internal class MyPlugin : IModule, IMyPlugin
    {
        private readonly IStatusBarManager _manager;
    
        [ImportingConstructor]
        public MyPlugin( [Import( AllowDefault = true )] IStatusBarManager manager )
        {
            _manager = manager;
            if( _manager != null )
            {
                _manager.Display( "Hi from MyPlugin!" );
            }
        }
    }
    
    [Export( typeof( IMyOtherPlugin ) )]
    internal class MyOtherPlugin : IModule, IMyOtherPlugin
    {
        private IStatusBarManager _statusManager;
    
        [Import( AllowDefault = true )]
        public IStatusBarManager StatusManager
        {
            get { return _statusManager; }
            private set
            {
                _statusManager = value;
                _statusManager.Display( "Hi from MyOtherPlugin!" );
            }
        }
    }
    

    状态栏模块

    [Export( typeof( IStatusBarManager ) )]
    internal class StatusBarManager : IStatusBarManager
    {
        public void Display( string msg )
        {
            Console.WriteLine( msg );
        }
    }
    

    【讨论】:

    • 是否可以集成该方法使每个插件成为单个 .dll 文件?喜欢使用 fody.costura 或类似的东西。
    猜你喜欢
    • 2019-05-23
    • 2010-10-04
    • 2011-10-15
    • 2019-08-24
    • 2011-08-16
    • 2020-01-09
    • 2011-06-11
    • 2010-12-11
    • 2012-10-09
    相关资源
    最近更新 更多