How can I simplify the registration of a large set of closed generic versions of the same open generic implementation?

StackOverflow https://stackoverflow.com/questions/17795982

Question

Is there a fluent way of writing the following:

var someTypes = GetType()
    .Assembly
    .GetTypes()
    .Where(x => someFilter == true);

foreach(var someType in someTypes)
{
    var genericInterface = typeof(IFoo<>).MakeGenericType(someType);

    var genericImplementation = typeof(Foo<>).MakeGenericType(someType);

    container.Register(
       Component.For(genericInterface)
            .ImplementedBy(genericImplementation));
}
Was it helpful?

Solution 2

The following should to it

container.Register(
    Component.For(typeof(IFoo<>))
      .ImplementedBy(typeof(Foo<>))
);

OTHER TIPS

a IGenericServiceStrategy is what you're after (on top of what @maxlego said, which is correct)

See this for details and example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top