簡體   English   中英

將MEF與企業庫一起使用

[英]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);

我認為也許我需要利用LogWriterImplExceptionManagerImpl類,因為它們具有接受配置的構造函數。 我現在的問題是:

  • 如何從IConfigurationSource檢索配置並將其饋送到LogWriterImplExceptionManagerImpl構造函數的構造函數中?
  • EnterpriseLibraryContainer.ConfigureContainer在我的MefContainerConfigurator調用RegisterAll 我應該在這里將所有企業庫類型注冊到容器中嗎?
  • 我保留為NotImplemented的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM