[英]Using MEF with the Enterprise Library
我试图覆盖企业库的默认Unity容器行为,以便可以使用我的MEF容器。 有一些资源提供了有关如何执行此操作的说明,但我只是没有得到它:
SO上也有此帖子,但由于LogWriter
受保护,因此代码无法编译。 我认为这是一个旧版本:
我了解的是,我需要对我的MEF容器使用CommonServiceLocator
,然后将其附加到企业库容器。 这是我的容器配置器的功能:
public class MefContainerConfigurator : IContainerConfigurator, IServiceLocator
{
[Import] private CatalogExportProvider provider;
public object GetService(Type serviceType)
{
throw new NotImplementedException();
}
public object GetInstance(Type serviceType)
{
return provider.GetExportedValue<Type>();
}
public object GetInstance(Type serviceType, string key)
{
throw new NotImplementedException();
}
public IEnumerable<object> GetAllInstances(Type serviceType)
{
throw new NotImplementedException();
}
public TService GetInstance<TService>()
{
return provider.GetExportedValue<TService>();
}
public TService GetInstance<TService>(string key)
{
return provider.GetExportedValue<TService>(key);
}
public IEnumerable<TService> GetAllInstances<TService>()
{
return provider.GetExportedValues<TService>();
}
public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
{
throw new NotImplementedException();
}
}
在我的引导程序中:
var configurator = new MefContainerConfigurator();
// Does this line read the Enterprise Library configuration from the app.config?
IConfigurationSource cs = new SystemConfigurationSource();
EnterpriseLibraryContainer.ConfigureContainer(configurator, cs);
我认为也许我需要利用LogWriterImpl
和ExceptionManagerImpl
类,因为它们具有接受配置的构造函数。 我现在的问题是:
IConfigurationSource
检索配置并将其馈送到LogWriterImpl
和ExceptionManagerImpl
构造函数的构造函数中? EnterpriseLibraryContainer.ConfigureContainer
在我的MefContainerConfigurator
调用RegisterAll
。 我应该在这里将所有企业库类型注册到容器中吗? IServiceLocator
接口中的方法; 我找不到使用这些方法从容器中返回对象的方法。 我是否应该让它们保持未实现状态,而改用通用方法? 编辑
我仍然无法完全正确。 基于@克里斯·塔瓦雷斯回答,我写我的RegisterAll
方法在MefContainerConfigurator
通过TypeRegistrations迭代,并将它们添加到容器中。 我一辈子都无法弄清楚如何将它们合并到在Bootstrapper
类中创建的AggregateContainer
,以便实际上可以在ContainerConfigurator
之外使用这些导出:
public void RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
{
var registrations = rootProvider.GetRegistrations(configurationSource);
foreach (var type in registrations)
{
var builder = new RegistrationBuilder();
builder.ForType(type.ServiceType).Export();
var cat = new AssemblyCatalog(type.ServiceType.Assembly, builder);
var container = new CompositionContainer(cat);
container.ComposeParts(this);
}
}
在Prism引导程序中配置ConfigureAggregateCatalog:
protected override void ConfigureAggregateCatalog()
{
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly));
// Module assemblies
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(DataEntryModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ReportingModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(StatusBarModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SplashScreenModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(WelcomeModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(AdministrationModule).Assembly));
AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
}
您的容器配置器严重不完整。 您有一种方法可以实现:RegisterAll,但是您没有实现它。
您不必从config读取原始配置信息; 相反,发生的是,当您调用EnterpriseLibraryContianer.ConfigureContainer时,Entlib将遍历配置源,挑选出所有重要的位,并为您提供一系列TypeRegistration对象。 这些家伙为您提供类型映射,构造函数依赖关系等。基本上,您需要在容器中注册依赖关系的所有内容(在本例中为MEF)。
因此,基本上,您需要做的是编写将抽象TypeRegistration对象转换为MEF正确配置的代码。
在这方面团结不是特别的。 它也具有配置器,并且遵循完全相同的过程,因此您可以查看entlib代码,作为您需要做的事情的示例。
我根本不了解MEF api,因此不幸的是无法帮助您实现特定的实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.