Brent Arias
Brent Arias

Reputation: 30165

Unity Injection of Arrays - What is the Order?

I see Unity can indeed inject an array of objects. Fantastic.

What I'd like to know is what Unity guarantees, if anything, about the order of the array objects that are injected? Is it:

Finally, I'd like to know if it is still true that a default (unnamed) component still cannot be injected as an array element.

Upvotes: 0

Views: 151

Answers (1)

TylerOhlsen
TylerOhlsen

Reputation: 5578

Short Answer

The order the elements were registered. Unnamed types will be filtered out of the list.

Long Answer

I do not see anywhere in the documentation that says one way or the other. This tells me that there is no "guarantee" from a design standpoint. Because there is no guarantee from a design standpoint, I would assume it is subject to change.

Going by the code...

All injection methods behind the scenes call IUnityContainer.ResolveAll(Type, params ResolverOverride[]), so all injection methods will result in the same order. (i.e. constructor injection, parameter injection, direct call to ResolveAll)

ResolveAll does the work. The order is defined by the order the elements were registered within a single container.

Also consider the case of parent/child containers. The order will be recursively the root container first, then each child down to the requested container. Also, if there is a duplicate registered name between two containers, the top most parent registration will be the only one resolved.

This is my own analysis of the source.

Upvotes: 2

Related Questions