Skip to content

5.8.0.488

Compare
Choose a tag to compare
@ENikS ENikS released this 27 Mar 23:00

This release

This release fixes #72 issue.

Problem

Unity has build in support for IEnumerable, Array, and Lazy. Theoretically it should be able to distinguish these types and properly resolve them individually and in combination. Unfortunately it did not do so.

container.Resolve<Lazy<IEnumenrable<type>>>  - will resolve correctly
container.Resolve<IEnumenrable<Lazy<type>>>  - will resolve empty

Fix

This release fixes resolution of collections of generic and array types and Lazy collections of items. This will now work fine:

Resolve<Lazy<IEnumenrable<type>>>
Resolve<IEnumenrable<Lazy<type>>>

Resolve<Lazy<type[]>>
Resolve<Lazy<type>[]>

Resolve<IEnumerable<Lazy<Func<IService>>>>()
Resolve<IEnumerable<Func<Lazy<IService>>>>()

Resolve<Lazy<Func<IService>>[]>()
Resolve<Func<Lazy<IService>>[]>()

The logic behind resolving collections is to find enumerable type and get all registrations for it no matter how deep in generics tree. Enumerated type could be:

  • Non generic (Constructed Generic) type
  • Explicitly registered with container type

So, in this example

RegisterType<IService, Service>("1", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>("2", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>("3", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>();

Resolve<IEnumerable<Func<Lazy<IService>>>>();

enumerable will return four instances of method which returns Lazy<IService>. But in this example:

RegisterType<IService, Service>("1", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>("2", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>("3", new ContainerControlledLifetimeManager());
RegisterType<IService, Service>();

RegisterInstance(new Lazy<IService>(() => new Service()));  <-- note this registraton

Resolve<IEnumerable<Func<Lazy<IService>>>>();

Enumerable will return only one item.

For more information see this wiki