【发布时间】:2012-03-07 03:52:55
【问题描述】:
我们使用统一作为 IoC。我们遇到了独特的问题。 我们创建了名为 IPlugin 的接口。此接口在各个第三方供应商之间共享,以基于此接口开发自己的插件。这些插件然后适合我们的系统。 供应商将提供他们的插件作为 dll。我们想要的是, 使用统一,我们想要解析使用 IPlugin 接口实现的所有程序集类型。我知道这是可以通过 MEF 导出属性实现的,我想知道这是否可以通过 Unity 使用一些简短的扩展来实现。
我们的代码
Public interface IPlugin
{
Void ProcessData();
}
Public class DataProcessor
{
Var pluginList = unityContainer.ResolveAssemblies<IPlugIn>()
/*
There is no such method in unity but what we want is scan all assemblies in bin folder and load all types which are inheriting from IPlugIn
*/
}
厂商组装
Public class AbcCompanyPlugIn : IPlugin
{
Void ProcessData()
{
// some code
}
}
Public class XyzCompanyPlugIn : IPlugin
{
Void ProcessData()
{
// some code
}
}
【问题讨论】:
-
这是 MEF 的主要用例之一,您可以考虑将它用于应用程序的插件部分(同时保留 Unity 以满足您的 IoC 需求。)跨度>
-
这不是 Unity 或其他 IoC 框架的设计目标。因此,如果您愿意,请按照@dlev 的建议坚持使用 MEF。
-
@LexLi 我不同意这不是 DI 容器工作的一部分。它们旨在组装松散耦合的应用程序。这与加载插件并将它们添加到应用程序有什么不同?
-
@SebastianWeber,或者我应该说“简单”的容器,因为 MEF 列在复杂的下面,en.wikipedia.org/wiki/Dependency_injection
-
@LexLi 而一个简单的容器是......? Funq 之类的东西主要是为在移动/嵌入式场景中使用而设计的?
标签: c# inversion-of-control unity-container