Mapping multiple interfaces to same singleton instance
Hi all,
I'm trying to use RL2 to make sure that multiple interfaces, when injected, map to the same singleton instance. Sometimes for data hiding purposes, sometimes for polymorphism purposes.
Let's say I have an IFruit interface and an IApple interface extending IFruit, and an Apple class implementing IApple. Most of the time I'm just interested in injecting IFruit, but sometimes I want to use IApple. In both cases I want the same instance injected.
I currently do it as follows:
injector.map(IApple).toSingleton(Apple);
var appleProvider:DependencyProvider = injector.map(IApple).getProvider();
injector.map(IFruit).toProvider(appleProvider);
Is this the way to go? It feels rather complex and I'm not even sure what happens exaclty. If anyone can shed some light on this, would be much appreciated.
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by creynders on 16 May, 2013 08:09 AM
Hi, I'm sorry, your post got dumped into the spam queue.
If I remember correctly that's the only way to do it. Could be I'm wrong though, don't have to time right now to look into it. Someone'll get back to you on this!
Support Staff 2 Posted by Shaun Smith on 24 May, 2013 06:44 PM
I think that might be the only way. Maybe a feature request for Swiftsuspenders is in order?
Support Staff 3 Posted by creynders on 24 May, 2013 08:48 PM
Yeah, a convenience method would be nice.
Of course were we to reintroduce the IInjector+wrapper, we could solve it ourselves.
Support Staff 4 Posted by Ondina D.F. on 18 Jul, 2013 03:00 PM
see: https://github.com/robotlegs/robotlegs-framework/issues/142
Ondina D.F. closed this discussion on 18 Jul, 2013 03:00 PM.