tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2721-give-model-instance-to-services-argumentsRobotlegs: Discussion 2013-11-13T15:04:07Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/289230042013-09-20T14:09:55Z2013-09-20T14:09:55Zgive model instance to services arguments <div><p>Hello,</p>
<p>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:</p>
<pre>
<code>injector.mapSingletonOf(ISomeModel, ModelOne, "modelOne");
injector.mapSingletonOf(ISomeModel, ModelTwo, "modelTwo");</code>
</pre>
<p>You dispatch an event to trigger the command, with a string
payload having the name of the model you want to use:</p>
<p>dispatch(new SomeRequestEvent(SomeRequestEvent.DATA_REQUESTED,
"modelTwo" ));</p>
<p>In SomeCommand:</p>
<pre>
<code>[Inject]
public var service:ISomeService;
[Inject]
public var event:SomeRequestEvent;
override public function execute():void
{
someService.model = injector.getInstance(ISomeModel, event.somePayload);
someService.someMethod();
}</code>
</pre>
<p>In SomeService:</p>
<pre>
<code>private var _model:ISomeModel;
public function set model(value:ISomeModel):void
{
_model=value;
}
public function someMethod():void
{
// do something with _model.someProperty
}</code>
</pre>
<p>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:</p>
<p>[Inject (name="modelOne")] public var model:ISomeModel;</p>
<p>...</p>
<p>someService.model = model;</p>
<p>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?</p>
<p>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?</p>
<p>Ondina</p></div>Ondina D.F.