2

I've got a lot of implementations of a specific generic interface and would like to resolve all of these implementations in the constructor. Of course, I can just add every implementation in the constructor. This will cause the constructor to have over 30 parameters, which isn't very nice to look and develop at.

While doing some research on the matter I discovered it's possible to inject an IEnumerable of an interface in the constructor, somewhat like this IEnumerable<IScriptExecution> scriptExecutionImplementations

This is what I'm searching for, but I need it to be like so:

public TheConstructor(IEnumerable<IScriptExecution<T>> scriptExecutionImplementations)
{}

Is it possible to inject all the IScriptExecution<T> in the constructor, using Autofac?

The following is an excerpt from my registration class:

builder.RegisterType<Library.ShardManager.Execution.DataScript.Users>().As<IScriptExecution<DBUser>>();
builder.RegisterType<Library.ShardManager.Execution.DataScript.Companies>().As<IScriptExecution<DBCompany>>();

Note: I don't think I need to use the Open Generics, because I already know the types and you can't use open generics in a constructor.

As requested by Jim Bolla in the comment section, an example on how I want to use this. At the moment I've got this constructor:

public MoveShardletData(Guid shardletId, ShardLocation oldLocation, ShardLocation newLocation, ILog log,
            IScriptCreator<DBAnalyticsInvitation> scriptAnalyticsInvitation, IScriptCreator<DBAnalyticsPromotion> scriptAnalyticsPromotion,
            IScriptCreator<DBAnalyticsRetailerSubscriptionModule> scriptAnalyticsRetailerSubscriptionModule, IScriptCreator<DBAnalyticsRetailerSubscription> scriptAnalyticsRetailerSubscription, //and the list goes on...

The piece of code where I want to use the IScriptCreator<T> objects looks a bit like this:

var analyticsInvitations = GetTheAnalyticsInvitationObjects(someLocalVariableInTheCurrentScope)
var script = scriptAnalyticsInvitation.Insert(analyticsInvitations);
sb.Append(script);
//With all IScriptCreator<T> objects listed here in the same fashion.

It would be nice if this could be done in a loop. Because of the someLocalVariableInTheCurrentScope it's a bit hard to extract this piece of code. The collection analyticsInvitations is the type T. If I can access the Autofac container I can probably resolve the correct IScriptCreator<T> manually, but that's cheating thus not advised.

Community
  • 1
  • 1
Jan_V
  • 4,244
  • 1
  • 40
  • 64
  • Managed Extensibility Framework would suit your need much better. – Darek Jan 05 '15 at 17:06
  • 3
    `IScriptExecution` is a separate type from `IScriptExecution`, so you would not be able to inject them in a single `IEnumerable>` unless you gave them a common interface, perhaps something like `IEnumerable` – Grax32 Jan 05 '15 at 17:14
  • Thanks for you suggestion, @Grax. Problem in my case is I need the `` in one of the methods. There's already an interface `IScriptExecution` which contains the methods which don't use the `T`, so I can use the `IEnumerable` solution elsewhere. Thanks anyway. – Jan_V Jan 06 '15 at 09:01
  • Can you show how you plan on using those objects? Doing so might make it more apparent as to what a solution could be. – Jim Bolla Jan 06 '15 at 19:54

0 Answers0