开发者

Forwarded Types in Microsoft Unity

How to use single component for multiple services in Unity Framework?

In Windsor it is configured in the following way:

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

var servi开发者_StackOverflow中文版ce1 = container.Resolve<Service1>();
var service2 = container.Resolve<Service2>();

The idea with forwarded types is that if the component is a singleton service1 and service2 are the same instance.


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);
}


AFAIK, you can't: you have to register each service with the same mapped component:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜