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.