我在嘗試覆蓋企業庫的默認Unity容器行爲,以便可以使用我的MEF容器。有一個如何做到這一點的解釋一些資源,但我只是沒有得到它:在企業庫中使用MEFE
- http://blogs.msdn.com/b/bobbrum/archive/2009/06/23/enterprise-library-5-0-some-architecture-changes.aspx
- http://entlib.uservoice.com/forums/90505-silverlight-integration-pack/suggestions/1284693-mef-configurator-for-enterprise-library-container
- http://entlib.codeplex.com/discussions/261443
還有對左右,但這個職位代碼不會編譯,因爲LogWriter
受保護。我認爲這指的是舊版本:
我的理解是,我需要使用CommonServiceLocator
我的MEF容器,然後附加至企業庫容器。下面是我對我的容器配置:
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
構造?我的MefContainerConfigurator
。這是我應該將所有企業庫類型註冊到容器中的位置嗎? - 來自
IServiceLocator
接口的方法,我把它留作NotImplemented;我無法找到一種方法來使用它們從我的容器中返回對象。我是否應該讓他們沒有實現,並使用泛型方法呢?
編輯
我仍然不能得到這個完全正確的。基於@Chris Tavares的回答,我在MefContainerConfigurator
中編寫了我的RegisterAll
方法來迭代TypeRegistrations並將它們添加到容器中。我不能爲我的生活弄清楚如何將這些合併到我的AggregateContainer
是在我Bootstrapper
類創建,這樣我可以實際使用的這些出口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);
}
}
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方法。請參閱編輯修改後的問題,但我認爲您不能提供幫助,因爲它現在是MEF特有的。 – ChrisO 2013-02-28 10:17:35