Dependency injection 注册使用MEF和autofac导出的所有模块

Dependency injection 注册使用MEF和autofac导出的所有模块,dependency-injection,autofac,mef,Dependency Injection,Autofac,Mef,使用Autofac.Mef扩展名,我想注册导出的模块。有办法吗 出口 有一本关于这种方法的很好的著作。方法是使用委托作为工厂来创建模块,而不是导出IModule类型 用法 有关完整的详细信息,请参阅帖子 此外,您可能希望查看以下链接: [Export(typeof(IModule))] public class MyModule : Module { protected override void Load(ContainerBuilder builder) {

使用Autofac.Mef扩展名,我想注册导出的模块。有办法吗

出口
有一本关于这种方法的很好的著作。方法是使用委托作为工厂来创建模块,而不是导出IModule类型

用法 有关完整的详细信息,请参阅帖子

此外,您可能希望查看以下链接:

[Export(typeof(IModule))]
public class MyModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<SampleJob>().AsSelf();
    }
}  
var catalog = new DirectoryCatalog(".");
builder.RegisterComposablePartCatalog(catalog); //This would register all exported types.
builder.RegisterModule(//All IModules registerd as that type I want to register as Modules)
using Autofac;

/// <summary>
/// Creates an <see cref="IModule" /> that's responsible for loading
/// dependencies in the specified context.
/// </summary>
/// <param name="mode">The mode the application is running in, let's the implementor
/// of the factory load different dependenices for different modes.</param>
/// <returns>An <see cref="IModule" />.</returns>
public delegate IModule ModuleFactory(ApplicationMode mode);


/// <summary>
/// Describes different modes an application can run in.
/// </summary>
public enum ApplicationMode
{
    /// <summary>
    /// The application is running in a test environment.
    /// </summary>
    Test = 0,

    /// <summary>
    /// The application is running in a staging environment.
    /// </summary>
    Staging = 1,

    /// <summary>
    /// The application is running in production environment.
    /// </summary>
    Production = 2
}
using Autofac.Builder;
using Autofac;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Collections.Generic;
using System;


/// <summary>
/// An <see cref="Autofac.Builder.Module" /> is composed by with the help of
/// the System.ComponentModel.Composition (MEF) framework.
/// </summary>
public class ComposedModule
    : global::Autofac.Builder.Module
{
    #region Fields
    private ApplicationMode mode;
    private ComposablePartCatalog catalog;
    [Import(typeof(ModuleFactory))]
    private IEnumerable<ModuleFactory> RegisteredModuleFactories; 
    #endregion

    #region Construction
    /// <summary>
    /// Creates a new ComposedModule using the specified catalog to
    /// import ModuleFactory-instances.
    /// </summary>
    /// <param name="mode">The mode the application is running in.</param>
    /// <param name="catalog">The catalog used to import ModuleFactory-instances.</param>
    public ComposedModule(ApplicationMode mode, ComposablePartCatalog catalog)
    {
        if (catalog == null) throw new ArgumentNullException("catalog");

        this.mode = mode;
        this.catalog = catalog;

        this.ImportFactories();
    }

    /// <summary>
    /// Creates a new ComposedModule that loads all the ModuleFactories
    /// exported in assemblies that exists in the directory specified in 
    /// the <param name="modulesDirectoryPath" />-parameter.
    /// </summary>
    /// <param name="mode">The mode the application is running in.</param>
    /// <param name="catalog">The catalog used to import ModuleFactory-instances.</param>
    public ComposedModule(ApplicationMode mode, string modulesDirectoryPath)
        : this(mode, new DirectoryCatalog(modulesDirectoryPath)) { } 
    #endregion

    #region Methods
    private void ImportFactories()
    {
        var batch = new CompositionBatch();
        batch.AddPart(this);

        var container = new CompositionContainer(this.catalog);
        container.Compose(batch);
    }

    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);

        foreach (var factory in this.RegisteredModuleFactories)
        {
            var module = factory(this.mode);
            builder.RegisterModule(module);
        }
    } 
    #endregion
}
public class MyModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);

        builder.RegisterType<SampleJob>().AsSelf();
    }

    [Export(typeof(ModuleFactory))]
    public static ModuleFactory Factory = x => new MyModule();
}