Extension Problem

mbarjawi's Avatar

mbarjawi

03 Apr, 2013 07:31 PM

I attempted to create a bootstrap extension for Robotlegs 2 to be used in my applications. I looked at the SignalCommandMap extension as an example and implemented similarly.

This is the extension file:

public class BootstrapExtension implements IExtension
{
    /*============================================================================*/
    /* Private Properties                                                         */
    /*============================================================================*/

    private const _uid:String = UID.create(BootstrapExtension);

    /*============================================================================*/
    /* Public Functions                                                           */
    /*============================================================================*/

    public function extend(context:IContext):void
    {
         context.injector.map(IBootstrap).toSingleton(Bootstrap);
    }

    public function toString():String
    {
        return _uid;
    }
}

Here is the some of the Bootstrap class:

public class Bootstrap implements IBootstrap
{
    /*============================================================================*/
    /* Private Properties                                                         */
    /*============================================================================*/

    private var _injector:Injector;
    private var _signalCommandMap:ISignalCommandMap;

    /*============================================================================*/
    /* Constructor                                                                */
    /*============================================================================*/

    public function Bootstrap( injector:Injector, signalCommandMap:ISignalCommandMap )
    {
        _injector = injector;
        _signalCommandMap = signalCommandMap;
    }
    .
    .
    .
}

My question is: as far as I understand, swiftsuspenders will inject the parameters of the constructor for the Bootstrap class. This is the way it is defined also in other extensions. However, this doesn't happen all the time for this extension. I mean that sometimes the Injector is mapped successfully... and other times it is not and I get simply NULL.

Is there something wrong with my implementation to this extension?

Thanks
MBarjawi

  1. Support Staff 1 Posted by creynders on 04 Apr, 2013 01:38 PM

    creynders's Avatar

    I mean that sometimes the Injector is mapped successfully... and other times it is not and I get simply NULL.

    You mean an error is thrown that no mapping is found for type Injector?
    Or..?

    How do you verify that _injector is null? trace? breakpoint? In the constructor?

  2. 2 Posted by mbarjawi on 04 Apr, 2013 03:17 PM

    mbarjawi's Avatar

    Thank you for your reply.

    No error is thrown from robotlegs which I think is strange. Mainly, it happens that when I am in debug mode and have a break point inside the constructor, I can see that the injector is null. When I run without breakpoints and without debugging, it runs normally... but not all the times. Only few times it will give a null object error (because I am using the injector later on in my class). Similarly, only few times while in debug mode, the injector will come in normally.

  3. Support Staff 3 Posted by Ondina D.F. on 04 Apr, 2013 03:22 PM

    Ondina D.F.'s Avatar

    Hi guys,

    I tried MBarjawi’s example with Swiftsuspenders-v2.0.0rc2.swc and it looks like the signalCommandMap has ‘eaten’ the injector :P.

    Without signalCommandMap the injector gets injected just fine.
    But with contructor injections for both, injector and signalCommandMap, the injector is null inside Bootstrap.

    In fact, it affects not only the injector, but any other dependencies provided as ctor injections.
    I think that behavior has to do with this bug:
    https://github.com/tschneidereit/SwiftSuspenders/pull/84

    which has been fixed in the latest version ( only source code)

    Workaround for the version with bugs:
    Map and instantiate signalCommandMap in BootstrapExtension,

    _injector = context.injector;
    
    _injector.map(ISignalCommandMap).toSingleton(SignalCommandMap);
    _signalCommandMap = _injector.getInstance(ISignalCommandMap);
    
    _injector.map(IBootstrap).toSingleton(Bootstrap);
    

    Or by using

    [PostConstruct]
    public function init(injector:Injector, signalCommandMap:ISignalCommandMap):void
    {
    

    Or install SignalCommandMapExtension too, and instantiate signalCommandMap in your config.

    [EDIT] Or just inject them instead of constructor injection in Bootstrap.
    Of course, they won’t be available inside the constructor, in this case.

    [Inject] 
    public var _injector:Injector;
    
    [Inject]
     public var _signalCommandMap:ISignalCommandMap;
    

    @MBarjawi could you try the newest version of swiftsuspenders(source code : https://github.com/tschneidereit/SwiftSuspenders) and tell us if that fixes your problem?

    Ondina

  4. 4 Posted by mbarjawi on 04 Apr, 2013 04:46 PM

    mbarjawi's Avatar

    @MBarjawi could you try the newest version of swiftsuspenders(source code : https://github.com/tschneidereit/SwiftSuspenders) and tell us if that fixes your problem?

    I just tested using the latest version of swiftsuspenders... the problem disappeared :)

    Thanks so much,

  5. mbarjawi closed this discussion on 04 Apr, 2013 04:47 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