tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/13209-injected-into-command-service-is-nullRobotlegs: Discussion 2015-09-21T15:45:16Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/379759512015-09-20T12:04:24Z2015-09-20T12:07:12ZInjected into Command service is null <div><p>Hello.<br>
Please point to my error.<br>
the config is:</p>
<p>`[Inject] public var injector:IInjector;</p>
<p>public function configure():void<br>
{ injector.map(FloxService);</p>
<pre>
<code> commandMap.map(ConfigEvent.CONFIG_INIT_COMPLETE).toCommand(ShowIntroScreenCommand);
commandMap.map(ScreenEvent.TO_MAIN_SCREEN_REQUEST).toCommand(ShowMainScreenCommand);
commandMap.map(ScreenEvent.TO_INTRO_SCREEN_REQUEST).toCommand(ShowIntroScreenCommand);
commandMap.map(ScreenEvent.SAVE_DATA_REQUEST).toCommand(SaveDataCommand);
mediatorMap.map(IntroScreen).toMediator(IntroScreenMediator);
mediatorMap.map(MainScreen).toMediator(MainScreenMediator);
context.afterInitializing(initComplete);
}
private function initComplete():void
{
dispatcher.dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_INIT_COMPLETE));
}`</code>
</pre>
<p>Command<br>
`public class SaveDataCommand extends Command{</p>
<pre>
<code>[Inject]
public var service:IService;
public function SaveDataCommand() {
trace("service = "+service);
if(service){
service.saveData();
}
else{
trace("Service is NULL");
}
}</code>
</pre>
<p>} }`</p>
<p>Service<br>
`package dev.div0.services { import com.gamua.flox.Flox;</p>
<p>public class FloxService implements IService {</p>
<pre>
<code>public function saveGoToMainCount(count:int):void {
Flox.logEvent("go_main_clicked", {count: count});
}
public function saveGoToIntroClick():void {
Flox.logEvent("go_intro_clicked");
}
public function saveData():void {
Flox.getTime(getTimeCompleteHandler, getTimeErrorHandler);
}
private function getTimeErrorHandler(error:String, httpStatus:int):void {
trace("saveData error:"+error +". httpStatus:"+httpStatus);
}
private function getTimeCompleteHandler(time:Date):void {
new SaveData(time.toString());
}
}</code>
</pre>
<p>}`</p>
<p>in case of service's interface injection</p>
<p>[Inject] public var service:IService;</p>
<p>the error is</p>
<p>Error: Injector is missing a mapping to handle injection into
property "service" of object "[object SaveDataCommand]" with type
"dev.div0.commands::SaveDataCommand". Target dependency:
"dev.div0.services::IService|"</p>
<p>in case of class injection</p>
<p>[Inject] public var service:FloxService;</p>
<p>service is NULL</p></div>dev.div0tag:robotlegs.tenderapp.com,2009-10-18:Comment/379759512015-09-20T12:47:51Z2015-09-20T12:47:51ZInjected into Command service is null <div><p>Hi,</p>
<p>injector.map(FloxService); is an incomplete mapping.</p>
<p>The correct mapping would be:</p>
<pre>
<code>injector.map(FloxService).asSingleton();</code>
</pre>
<p>Please read the answers from the following discussions for more
details:</p>
<p><a href="http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-necessary-conditions-of-injection-to-work#comment_30057747">
http://knowledge.robotlegs.org/discussions/robotlegs-2/8182-the-nec...</a></p>
<p><a href="http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automagic-view-injection#comment_28759520">
http://knowledge.robotlegs.org/discussions/robotlegs-2/5766-automag...</a></p>
<p><a href="http://knowledge.robotlegs.org/discussions/robotlegs-2/8018-why-i-can-inject-a-signal-in-mediator-and-dispantch-but-not-in-view#comment_29936130">
http://knowledge.robotlegs.org/discussions/robotlegs-2/8018-why-i-c...</a></p>
<p>Also, be aware of the differences between the different mappings
:</p>
<p><strong>toValue</strong> - Makes the mapping return the given
value for each consecutive request.</p>
<p><strong>toType</strong> - Makes the mapping return a newly
created instance of the given type for each consecutive
request.</p>
<p><strong>toSingleton</strong> - Makes the mapping return a lazily
constructed singleton instance of the mapped type for each
consecutive request.</p>
<p><strong>asSingleton</strong> - Makes the mapping return a lazily
constructed singleton instance of the mapped type for each
consecutive request.</p>
<p>
<strong>asSingleton(initializeImmediately:Boolean=false)</strong> -
initializeImmediately determines when the instance should be
created, immediately after mapping or after the first request</p>
<p>Hope this helps.<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/379759512015-09-20T12:50:52Z2015-09-20T12:50:52ZInjected into Command service is null <div><p>I forgot to say that if you are injecting an interface, you
should map it like this:</p>
<pre>
<code>injector.map(IService).toSingleton(FloxService);</code>
</pre></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/379759512015-09-20T13:21:35Z2015-09-20T13:21:35ZInjected into Command service is null <div><p>Thank you.<br>
By the way - its my error.<br>
I need to override execute() method to use injected service inside
Command - my inattention.</p></div>dev.div0tag:robotlegs.tenderapp.com,2009-10-18:Comment/379759512015-09-21T15:45:15Z2015-09-21T15:45:15ZInjected into Command service is null <div><p>You're welcome!</p>
<p>Concerning the execute method in commands, I didn't notice that
you didn't have an execute() in your command. I spotted the
incomplete mapping and didn't pay much attention to the code below
it.</p>
<p>So yes, injected classes will be null within a constructor,
because the Injector hasn't finished its job (injection) yet:</p>
<p><a href="https://github.com/robotlegs/robotlegs-framework/wiki/common-problems#injected-properties-are-null-in-constructor">
https://github.com/robotlegs/robotlegs-framework/wiki/common-proble...</a></p>
<p>Just for your information, you don't need to extend the
robotlegs Command class. Any class with a public execute method can
be used as a Command.</p>
<p>I'm going to mark this discussion as resolved, but you can
re-open it, if need be, or you can create a new discussion if you
need assistance on another topic.</p>
<p>Ondina</p></div>Ondina D.F.