Mapping multiple types to same singleton

mike.cann's Avatar

mike.cann

16 Oct, 2015 02:30 AM

Hi,

I would like to map multiple types to the same singleton. So for example something like this:

_injector.mapAll(SGDialogEngine, DialogEngine).toSingleton(SGDialogEngine);

SGDialogEngine extends DialogEngine.

Is this possible?

Cheers,
Mike

  1. Support Staff 1 Posted by Ondina D.F. on 16 Oct, 2015 11:16 AM

    Ondina D.F.'s Avatar

    Hi Mike,

    Do you want something similar to the viewProcessorMap?

    viewProcessorMap.mapMatcher(new TypeMatcher().anyOf(ISpaceShip, IRocket)).toProcessor(SpacecraftSkinner);

    _injector.mapAll(SGDialogEngine, DialogEngine).toSingleton(SGDialogEngine);

    That's not possible, as far as I know. There is no mapAll in Swiftsuspenders' Injector.
    You'd need to build your own extension that would add this functionality....

    Or just a simple method called mapAll which takes as arguments a DependencyProvider and a collection of classes to be mapped:

    var clazzes:Array = [AnotherClass, IDrawThings, ICountThings, OtherClass];
    var depProvider:DependencyProvider = injector.map(SomeClass).getProvider();
    
    mapAll(clazzes, depProvider);
    
    inside of mapAll you iterate over the clazzes and map them toProvider:
    
    injector.map(clazzes[i]).toProvider(depProvider);
    

    Or, you can use the new Injector's method toProviderOf instead of toProvider:

    injector.map(SomeClass).asSingleton();
    var clazzes:Array = [AnotherClass, IDrawThings, ICountThings, OtherClass];
    
    mapAll(clazzes, SomeClass);
    
    injector.map(clazzes[i]).toProviderOf(someClass);
    

    Actually, in your example you are mapping a derived class and the base class to the derived class. Is that right? Why do you need to do that?

    Ondina

  2. 2 Posted by mike.cann on 18 Oct, 2015 11:44 PM

    mike.cann's Avatar

    Hi Ondiana,

    Thanks so much for getting back to me, really appreciated and thanks for continuing support of RL, I have been using it for many years, had a break for a little while but back again now, really great to see someone continuing on supporting the community.

    As for this problem. Yes I am mapping the base and derived class to the same instance. The reason is because some parts of the code rely upon the base class type and some rely upon the derived class type.

    I suppose another solution would be to do something like this:

    ```
    _injector.map(SGDialogEngine).asSingleton();
    _injector.map(DialogEngine).toProvider(new SimpleProvider(function() : * {
    return _injector.getInstance(SGDialogEngine);
    }));
    ```

  3. Support Staff 3 Posted by Ondina D.F. on 19 Oct, 2015 02:02 PM

    Ondina D.F.'s Avatar

    Thank you for your appreciation, Mike!! Yeah, I'm trying to keep this forum alive, but now, without the combined brainpower of the great rl creator, contributors and users, the forum is not the same as it was years ago, of course.

    +1 to your solution

    If you think this is resolved, you can mark the discussion as such, or let me know if/when I can close it.
    Ondina

  4. 4 Posted by mike.cann on 19 Oct, 2015 11:47 PM

    mike.cann's Avatar

    Sure, im not sure how to mark it as resolved but I think it is..

  5. Support Staff 5 Posted by Ondina D.F. on 20 Oct, 2015 05:04 PM

    Ondina D.F.'s Avatar

    That's the "Close the discussion" link-button on the right of the screen. When a discussion gets closed, it appears as 'resolved' in the list of discussions

    Closing it now...

    See you on the other thread :)

  6. Ondina D.F. closed this discussion on 20 Oct, 2015 05:04 PM.

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