Skip to content

Autofac

Marty Mathis edited this page Jan 20, 2019 · 2 revisions

Install via NuGet first: Install-Package PipelineFramework.Autofac

The recommended approach to using this package is leveraging Autofac Modules. The package provides an abstract module class to public abstract class PipelineModuleBase : Module. If you plan to use the module approach, you should inherit from this class and implement your pipeline dependency registrations here. For example:

public class PipelineModule : PipelineModuleBase
{
    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);

        builder.RegisterType<Component1>()
            .Named<IAsyncPipelineComponent<ExamplePipelinePayload>>(typeof(Component1).Name);
        builder.RegisterType<Component2>()
            .Named<IAsyncPipelineComponent<ExamplePipelinePayload>>(typeof(Component2).Name);
        builder.RegisterType<Component3>()
            .Named<IAsyncPipelineComponent<ExamplePipelinePayload>>(typeof(Component3).Name);

        builder.RegisterInstance(
            new[]
            {
                typeof(Component1),
                typeof(Component2),
                typeof(Component3)
            })
            .As<IEnumerable<Type>>();

        builder.RegisterInstance(new Dictionary<string, IDictionary<string, string>>())
            .As<IDictionary<string, IDictionary<string, string>>>();

        builder.Register(context =>
                new AsyncPipeline<ExamplePipelinePayload>(
                    context.Resolve<IPipelineComponentResolver>(),
                    context.Resolve<IEnumerable<Type>>(),
                    context.Resolve<IDictionary<string, IDictionary<string, string>>>()))
            .As<IAsyncPipeline<ExamplePipelinePayload>>();
    }
}

Once the registrations are completed, an Autofac container can be used to instantiate your pipeline. For example:

    var builder = new ContainerBuilder();

    builder.RegisterModule<PipelineModule>();
    var container = builder.Build();

    var pipeline = container.Resolve<IAsyncPipeline<ExamplePipelinePayload>>();
Clone this wiki locally