Skip to content

LightInject

Marty Mathis edited this page Jan 20, 2019 · 1 revision

Install via NuGet first: Install-Package PipelineFramework.LightInject

The recommended approach to using this package is leveraging LightInject CompositionRoot. The package provides an abstract class which implements ICompositionRoot.

public abstract class PipelineCompositionRootBase : ICompositionRoot

If you plan to use the CompositionRoot approach, you should inherit from this class and implement your pipeline dependency registrations here. For example:

public class CompositionRoot : PipelineCompositionRootBase
{
    public override void Compose(IServiceRegistry registry)
    {
        //Inheriting from PipelineCompositionRootBase and calling base class Compose method
        //will register the LightInject container implementation of IPipelineComponentResolver automatically.
        base.Compose(registry);

        registry
            .Register<IAsyncPipelineComponent<ExamplePipelinePayload>, Component1>(typeof(Component1).Name)
            .Register<IAsyncPipelineComponent<ExamplePipelinePayload>, Component3>(typeof(Component2).Name)
            .Register<IAsyncPipelineComponent<ExamplePipelinePayload>, Component3>(typeof(Component3).Name)
            .Register<IEnumerable<Type>>(factory =>
                new[]
                {
                    typeof(Component1),
                    typeof(Component2),
                    typeof(Component3)
                })
            .Register<IDictionary<string, IDictionary<string, string>>>(factory => 
                new Dictionary<string, IDictionary<string, string>>())
            .Register<IAsyncPipeline<ExamplePipelinePayload>>(factory =>
                new AsyncPipeline<ExamplePipelinePayload>(
                    factory.GetInstance<IPipelineComponentResolver>(),
                    factory.GetInstance<IEnumerable<Type>>(),
                    factory.GetInstance<IDictionary<string, IDictionary<string, string>>>()));
    }
}

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

    var container = new ServiceContainer();
    container.RegisterFrom<CompositionRoot>();

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