我如何在同一个实例中注册两个不同的接口...
目前我正在使用
_container.RegisterType<EventService, EventService>(new ContainerControlledLifetimeManager());
_container.RegisterInstance<IEventService>(_container.Resolve<EventService>());
_container.RegisterInstance<IEventServiceInformation>(_container.Resolve<EventService>());
哪个可行,但看起来不太好..
所以,我想你明白了。 EventService实现了两个接口,如果我解析这些接口,我希望引用同一个对象。
克里斯
[第二次编辑]
由于Unity处理注册方式的重大变化,更新的方法不再起作用。 [原始答案]是再次走的路。 (有关Unity中更改的更多详细信息,请参阅下面的注释中给出的链接。)
[编辑]
通过XML配置执行此操作的解决方案可在此处找到。 基于该答案,我将提出一种简化的仅代码方式,如下所示:
_container.RegisterType<IEventService, EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventServiceInformation, EventService>(new ContainerControlledLifetimeManager());
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
这样,EventService类本身不会由容器发布。由于应将类视为实现细节,因此这是首选方法。
[原始回答]
答案迟了一点,但应该可以解决:
_container.RegisterType<EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IEventServiceInformation, EventService>();
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
编辑 [短码网_DuanMa.NET]
在评论中获得一些反馈后,我认为Sven的答案是一个更好的答案。感谢克里斯·塔瓦雷斯(Chris Tavares)指出技术优点。
那几乎是唯一的方法。
您可以对其进行一些修改(我讨厌每个通用参数都具有相同类型的RegisterType):
EventService es = _container.Resolve<EventService>();
_container.RegisterInstance<IEventService>(es);
_container.RegisterInstance<IEventServiceInformation>(es);
如果您的IoC子级中的一个或多个要请求具体的
希望这可以帮助,
安德森
对于这种简单的事情,适配器方法似乎体积很大,因此我进一步研究了一下。要解决命名实例的问题,您需要注册类型,并注册接口的工厂。
InjectionFactory factory = new InjectionFactory(x => x.Resolve<SimulationService>());
this.Container.RegisterType<SimulationService>(new ContainerControlledLifetimeManager());
this.Container.RegisterType<IContentProvider>("SimulationContentProvider", factory);
this.Container.RegisterType<ISimulationService>(factory);
这样,您无需创建具体类的实例(在注册时),在我的情况下由于缺少依赖项而无法创建实例。
使用多个接口进行单例的正确方法如下:
_container.RegisterType<EventService>(TypeLifetime.Singleton); <- This could be an instance
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IOtherEventService, EventService>();
您需要分别注册一个单例及其所有映射。 Unity v6将有一种注册方法可以一次完成所有操作。
一种也适用于命名实例的解决方案是使用适配器模式为包装围绕单例实例的接口创建一次性适配器。然后,如果使用ResolveAll解析实例,则将始终将其定向到单例实例。当拥有大量实现通用接口(如IStartable或诸如此类)的服务时,这会有所帮助。
public class EventServiceAdapter< T > : IEventService where T : IEventService
{
private readonly T _adapted;
EventServiceAdapter(T adapted)
{
_adapted = adapted;
}
public string SomeMethod()
{
return _adapted.SomeMethod();
}
}
然后在您注册的单例类型周围注册接口适配器。
_container
.RegisterType<EventService>(new ContainerControlledLifetimeManager())
.RegisterType<IEventService, EventServiceAdapter<EventService>>("namedEventService");
然后,您可以将任何单例隐藏在任意数量的接口后面,并同时使用Resolve和ResolveAll。
以上就是短码网小编为大家整理的《Unity将两个接口注册为一个单例》相关内容,希望大家喜欢。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!
《Unity将两个接口注册为一个单例》文档下载仅供参考学习,下载后请在24小时内删除。
转载注明出处:https://www.duanma.net/article/3c0f59bc862.html