tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/2327-what-sorts-of-dependencies-are-candidates-for-injectionRobotlegs: Discussion 2018-10-18T16:35:48Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/270070262013-05-25T01:41:14Z2013-05-25T01:41:16ZWhat sorts of dependencies are candidates for injection?<div><p>Hi there,</p>
<p>I'm a fairly experienced AS3 developer, but new to Robotlegs.
I've got a handle on the major concepts, read the O'Reilly book,
and have dug into a few demo projects. I'm stoked. But one thing I
can't quite wrap my head around is when to use dependency
injection.</p>
<p>I get the argument against statics, and I can clearly see the
benefit of injecting custom classes. But what about base classes
like Strings, int, Number, Boolean, Array, and others? What is the
benefit of injecting these? If I need a Boolean to hold some bit of
state in a class, why use injection for such a minor thing?</p>
<p>I've poured over a number of demo projects, including the Mosaic
& Kanban apps discussed in the O'Reilly book, and dependencies
are never injected for base classes in any of them. Instead they
are declared as protected vars. However, in the book itself there
is some mention of injecting Strings and especially Arrays.</p>
<p>To confuse things further, flash components like Labels and
TextFields are also not injected in the demo projects I've looked
at.</p>
<p>So what I'm asking is: What are the best practices for
dependency injection, and what sorts of dependencies are candidates
for injection?</p>
<p>If you got this far, thank you!</p>
<p>Gabe</p></div>gmasteytag:robotlegs.tenderapp.com,2009-10-18:Comment/270070262013-05-25T05:13:28Z2013-05-25T05:13:28ZWhat sorts of dependencies are candidates for injection?<div><p>The key benefit of dependency injection is that it creates a
"seam."<br>
Because it provides a mechanism for getting access to an object
without<br>
having to define how the object is created, you can easily
substitute some<br>
other object in its place (most commonly for testing purposes, but
also to<br>
give you flexibility to easily change how you implement a
particular part<br>
of your app).</p>
<p>So a common candidate for dependency injection would be
something that a<br>
class needs in order to do its job, that you might want to replace
with a<br>
"mock" version in a testing use case.</p>
<p>Usually you're doing injection in a model (so you can easily
test your<br>
business logic) or in a service (so you can test how the service
calls and<br>
responds). In addition, utilities such as data adapters that you
would<br>
likely want to test.</p>
<p>In addition, in Robotlegs you use injection in commands and
mediators. This<br>
isn't so much so you can test them (since they're ideally more just
message<br>
passers rather than doing any important calculations). Rather its
for<br>
convenience to keep the parts of your app separated from each other
to keep<br>
them from being tightly connected.</p>
<p>Consequently, there's rarely any need to inject a primitive
value such as a<br>
string or number. They just don't match the common use cases. (You
might<br>
have a string such as a service URL that is a dependency, but
it's<br>
preferable to make it a property of a model object and either
inject the<br>
model or pass the string in through a call to the service method.)
One big<br>
reason for tis is because those types are just too common, so it's
not as<br>
straightforward to inject them. You end up using a "magic string"
such as<br>
an id or name, but that's not preferred because its subject to
typos and<br>
being changed in one place but not another.</p>
<p>The reason display objects such as UI components aren't injected
is because<br>
then the application becomes too tightly coupled. It's best to just
let the<br>
parent view (e.g. A Sprite) be the only one that knows about
the<br>
particulars of the UI, since that's an area that could change
frequently.<br>
The mediator can just call methods on the view and it can handle
the<br>
management of the components.</p>
<p>Hopefully that helps. Feel free to ask for ore clarification.
I'll try to<br>
give more concrete examples (those aren't as easy to type on a<br>
mobile device).</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/270070262013-05-27T16:54:39Z2013-05-27T16:54:40ZWhat sorts of dependencies are candidates for injection?<div><p>Hi Paul,</p>
<p>Thanks so much for this explanation. I definitely see the
benefits for testing, and for avoiding tight coupling. It seems
like Robotlegs is a clever way to enforce good OOP. I'm relieved
that I don't need to inject primitive values - that would be so
incredibly tedious.</p>
<p>Cheers!</p>
<p>Gabe</p></div>gmastey