Pipeline of components

16 views
Skip to first unread message

fel...@bitwize.com

unread,
Jul 3, 2015, 2:00:23 AM7/3/15
to aut...@googlegroups.com
Hi,

In my application (Windows service), I use a concept of pipeline, which is basically an ordered IEnumerable<TService>.

(background: during application start, a set of deployed modules is loaded, each having a class derived from Autofac.Module, where the module contributes its components to the application - so far so good).

One kind of components being registered, are metadata conventions. After modules are loaded, conventions are resolved from the container as IEnumerable<TService> and executed in the order of enumeration. The order in which conventions are applied is important, and I need the ability to define it.

I searched for such a feature in Autofac, but all I found were a few suggestions in Stack Overflow. This one - http://stackoverflow.com/a/8602341/4544845 - inspired me to implement a couple of extension methods, which solved my problem, like this:

//-- arrange

var builder = new ContainerBuilder();

builder.RegisterPipeline<ITestComponent>();

builder.RegisterType<ComponentB>().As<ITestComponent>();

// each component can ask to be the first or the last in the pipeline, so far
// if B asks to be first, and then A asks to be first, the order will be A,B
// if C asks to be last, and then D asks to be last, the order will be C,D

builder.RegisterType<ComponentC>().As<ITestComponent>().LastInPipeline();
builder.RegisterType<ComponentA>().As<ITestComponent>().FirstInPipeline();

var container = builder.Build();

//-- act

var pipeline = container.ResolvePipeline<ITestComponent>();

//-- assert

Assert.That(pipeline1.Count, Is.EqualTo(3));
Assert.That(pipeline1[0], Is.InstanceOf<ComponentA>());
Assert.That(pipeline1[1], Is.InstanceOf<ComponentB>());
Assert.That(pipeline1[2], Is.InstanceOf<ComponentC>());


I thought it would be nice to have such feature integrated in Autofac out of the box. I attach sources of my implementation, and if it looks like the right thing to do, I'd be glad to submit a pull request through GitHub - of course after I convert it to Autofac's coding conventions.

Cheers,
Felix
AutofacExtensions.Pipeline.cs
Pipeline.cs
AutofacExtensionTests.cs
Reply all
Reply to author
Forward
0 new messages