简体   繁体   English

MEF导出存储库 <T> :IRepository <T>

[英]MEF export of Repository<T> : IRepository<T>

I am trying to use MEF to Export the following: 我正在尝试使用MEF导出以下内容:

[Export(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
    where T : class
{

With an import of 随着进口

[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;

But I keep getting an error message when composing MEF of: 但是在撰写MEF时,我不断收到错误消息:

========================================= =========================================

The composition remains unchanged. 构图保持不变。 The changes were rejected because of the following error(s): The composition produced a single composition error. 由于以下错误,更改被拒绝:组合产生单个组合错误。 The root cause is provided below. 根本原因如下。 Review the CompositionException.Errors property for more detailed information. 查看CompositionException.Errors属性以获取更多详细信息。

1) No valid exports were found that match the constraint '((exportDefinition.ContractName = "Interfaces.IRepository()") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "Interfaces.IRepository()".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected. 1)没有找到与约束'((exportDefinition.ContractName =“Interfaces.IRepository()”)&&(exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)&&“Interfaces.IRepository()”匹配的有效导出.Equals(exportDefinition) .Metadata.get_Item(“ExportTypeIdentity”))))',无效导出可能已被拒绝。

Resulting in: Cannot set import 'SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()")' on part 'SoCLINQ2SQL.RepositoryTest'. 导致:无法在部分'SoCLINQ2SQL.RepositoryTest'上设置导入'SoCLINQ2SQL.RepositoryTest.repository(ContractName =“Interfaces.IRepository()”)'。 Element: SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()") --> SoCLINQ2SQL.RepositoryTest 元素:SoCLINQ2SQL.RepositoryTest.repository(ContractName =“Interfaces.IRepository()”) - > SoCLINQ2SQL.RepositoryTest

To the best of my knowledge, and according to Glenn Block's post on the subject, MEF does not support open generic types "out of the box". 据我所知,根据Glenn Block关于这个主题的帖子 ,MEF不支持“开箱即用”的开放式通用类型。

Apparently there is support for it in the MEF contrib project . 显然,在MEF贡献项目中有它的支持。

I believe in that case you would be able to leave your export as an open generic type but on the import side you would need to change your import to look like: 我相信在这种情况下,您可以将导出保留为开放泛型类型,但在导入方面,您需要将导入更改为:

[Import(typeof(IRepository<Contact>))]
private IRepository<Contact> repository;

I had a similar problem, and it was related to the order on which the assemblies were added to the AggregateCatalog . 我遇到了类似的问题,它与程序集添加到AggregateCatalog的顺序有关。 The example below illustrates the Bootstrapper.ConfigureAggregateCatalog() . 下面的示例说明了Bootstrapper.ConfigureAggregateCatalog() "Module B" was invoking a service from "Module C" , but "Module C" wasn't added to the AggregatorCatalog yet. “模块B”正在调用“模块C”中的服务,但“模块C”尚未添加到AggregatorCatalog中 I just had to change the order, and it solved to problem. 我只需要改变顺序,它就解决了问题。

    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();


 // Be aware of the order on which the assemblies are added to the AggregateCatalog.
 // It's important to add the assembly to the AggregateCatalog in the correct order, otherwise
 // you may get the error "No valid exports were found that match the constraint".
 // In the example below, if Module B invokes a method of Module C, module C must be
 // added to the AggregateCatalog prior to Module B. 
 // Please note the Bootstrapper assembly also needs to be added to the AggregateCatalog.
 // -------------------------------------------------------------------------------------- 
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
        this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB).Assembly));

    }

Just a FYI: this is supported in the upcoming version. 仅供参考:即将推出的版本支持此功能。 A preview drop should be in our codeplex site soon. 预览下拉应该很快就会出现在我们的codeplex站点中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM