Injecting parameter values
Hi, it's me again.
I once played around with spring for Java and I wonder if it is
possible to Inject parameters into constructors and setters with
the RL2 framework.
Something like injector inject into class Alpha the string "Hello"
using setter setString(). Or inject into constructor param 1 = 4,
param 2 = Classname.
On a side note, how can i use named injections?
Thanks.
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 Ondina D.F. on 13 Feb, 2014 11:11 AM
Named injection:
Mapping:
Injection point:
Using an interface:
Constructor injection
Mappings:
Dependencies are mapped first, then the class needing them in the constructor!
SomeModel:
2 Posted by matej on 13 Feb, 2014 11:12 AM
1:
if constructor has parameters, they will be automatically injected if the class is created with injector.
(you inject the class somewhere, or you create it directly with injector.getInstance() or injector.instantiateUnmapped if the class is not mapped.
to inject into setters, just put inject tag above setter
[Inject]
public function set (value:Foo):void;
2:to use named injections, you would inject like this
[Inject(name=“foo”)]
public var foo:Foo;
it works for setters also, but I don’t know how to achieve that in contractor
When you map the class, you have the optional name parameter
injector.map(FOO,”foo”);
3 Posted by dhagenbln on 13 Feb, 2014 11:21 AM
Wow thanks, that clears up a lot of the fog still residing in my brain.
Support Staff 4 Posted by Ondina D.F. on 13 Feb, 2014 11:42 AM
@matey Place the metadata tag above the class :
5 Posted by dhagenbln on 17 Feb, 2014 05:53 PM
One little thing Is it possible to Inject multiple parameters with and without names?
Something like:
[Inject (first param some instance, name="someValue")] public class SomeClass
{ public function SomeClass(someInstance:InstanceType, someValue:String){}
}
So the first parameter is unnamed and the second is named.
Support Staff 6 Posted by Ondina D.F. on 18 Feb, 2014 01:11 PM
In theory, according to this: https://github.com/tschneidereit/SwiftSuspenders/blob/the-past/READ..., it should work like this:
But, it doesn't. I don't know whether it is a bug or not.
I've tried this:
After changing the order of the params, where someInstance doesn't have a named mapping, it worked. I have no idea if it has to do with the order of the _requiredParameters and/or the way the DependencyProvider is set, and I don't have the time to investigate this any further or to experiment with different mappings and so on.
If you, or Matej, or someone else have the time to investigate this, please do so, and if it appears to be indeed a bug, please report it on github, on the new location of swiftsuspenders:
https://github.com/robotlegs/swiftsuspenders
Ondina
Ondina D.F. closed this discussion on 11 Mar, 2014 08:50 AM.