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 );
}
}