Matus
Matus

Reputation: 437

RegisterManySingleton as named instances

I want to inject all registered implementaions of a base interface as a collection, but I also want to resolve them as more concrete interface implementations.

For the collection resolving to work, I need to register them as named instances

To be able to resolve them by base or derived interface, I need to use RegisterManySingleton

Is there a way to combine these 2 requirements?

something like

containerRegistry.RegisterManySingleton<ServiceOne>(typeof(IInitialize), typeof(IServiceOne), "one");
containerRegistry.RegisterManySingleton<ServiceTwo>(typeof(IInitialize), typeof(IServiceTwo), "two");

usage:

public interface IInitialize
{
  void Initialize();
}

public interface IServiceOne : IInitialize
{
}

public interface IServiceTwo : IInitialize
{
}

public class Initializer
{
  public Initializer(IInitialize[] services)
  {
  }
}

public class Foo
{
  public Foo(IServiceOne service)
  {
  }
}

Upvotes: 0

Views: 27

Answers (1)

Dan Siegel
Dan Siegel

Reputation: 5799

First of all Unity Container is a terrible choice since it's end of life and missing a number of features which is why it's been dropped from active support for all but WPF which is only to help maintain legacy apps.

Second while DryIoc actually uses an internal API that doesn't work this way, the other Prism containers do actually replicate the behavior by registering the concrete type as a Singleton and then resolving that concrete singleton for the interfaces, so it's already doing what you're asking.

Upvotes: 0

Related Questions