Injection by type/interface

thomas.auinger's Avatar

thomas.auinger

29 Nov, 2013 12:21 PM

I think the answer is no, yet I would like to ask: Is is possible to inject all mapped singletons that implement a certain interface into a target class (e.g. as an array)?

Or even better as a dynamic object with the mapped "name" as the key and the dependency as the value...?

Please?? ;)

  1. Support Staff 1 Posted by creynders on 29 Nov, 2013 12:38 PM

    creynders's Avatar

    That's definitely not possible, no... Sorry :)

    If I understand it correctly you have multiple singletons that implement
    the same interface and you've used named mappings in the injector, right?

    E.g.:

    map(IFoo, 'foo').toSingleton(Foo)
    map(IFoo, 'bar').toSingleton(Bar)

    etc.

    Why do you need access to all of them at once?

  2. Support Staff 2 Posted by Ondina D.F. on 29 Nov, 2013 01:29 PM

    Ondina D.F.'s Avatar

    I think Thomas means something like this:
    https://github.com/ninject/ninject/wiki/Multi-injection

    which would be cool to have.

  3. 3 Posted by thomas.auinger on 29 Nov, 2013 02:05 PM

    thomas.auinger's Avatar

    @creynders Yes, spot on.

    In fact the interface is named IEntityXmlParser and I have a service that needs access to a range of parsers (one per entity domain) since its loading a complex object graph, which is composed of a variety of entities which all need their own parser.

  4. Support Staff 4 Posted by Ondina D.F. on 29 Nov, 2013 02:44 PM

    Ondina D.F.'s Avatar

    If you mapped them as creynders said, you can iterate over an array of strings ('foo', 'bar'...) that correspond to the names used for the mapping, and let the injector get all the needed instances

    injector.getInstance(IFoo,str)

  5. Support Staff 5 Posted by creynders on 30 Nov, 2013 07:55 AM

    creynders's Avatar

    It seems to me this could be solved far more elegantly with a factory. Since these are parser classes they don't need to persist (I assume) and could be created as throw-away objects.
    I tend to avoid named injections whenever possible. (So far, I've never needed them)

  6. Support Staff 6 Posted by Ondina D.F. on 30 Nov, 2013 10:34 AM

    Ondina D.F.'s Avatar

    @creynders

    Yes, a factory is always a good choice.

    I was looking at IoC libraries for C# , like SimpleInjector, ninject.., and their ability to obtain a list of all the registered objects of a specific type.

    I'm just curious, what do you think would speak against having a mapAll (registerAll) and getAllInstances for multiple classes implementing the same interface in robotlegs, besides more work ;)

  7. Support Staff 7 Posted by creynders on 30 Nov, 2013 12:43 PM

    creynders's Avatar

    > I'm just curious, what do you think would speak against having a mapAll
    (registerAll) and getAllInstances for multiple classes implementing the
    same interface in robotlegs, besides more work ;)

    Nothing really, I suppose. Personally I've never had a need for it, but
    that doesn't mean it wouldn't be a good idea.

  8. 8 Posted by thomas.auinger on 03 Dec, 2013 09:28 AM

    thomas.auinger's Avatar

    Thanks for the discussion! The factory pattern seems to make a lot of sense here and I will switch to that. Anyway, thanks for all the input, helped me beyond this issue as well.

    BTW great work on the framework!!

  9. Support Staff 9 Posted by Ondina D.F. on 05 Dec, 2013 12:48 PM

    Ondina D.F.'s Avatar

    No problem :)

  10. Ondina D.F. closed this discussion on 05 Dec, 2013 12:48 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