tag:robotlegs.tenderapp.com,2009-10-18:/discussions/problems/612-do-i-need-command-chainingRobotlegs: Discussion 2012-07-05T10:08:59Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-06-29T13:15:38Z2012-06-29T13:15:38Zdo I need command chaining? <div><p>Hi Andy,</p>
<p>Of course you can create an instance of
CreateDefaultSettingsXMLService where you need it and destroy it
when you don’t need it anymore.</p>
<p>But, you can also do something like this:</p>
<p>-Context mappings:</p>
<p>injector.map(ISomeService).toSingleton(SomeService);</p>
<p>commandMap.mapEvent(SomeEvent.LOAD_SOMETHING, SomeCommand,
SomeEvent);</p>
<p>-In SomeCommand :</p>
<p>[Inject] public var someService: ISomeService;</p>
<p>then access someService.loadSomething()</p>
<p>After SomeService has finished its job, it dispatches an event
triggering an</p>
<p>-UnmapSomeServiceCommand, which will do:</p>
<p>injector.unmap(ISomeService); // ==> SomeService gets
gc-ed</p>
<p>And, if you want, you can unmap the commands as well:</p>
<p>commandMap.unmapEvent(SomeEvent.LOAD_SOMETHING, SomeCommand,
SomeEvent);</p>
<p>But, as you probably already know, commands won’t be kept
in memory after their execute() has run.</p>
<p>HTH<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-06-29T14:30:10Z2012-06-29T14:30:12Zdo I need command chaining? <div><p>Thanks! Super quick reply too :)<br>
I ended up with something similar to what you suggest.<br>
In my Context:<br>
injector.mapSingleton(SaveDefaultsService);<br>
and<br>
commandMap.mapEvent(DefaultsEvent.CREATE_DEFAULTS,
CreateFreshDefaultsCommand,DefaultsEvent);</p>
<p>I am guessing that it's overkill to use a command in my above
code when a service can just call another service. Guessing to it
is better practice not to make my singleton 'accessible to all' and
just accessible to what needs it. I'll update to your code :)</p>
<p>Slowly things are becoming more understandable to me.
Cheers!</p></div>andytwoodstag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-06-29T14:57:27Z2012-06-29T14:57:27Zdo I need command chaining? <div><p>No problem, Andy:)<br>
Maybe my first statement was a bit misleading.<br>
I should have warned you that creating an instance of
CreateDefaultSettingsXMLService in another service may be
problematic.<br>
How will you know when to destroy it, in case of an asynchronous
response, and where are you intending to destroy it? Or are both
services disposable after running them?<br>
Besides, it’s not quite “nice” to call a service
from another service, if you want to strictly follow the best
practices ;)<br>
But, if you really need to do it like that, just do it. You can
decide later whether it’s a good approach or not.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T05:47:32Z2012-07-02T05:47:32Zdo I need command chaining? <div><p>Hi there,<br>
afraid it's me again :)<br>
Digesting this part of your first message:</p>
<p>"-Context mappings:
injector.map(ISomeService).toSingleton(SomeService);<br>
commandMap.mapEvent(SomeEvent.LOAD_SOMETHING, SomeCommand,
SomeEvent);<br>
-In SomeCommand : [Inject] public var someService: ISomeService;
then access someService.loadSomething()"</p>
<p>You've probably guessed it but I've a Q or 2!:<br>
-does RobotLegs only instantiate a service when it is called? Most
likely this is a stupid question :-) -regarding
"injector.map(ISomeService).toSingleton(SomeService);" Why use
ISomeService and not just injector.mapSingleton(SomeService)? Is
there a memory benefit of not referring to the 'full fat' class and
referring to it's interface?</p>
<p>Thanks!</p></div>andytwoodstag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T06:08:31Z2012-07-02T06:08:31Zdo I need command chaining? <div><p>Hi,<br>
another quick Q:<br>
" injector.map(ISomeService).toSingleton(SomeService);" guessing
this is robotlegs 2? map method not available to me (robotlegs 1).
cheers,<br>
Andy.</p></div>andytwoodstag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T06:58:22Z2012-07-02T06:58:23Zdo I need command chaining? <div><p>OMG This is so amazingly incredibly awesome:<br>
[PostConstruct]</p>
<p>For anyone reading this and wondering, calls any function after
everything has been loaded.<br>
<a href=
"https://github.com/robotlegs/robotlegs-framework/wiki/Robotlegs-Internals">
https://github.com/robotlegs/robotlegs-framework/wiki/Robotlegs-Int...</a></p></div>andytwoodstag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T14:10:20Z2012-07-02T14:10:20Zdo I need command chaining? <div><p>Hey Andy,</p>
<blockquote>
<p>injector.map(ISomeService).toSingleton(SomeService);" guessing
this is robotlegs 2? map method not available to me</p>
</blockquote>
<p>Right, this is rl 2 syntax. My bad, I was working on an rl2
project by the time I answered your question, and the more
intuitive syntax used in rl2 was still fresh in my mind.</p>
<p>For rl1 the mapping would look like this:<br>
injector.mapSingletonOf(ISomeService, SomeService);</p>
<p>Regarding the question about interfaces:<br>
It’s impossible for me to cover all the benefits of
“Programming to an interface, not an implementation“ in
this post. The topic is too complex, and besides there are a
multitude of in-depth articles and books on the subject, written by
experts. So I’ll just mention a few things (rather keywords
that can be used for further research) that come to mind:</p>
<ul>
<li>
<p>using interfaces enables code reuse, which can minimize
duplicate and repetitive code, the code becomes flexible, managing
dependencies is easier, your application is easily maintainable</p>
</li>
<li>
<p>using interfaces makes it possible to follow the SOLID principle
of OOP and OOD</p>
</li>
<li>
<p>interfaces are used in several design patterns</p>
</li>
<li>
<p>interfaces as APIs</p>
</li>
<li>
<p>interfaces keep the coupling minimal</p>
</li>
<li>
<p>encapsulating the implementation is a good thing</p>
</li>
<li>
<p>you can be sure that a certain functionality will work in a
standardized way</p>
</li>
<li>
<p>type safety</p>
</li>
<li>
<p>you get compiler errors when the class that implements an
interface does not implement all the methods declared in the
interface, and that’s good when building an application that
will be extended by another developer or used by third parties</p>
</li>
<li>
<p>it is easier to test classes in isolation and to use mock
objects when real objects are not available</p>
</li>
<li>
<p>generalization is a good thing, in OOP ;)</p>
</li>
<li>
<p>polymorphic capabilities</p>
</li>
<li>
<p>allows the use of interchangeable behaviors, variation of
implementation</p>
</li>
<li>
<p>using an interface allows a service to be swapped for another
one that implements the same interface, i.e., if IAssetsLoader is
implemented by SQLAssetsLoader , XmlAssetsLoader, MockAssetsLoader
, all you have to do is replace<br>
injector.mapSingletonOf(IAssetsLoader, SQLAssetsLoader);<br>
with<br>
injector.mapSingletonOf(IAssetsLoader, XmlAssetsLoader);<br>
or<br>
injector.mapSingletonOf(IAssetsLoader, MockAssetsLoader);// a dummy
class used to mimic services’ behavior – used in unit
testing<br>
This way your Application doesn’t have to care where the data
comes from.</p>
</li>
</ul>
<p>Sorry, if you already know all of this, or if the random listing
was confusing! :)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T15:18:03Z2012-07-02T15:18:04Zdo I need command chaining? <div><p>thanks for all your help! I'm still very much a newbie at this
sort of thing :-)</p></div>andytwoodstag:robotlegs.tenderapp.com,2009-10-18:Comment/169735602012-07-02T16:46:39Z2012-07-02T16:46:39Zdo I need command chaining? <div><p>You’re welcome, Andy!</p>
<p>And don’t worry, compared to the design patterns gods and
geniuses out there (GoF, Fowler and many others) we, ordinary
(mortals) coders, will be Noobs Forever;) No matter how advanced we
get, they’ll be always ahead of us, unless the best practices
and design patterns change over time…<br>
Besides, being a perpetual newbie in a programming/platform field
or another is our fate as developers, am I right?</p>
<p>You can close the discussion, in case your original problem has
been solved. Please open new threads for new issues.<br>
Thank you :)</p></div>Ondina D.F.