how do I register two different interfaces in Unity with the same instance... Currently I am using
_container.RegisterType
The proper way of doing singleton with multiple interfaces is as follows:
_container.RegisterType<EventService>(TypeLifetime.Singleton); <- This could be an instance
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IOtherEventService, EventService>();
You need to register a singleton and all the mappings to it separately. Unity v6 will have a registration method to do it all at once.
Edit
After some feedback in the comments I've decided that Sven's answer is a much superior answer. Thanks to Chris Tavares for pointing out the technical merits.
That's pretty much the only way to do it.
You could modify it slightly (I hate RegisterType with the same type for each generic parameter):
EventService es = _container.Resolve<EventService>();
_container.RegisterInstance<IEventService>(es);
_container.RegisterInstance<IEventServiceInformation>(es);
If one or more of your IoC children is going to request the concrete EventService type (hopefully not) you'd add one more RegisterInstance of type RegisterInstance<EventService>. Hopefully you don't need that and all of the dependent objects are asking for an IEventService, rather than an EventService.
Hope this helps, Anderson
The adaptor approach seemed to bulky for such a simple thing so I looked a bit further. To get around the problem with the named instances you need to register the type, and register factories for the interfaces.
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);
This way you do not need to create an instance of the concrete class (at registration) which was not possible in my case due to missing dependencies.
One solution that can also work for named instances is to use the adapter pattern to create throwaway adapters to the interface that wrap around the singleton instances. Then resolved instances will always be directed to the singleton instance, event if they are resolved using ResolveAll. This helps when having a heap of services that implement a generic interface like IStartable or something.
public class EventServiceAdapter<T> : IEventService where T : IEventService
{
private readonly T _adapted;
EventServiceAdapter(T adapted)
{
_adapted = adapted;
}
public string SomeMethod()
{
return _adapted.SomeMethod();
}
}
Then register the interface adapter around your registered singleton type.
_container
.RegisterType<EventService>(new ContainerControlledLifetimeManager())
.RegisterType<IEventService, EventServiceAdapter<EventService>>("namedEventService");
You can then hide any singleton behind any number of interfaces, and work using both Resolve and ResolveAll.
[2nd Edit]
Because of breaking changes in the way Unity handles registrations, the updated approach does not work anymore. The [Original answer] is the way to go again. (For more details about the changes in Unity, please refer to the link given in the comments below.)
[Edit]
The solution for doing this via XML configuration can be found here. Based on that answer I would propose a streamlined code-only approach as follows:
_container.RegisterType<IEventService, EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventServiceInformation, EventService>(new ContainerControlledLifetimeManager());
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
This way, the EventService class itself is not published by the container. As the class should be considered an implementaion detail, this is the preferable approach.
[Original answer]
A little late an answer, but should do the trick:
_container.RegisterType<EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IEventServiceInformation, EventService>();
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());