Currently, Simple Injector has built-in support for injecting IEnumerable<T>
. There's currently no support for injecting arrays and
. The reasoning for not having arrays and IList<T>
is that users are not expected to have many dependencies on collections since they should use the composite pattern, and in case you need a dependency on a list, you want
to use the most abstract type possible. As explained
Downside of this decision however is that people will use
this extension method
, but in case there's a consumer of such list that is registered with a lifestyle bigger than transient, using that extension method will lead to a lifestyle mismatch warning of the
, stating something like:
Consumer (Singleton) depends on ILogger (Unknown).
The warning is reasonable, because while IEnumerable<T>
is a stream which will call back into the container every time it is iterated, an array or list is just a one-time copy of that
that gets injected. This means that you need to be careful about the lifestyles of the items in the collection. And since the extension method can't safely determine the lowest lifestyle of the elements in the collection, it gets
the lifestyle 'Unknown', and this basically means that you'll get a warning every time there is a consumer with a lifestyle bigger than transient.
But this warning can result in false-positives, for instance when the collection only consists of singletons. This can cause users to discard the warning all together. This however, can lead to the situation where a real lifestyle-mismatch gets undetected,
when the collection starts to contain a transient.
So I'm wondering whether it would be good to allow the container to inject arrays and lists as well, because this would allow to integrate this with the diagnostic services, which prevents any false-positives and prevents users from discarding the warning all
I'm wondering what counter arguments there are for not supporting this.