Microsoft Unity中转发的类型

Dmi*_*ich 5 .net castle-windsor unity-container

如何在Unity Framework中将单个组件用于多个服务?

在Windsor中,它按以下方式配置:

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

var service1 = container.Resolve<Service1>();
var service2 = container.Resolve<Service2>();
Run Code Online (Sandbox Code Playgroud)

转发类型的想法是,如果组件是单例service1并且service2 是相同的实例.

Mar*_*ann 3

该测试通过:

[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);
}
Run Code Online (Sandbox Code Playgroud)