问题描述
我使用的是从在MEF2不再使用懒< IExtension,IExtensionMetadata>
,但现在你必须提供元数据视图具体类型(也可能使用ExportFactory<>而不是懒惰<?>)。
I've read that in MEF2 you no longer use Lazy<IExtension, IExtensionMetadata>
, but now you must provide a concrete type for the metadata view (and possibly use ExportFactory<> instead of Lazy<> ?).
不过,我无法找到如何这应该一切工作的例子 - 短短提到用一个具体类型,而不是一个接口的
However, I can't find any examples of how this should all work - just a few mentions of using a concrete type instead of an interface.
我已经尝试了一些东西,但不断收到以下错误 - 为帐户ID缺少,并没有提供缺省值导出的元数据的
I've tried a few things, but keep getting the following error - "Export metadata for 'AccountID' is missing and no default value was supplied".
我的code ...
My code...
创建容器(在Global.asax中或App_Start文件夹):
Creating the container (in Global.asax or App_Start folder):
// Get assemblies that will be providing imports and exports
var assemblies = GetAssemblies();
// Get conventions that will be used to find imports and exports
var conventions = GetConventions();
var container = new ContainerConfiguration().WithAssemblies(assemblies, conventions).CreateContainer();
// Create and apply a MefControllerFactory so controllers can be composed
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
GetConventions()方法:
GetConventions() method:
private static ConventionBuilder GetConventions()
{
var conventionBuilder = new ConventionBuilder();
conventionBuilder.ForTypesDerivedFrom<IController>().Export();
conventionBuilder.ForTypesDerivedFrom<IExtension>().Export<IExtension>();
conventionBuilder.ForTypesMatching(t => t.Namespace != null && t.Namespace.EndsWith(".Parts")).Export().ExportInterfaces();
return conventionBuilder;
}
IExtension.cs:
IExtension.cs:
public interface IExtension
{
void DoWork();
}
ExtensionMetadata.cs:
ExtensionMetadata.cs:
public class ExtensionMetadata
{
public int AccountID { get; set; }
}
ExtensionA.cs(同ExtensionB.cs):
ExtensionA.cs (same as ExtensionB.cs):
public void DoWork()
{
System.Diagnostics.Debug.WriteLine("ExtensionA doing work..");
}
ExtensionManager.cs:
ExtensionManager.cs:
public class ExtensionManager
{
private IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> _extensions;
public ExtensionManager(IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> extensions)
{
_extensions = extensions;
}
public void DoWork(int accountID)
{
foreach (var extension in _extensions)
{
if (extension.Metadata.AccountID == accountID)
{
extension.DoWork();
}
}
}
}
我想我失去了一些东西很重要的位置。基本上我想懒洋洋地导入所有扩展,检查他们的元数据,并且如果条件满足有扩展做一些事情。
I think I'm missing something quite major here. Basically I want to lazily import all Extensions, check their metadata and if a condition is fulfilled have that extension do something.
真的AP preciate您的意见或来样code /教程,涵盖我的方案的任何链接。
Would really appreciate your feedback or any links to sample code / tutorials that cover my scenario.
非常感谢!
推荐答案
我想我已经看完this SO质疑。
我创建了一个元数据属性:
I created a Metadata Attribute:
[MetadataAttribute]
public class ExtensionMetadataAttribute : ExportAttribute, IExtensionMetadata
{
public int AccountID { get; set; }
public ExtensionMetadataAttribute(int accountID) : base(typeof (IExtension))
{
AccountID = accountID;
}
}
然后修改ExtensionA.cs:
Then modified ExtensionA.cs:
[ExtensionMetadata(1)]
public class ExtensionA : IExtension
{
public void DoWork()
{
System.Diagnostics.Debug.WriteLine("ExtensionA doing work..");
}
}
现在ExtensionManager.cs看起来是这样的:
And now ExtensionManager.cs looks like this:
public class ExtensionManager : IExtensionManager
{
private readonly IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> _extensions;
public ExtensionManager(IEnumerable<ExportFactory<IExtension, ExtensionMetadata>> extensions)
{
_extensions = extensions;
}
public void DoWork(int accountID)
{
foreach (var extension in _extensions)
{
if (extension.Metadata.AccountID == accountID)
{
using (var foo = extension.CreateExport())
{
foo.Value.DoWork();
}
}
}
}
}
这似乎这样的伎俩,但我仍然有兴趣在任何反馈重的最佳实践,性能问题等。
This seems to do the trick, but I would still be interested in any feedback re best practices, performance issues etc.
谢谢!
这篇关于在MEF2强类型的元数据(System.Composition)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!