give model instance to services arguments

iamable's Avatar

iamable

20 Sep, 2013 08:58 AM

hey there,

I am a beginner with robotlegs and just made one simple project with RL 1.5.2 and now with my second project I have a problem.

I would like to write a service class ("Connect.as") for a library. Everytime I will use this class I would like to give a special model as an argument.
But I dont know how to write this in the MainContext.

What is the correct way to map singletons?

I think this is incorrect:

                        //models
injector.mapSingleton(VehicleModel);

//services
injector.mapSingleton(CanConnect(VehicleModel));

  1. Support Staff 1 Posted by Ondina D.F. on 20 Sep, 2013 02:09 PM

    Ondina D.F.'s Avatar

    Hello,

    Assuming that your models implement the same interface, say ISomeModel, a possible solution I can think of right now, would be to use named injections:

    injector.mapSingletonOf(ISomeModel, ModelOne, "modelOne");
    injector.mapSingletonOf(ISomeModel, ModelTwo, "modelTwo");
    

    You dispatch an event to trigger the command, with a string payload having the name of the model you want to use:

    dispatch(new SomeRequestEvent(SomeRequestEvent.DATA_REQUESTED, "modelTwo" ));

    In SomeCommand:

    [Inject]
    public var service:ISomeService;
    
    [Inject]
    public var event:SomeRequestEvent;
    
    override public function execute():void
    {
        someService.model = injector.getInstance(ISomeModel, event.somePayload);
        someService.someMethod();
    }
    

    In SomeService:

    private var _model:ISomeModel;
            
    public function set model(value:ISomeModel):void
    {
        _model=value;
    }
    
    public function someMethod():void
    {
        // do something with _model.someProperty
    }
    

    Another option is to have more than one command, each one injected with the model you want to use for this specific server call, that you can pass on to the service. Say, in SomeCommand you inject:

    [Inject (name="modelOne")] public var model:ISomeModel;

    ...

    someService.model = model;

    Depending on your use case, there might be better solutions than having named injections and having the injector get an instance of a model in a command. More info?

    I'm curious as of why you need to use different models in a service. Are you using them to read or to set some properties?

    Ondina

  2. Ondina D.F. closed this discussion on 11 Oct, 2013 08:53 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