getting different instances of a Singleton! :-S

quebequiano's Avatar

quebequiano

20 Nov, 2012 11:43 AM

Hi everybody,

I'm using robotlegs for my app. I define a Singleton in the Context as follows:

        injector.mapSingleton(InfraPreModel);

Everything worked fine until I realized (with the debugger) that I had two different instances of InfraPreModel in execution time. I never call the new InfraPreModel(). Does anybody know the possible reasons of this error? I'm almost sure that is RobotLegs who is instanciating the object twice.

Thanxxx

  1. Support Staff 1 Posted by Ondina D.F. on 20 Nov, 2012 12:14 PM

    Ondina D.F.'s Avatar

    Hi!

    Are you using the latest versions of rl and swiftsuspenders?

    Are you using constructor injections? See http://knowledge.robotlegs.org/discussions/problems/94-constructor-... for a similar issue

    How and where are you injecting your model?

    Ondina

  2. 2 Posted by quebequiano on 20 Nov, 2012 12:23 PM

    quebequiano's Avatar

    Hi Ondina,

    I'm going to be more specific. I'm new in RobotLegs, and I realized some new stuff.

    I thought that you only have one Context for the whole application, but I see that you can have several Context of the same type, that's the reason because I get different instances of the "Singleton".

    What's the best way to share the same context in the whole application?

    My problem is:

    I have a MXML Group named "A" with the following declaration:

    <fx:Declarations>
        <control:MasterContext contextView="{this}"/>
    </fx:Declarations>
    

    And inside "A" there are several MXML Groups of type "B" with the same declaration:

    <fx:Declarations>
        <control:MasterContext contextView="{this}"/>
    </fx:Declarations>
    

    And I have an ActionScript Class C.

    In all of them I inject (with [Inject]) the object. When I check the ActionScript class and the "B" object I see that I hace two different instances of InfraPreModel and I want it to be a Singleton (common instance for the three components).

    Thanx

  3. Support Staff 3 Posted by Ondina D.F. on 20 Nov, 2012 01:06 PM

    Ondina D.F.'s Avatar

    It seems you could use the modular utility. See these:

    (search for “modular”)

    „if you don’t create a mapping in the child injector it will supply the injection as mapped within the parent (or grandparent).“ See links from here:
    http://knowledge.robotlegs.org/discussions/questions/983-sharing-mo...

    Are you repeating the mapping for InfraPreModel in each subcontext?

  4. 4 Posted by quebequiano on 20 Nov, 2012 01:49 PM

    quebequiano's Avatar

    I think I didn't understand the way RobotLegs works.

    I'm asking know a new and general question... Is there any way to implement Dep. Injection without using Mediators and Actors, I mean, only with one Context general to the whole Application?

  5. Support Staff 5 Posted by Ondina D.F. on 20 Nov, 2012 02:53 PM

    Ondina D.F.'s Avatar

    I think I didn't understand the way RobotLegs works. I'm asking know a new and general question... Is there any way to implement Dep. Injection without using Mediators and Actors, I mean, only with one Context general to the whole Application?

    Of course you can have one Context. You don’t need to use a new context for each component in your application.
    Since you’ve talked about more contexts, I assumed your goal was to build a modular app.

    Your Groups, A to C, are probably Views, say a Panel or a VGroup, containing different subcomponents, buttons, forms, whatever. Those Views might need a Mediator in order to communicate to other Views, or to trigger Commands etc.
    Probably Group A is the main application container, let’s call it ContextView, to which you add (through script or mxml) other components (B and C). The context definition/initialization would be made in the ContextView. The Context will then serve the entire app, and the mappings you define inside your Context will be available throughout your app.
    A View is an area of functionality. Let’s say you have a UserLoginView, containing input elements for name, pw and a login button. You’d mediate that View through UserLoginMediator . Another View could be UserDetailsView with a UserDetailsMediator, and so on.
    If you map a Model within your Context, it will be available in every class that needs it ( after injecting it)

    Start here:

    https://github.com/robotlegs/robotlegs-framework/wiki/best-practices

    http://www.developria.com/2010/06/an-introduction-to-robotlegs-a.html

    If you’ve read those already and looked at examples and tried them out as well, do it again ;)

    And don’t hesitate to ask questions, if need be.

  6. Ondina D.F. closed this discussion on 22 Nov, 2012 04:30 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