Injecting parameter values

dhagenbln's Avatar

dhagenbln

13 Feb, 2014 09:59 AM

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.

  1. Support Staff 1 Posted by Ondina D.F. on 13 Feb, 2014 11:11 AM

    Ondina D.F.'s Avatar

    Named injection:

    Mapping:

    injector.map(SomeModel, "someName").asSingleton();
    

    Injection point:

    [Inject (name="someName")]
    public var someModel:SomeModel;
    

    Using an interface:

    injector.map(ISomeModel, "someName").toSingleton(SomeModel);
    
    [Inject (name="someName")]
    public var someModel:ISomeModel;
    

    Constructor injection

    Mappings:

    injector.map(SomeVO).asSingleton(); //1
    
    var someProperty:String = "something";
    injector.map(String).toValue(someProperty); //2
    
    injector.map(ISomeModel, "someName").toSingleton(SomeModel); //3
    

    Dependencies are mapped first, then the class needing them in the constructor!

    SomeModel:

    private var _someVO: SomeVO;
    private var _ someProperty: String;
            
    public function SomeModel(someVO:SomeVO, someProperty:String)
    {
    ...
    
  2. 2 Posted by matej on 13 Feb, 2014 11:12 AM

    matej's Avatar

    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. 3 Posted by dhagenbln on 13 Feb, 2014 11:21 AM

    dhagenbln's Avatar

    Wow thanks, that clears up a lot of the fog still residing in my brain.

  4. Support Staff 4 Posted by Ondina D.F. on 13 Feb, 2014 11:42 AM

    Ondina D.F.'s Avatar

    but I don’t know how to achieve that in contractor

    @matey Place the metadata tag above the class :

    [Inject (name="someString")]
    public class SomeModel implements ISomeModel
    {
        private var _someProperty:String;
        public function SomeModel(someProperty:String)
        {
        ..
    
  5. 5 Posted by dhagenbln on 17 Feb, 2014 05:53 PM

    dhagenbln's Avatar

    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.

  6. Support Staff 6 Posted by Ondina D.F. on 18 Feb, 2014 01:11 PM

    Ondina D.F.'s Avatar

    In theory, according to this: https://github.com/tschneidereit/SwiftSuspenders/blob/the-past/READ..., it should work like this:

    [Inject (name='', name='someValue')] 
    
    public class SomeClass
    {
    
    public function SomeClass(someInstance:InstanceType, someValue:String){}
    
    }
    

    But, it doesn't. I don't know whether it is a bug or not.

    I've tried this:

    [Inject (name='someValue')] 
    
    public class SomeClass
    {
    ///////changed the order of parameters:
    public function SomeClass(someValue:String, someInstance:InstanceType){}
    
    }
    

    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

  7. Ondina D.F. closed this discussion on 11 Mar, 2014 08:50 AM.

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