Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving array types in a Unity (Prism) container

Is it possible to register and resolve array types in a Unity container? I'd like to do something like this:

this.mContainer
    .RegisterType<ISomeType, SomeType>()
    .RegisterType<ISomeType[], SomeType[]>();
ISomeType[] lSomeTypes = this.mContainer.Resolve<ISomeType[6]>();

It would be even better if I didn't have to register the array type, and have Unity figure out the array based on RegisterType<ISomeType, SomeType>() and Resolve<ISomeType[]>() alone.

like image 904
sourcenouveau Avatar asked Feb 07 '11 15:02

sourcenouveau


1 Answers

If you register multiple types for a particular type (using named registrations), then when the container sees a dependency on an array of that type, it'll automatically inject all the named registrations.

So this will work:

this.mContainer
  .RegisterType<ISomeType, SomeImpl1>("one")
  .RegisterType<ISomeType, SomeOtherImpl>("other")
  .RegisterType,ISomeType, AnotherImpl>("another");

ISomeType[] someTypes = mContainer.Resolve<ISomeType[]>();

This logic will kick in whenever there's a dependency of ISomeType[] - constructor parameter, injected property, etc.

Note that the array injection will only inject named registrations. The default, unnamed registration is not included in the array.

like image 95
Chris Tavares Avatar answered Oct 13 '22 19:10

Chris Tavares