tag:robotlegs.tenderapp.com,2009-10-18:/discussions/robotlegs-2/13233-when-to-use-extensions-and-when-to-use-commandsRobotlegs: Discussion 2015-10-28T15:48:11Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-22T09:55:42Z2015-10-22T09:55:42ZWhen to use Extensions and when to use Commands?<div><p>Do you want to create an extension for error handling?<br>
If the answer is no, I suggest to make the mapping in a config
class ( a class implementing IConfig and having a public
configure() method)</p>
<p>You have a few options:</p>
<p>a. map your ErrorHandler asSingleton with initializeImmediately
set to true</p>
<p>
org.swiftsuspenders.mapping.InjectionMapping.asSingleton(initializeImmediately:Boolean=false):UnsealedMapping</p>
<pre>
<code>injector.map(ErrorHandler).asSingleton(true);</code>
</pre>
<p>In your ErrorHandler class annotate the
startListeningForErrors() method with a [PostConstruct] metadata
tag, so it starts automatically.</p>
<p>b. add an afterInitializing method in the class where you create
your context and instantiate the ErrorHandler like this:</p>
<pre>
<code>var errorHandler:ErrorHandler = context.injector.getOrCreateNewInstance(ErrorHandler);
errorHandler.startListeningForErrors();</code>
</pre>
<p>c. within an afterInitializing method dispatch the event of your
choice to trigger a command where you've injected an already mapped
ErrorHandler and call its startListeningForErrors();<br>
That's only needed, if you have a special reason to do it in a
command</p>
<p>Would that work for you?</p>
<blockquote>
<p>When to use Extensions and when to use Commands?</p>
</blockquote>
<p>An Extension is like a utility or a library. Like the modular
extension , logger extension, macrocommand, starling view map
etc.<br>
Even in an Extension you don't need commands for mappings or
instantiating of classes.</p>
<blockquote>
<p>Should this class be created and mapped in an Extension and then
hook into the context lifecycle events such</p>
</blockquote>
<p>Yes, in case your intention is to create an Extension. The
ErrorHandlerExtension class would take care of the mappings and
hooking into the context's lifecycle, for example calling
startListeningForErrors from within an afterInitializing
handler.<br>
Of course, you need to pass<br>
ErrorHandlerExtension to the context.install() or to add it to the
list of extensions in a custom MVCSBundle, for it to work as
expected.</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-22T10:09:32Z2015-10-22T10:09:32ZWhen to use Extensions and when to use Commands?<div><p>I wanted to ask you, but then I forgot: How familiar are you
already with rl2? Should I tell you more about config classes used
for bootstrapping instead of rl1 commands? I'm asking because I
don't want to offend an expert by being too didactic;)</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-23T01:07:44Z2015-10-23T01:08:20ZWhen to use Extensions and when to use Commands?<div><p>Hi Ondina,</p>
<p>Thanks for such a long and detailed reply as always, you really
are a ledgend!</p>
<p>I am very familiar with RL1, used it on many projects for years.
This is the first project with RL2 that is not a greenfields
project, im trying to retroactively apply RL2 to it which is no
easy task.</p>
<p>My RL2 experience is limited to only a few small projects and
this one. You dont need to explain all the basics tho I have a firm
grasp of all that. My main difficultly was working out there
"initialisation" logic goes in general.</p>
<p>The ErrorHandler example I gave above was just an example. Its
more of a generic question really.</p>
<p>Assume the ErrorHandler is a generic bit of business logic that
needs to be initiated with some runtime variables.</p>
<p>I guess what you are saying is that if it isnt designed to be
reused then dont put it in an Extension?</p>
<p>Mike</p></div>mike.canntag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-23T15:20:56Z2015-10-23T15:20:56ZWhen to use Extensions and when to use Commands?<div><p>You're welcome, Mike. And thanks for what I think was a
compliment;)</p>
<blockquote>
<p>You dont need to explain all the basics tho I have a firm grasp
of all that.</p>
</blockquote>
<p>I'm sure you do have a firm grasp of all that!! And I wasn't
going to explain <strong>all</strong> the basics, hehe. I know that
you are an experienced developer in general and a robotlegs 1 user
in particular, but I was not sure where you stood on rl2 and the
differences between rl1 and rl2.</p>
<blockquote>
<p>My main difficultly was working out there "initialisation" logic
goes in general.</p>
</blockquote>
<p>That's why I asked if you needed more details on config
classes.</p>
<p>I fact, I just wanted to mention these answers :<br>
<a href="http://knowledge.robotlegs.org/discussions/questions/2209-mediator-#comment_26311878">
http://knowledge.robotlegs.org/discussions/questions/2209-mediator-...</a><br>
<a href="http://knowledge.robotlegs.org/discussions/robotlegs-2/6370-baby-steps-simple-robolegs-2-setup#comment_28678205">
http://knowledge.robotlegs.org/discussions/robotlegs-2/6370-baby-st...</a></p>
<p>because there you can find a few tips on porting rl1 apps to rl2
and on how to tackle the initialisation of a rl context + mappings.
Ignore them, if you already know all this stuff.</p>
<p>I guess, the short answer should have been:</p>
<p><strong>Commands</strong>, are, as always, the place to handle
application logic/behaviour/use cases, interaction with models and
services, application's bootstrapping. You can continue using
commands for bootstrapping, or replace them with
<strong>configuration</strong> classes and use them like this:</p>
<pre>
<code>_context = new Context()
.install(MVCSBundle)
.configure(MediatorsConfig, ModelsConfig, ModelsConfig, SignalsConfig, CommandsConfig)
.configure(new ContextView(_view);
// view == DisplayObjectContainer serving as contextView</code>
</pre>
<blockquote>
<p>I guess what you are saying is that if it isnt designed to be
reused then dont put it in an Extension</p>
</blockquote>
<p>Yes, reusable classes are good candidates for a library or
utility. But, a library can be framework agnostic and as such it
can be used in non-rl projects as well. Only if you want to
integrate it into a robotlegs context, you create an extension for
it. Extensions make the framework customizable. Extensions
<em>extend</em> framework's capabilities, by adding new features or
by integrating external utils. Extensions aren't meant to handle
the business logic of an application. But, like with anything else
in the robotlegs world, you are free to design your extensions for
whatever purpose you need them for.</p>
<blockquote>
<p>The ErrorHandler example I gave above was just an example. Its
more of a generic question really. Assume the ErrorHandler is a
generic bit of business logic that needs to be initiated with some
runtime variables.</p>
</blockquote>
<p>I think I answered that in the first part of my previous
message. Or not?<br>
In general, afterInitializing would be a good place to start
working with the already mapped classes of any kind. For example,
you can dispatch an event to trigger a command, if you want to
initiate some business logic...<br>
Tell me what exactly is still unclear, and the answers will be
shorter:)<br>
Cheers<br>
Ondina</p></div>Ondina D.F.tag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-28T08:51:07Z2015-10-28T08:51:07ZWhen to use Extensions and when to use Commands?<div><p>Hi Ondina,</p>
<p>Sorry for the late reply.</p>
<p>Thanks again for that answer, its really appreciated. After
thinking some more over the weekend and hacking at it the last few
days I think I have a clearer understanding.</p>
<p>I read those posts you linked and were a great help, thanks for
that and thanks again for your continued support here :)</p>
<p>Mike</p></div>mike.canntag:robotlegs.tenderapp.com,2009-10-18:Comment/382657722015-10-28T15:47:55Z2015-10-28T15:47:55ZWhen to use Extensions and when to use Commands?<div><p>My pleasure!<br>
Thanks again for your kind words, Mike. Great motivational booster
:)</p>
<p>Ondina</p></div>Ondina D.F.