Forwarded Types in Microsoft Unity

How to use one component for several services in Unity Framework?

In Windsor, it is configured as follows:

var container = new WindsorContainer();
container.Register(Component.For<Service1, Service2>()
                            .ImplementedBy<Component>());

var service1 = container.Resolve<Service1>();
var service2 = container.Resolve<Service2>();

The idea with redirected types is that if the component is singleton service1and service2 it is the same instance .

+5
source share
2 answers

This test passes:

[Fact]
public void ContainerCorrectlyForwards()
{
    var container = new UnityContainer();
    container.RegisterType<IService1, MyComponent>(
        new ContainerControlledLifetimeManager());
    container.RegisterType<IService2, MyComponent>(
        new ContainerControlledLifetimeManager());

    var service1 = container.Resolve<IService1>();
    var service2 = container.Resolve<IService2>();

    Assert.Same(service1, service2);
}
+3
source

AFAIK, you cannot: you must register each service with the same displayed component:

container.RegisterType<Service1, Component>(new ContainerControlledLifetimeManager())
         .RegisterType<Service2, Component>(new ContainerControlledLifetimeManager())
0
source

All Articles