My goal is to constructor inject an array of objects implementing an interface.
The following is the way I currently have it.
Container
.RegisterInstance<Company>(ParseCompany(args[1])
.RegisterInstance<eTargets>(ParseTargets(args[2]))
.RegisterInstance<ILoader[]>(new ILoader[] {
Container.Resolve<CustomerLoader>(),
Container.Resolve<PaymentLoader(),
Container.Resolve<InvoiceLoader()
});
Is it typical to call Resolve in container configuration this way or is there a more standard way to accomplish the same thing?
Unity natively understands arrays, so there’s no reason to make it so complicated. Just register the
ILoadersyou want to include and resolve the object graphs normally. Auto-wiring will take care of the rest:assuming that the
MyConsumerconstructor is defined like this:However, you should be aware that (for some unfathomable reason) Unity only includes named components in this way. The default component:
will not be included in the array, since it has no name.