Object does not get dependencies injected into it when it is mapped using toValue()
While creating a context, I have a line like this:
viewContext.injector.map( IUITrackingService ).toValue( tracking );
This works ok, and I get the instance correctly wherever I inject it. However, the object itself does not have dependencies injected. I assumed that the object would become managed if it was added to a mapping.
Any ideas how to get the injector to inject into that object? (injector.injectInto() can't be used directly here because the context isn't ready).
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 Shaun Smith on 22 Aug, 2013 05:39 PM
Yeh, that's a troublesome one, as objects aren't really "managed" in RL/SwiftSuspenders, and we made a conscious decision not to inject into values provided to toValue().
The easiest thing would probably be to create a config class and add it as the final config. Inject IUITrackingService and the injector into it, and call injectInto in the config method.
2 Posted by peter.hall on 23 Aug, 2013 12:56 PM
Thanks, it works fine to add the mapping and do the injection after the context is initialized.
But perhaps you can suggest a better approach for what I'm doing..
There's a parent context that contains a factory, each child context needs it's own instance created by that factory, so that it can be injected with different objects.
I know that the parent context will be available already, so I'm injecting the factory into the child's config, then using it to create an instance. Then I'm adding a mapping to the child, and - now - I'm using injectInto to perform injections there.
Could you suggest a better approach using existing RB extensions?
Ondina D.F. closed this discussion on 11 Oct, 2013 08:52 AM.