tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/878-injecting-a-model-in-a-view-is-it-ok-any-alternativesRobotlegs: Discussion 2018-10-18T16:35:39Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/153111512012-04-17T00:48:44Z2012-04-27T07:52:58ZInjecting a model in a view. is it ok? any alternatives?<div><p>Hi,</p>
<p>I was struggling with the same issues and decided to adopt a
simple routine. I did not create this, it was suggested by a fellow
developer.</p>
<p>I build each view or component of an app with the following
package:<br>
- MyView (Sprite or other display object) - MyViewModel (Actor) -
MyViewMediator (Mediator) - MyViewEvent (Event)</p>
<p>The view exposes the necessary functions to be updated by the
mediator as needed. If the data is complex, I pass Value Objects.
It has no awareness of either the Model or the Mediator.</p>
<p>The Mediator usually has 2 injections the View and the Model.
Its job it to update the view with the data from the model. Or
update the model with the data from the view and tell the app it
has been updated. It registers listeners for the various events it
handles and dispatches events as needed. All these are defined in
the MyViewEvent class. The mediator has no concept of the rest of
the app.</p>
<p>The Model is updated by the Mediator, following user input in
the view, or via Commands by other actors in the app.</p>
<p>Events do not carry any data. Usually an "update" event would
tell the Mediator to update the View with Model data. And a
"change" event is dispatched by the Mediator when the View has
changed.</p>
<p>The flow is simple App Event > Execute Command > Update
Model >Dispatch Event > Mediator Event Handler > Update
View. Or View Signal > Mediator updates Model > Mediator
DIspatches Event > App Executes Command > ...</p>
<p>This tends to be boilerplate code heavy, since I creates 4
classes, minimally, per view. But I find this regimented mode to be
quite flexible. A view components ends up being 100% self contained
and can be poped into another app with little or no re-write.</p>
<p>It also clearly segment the roles and responsibilities of the
various actors. I find that managing the flow of data can be
tricky, especially when many actors start firing events and trigger
actions, keeping track of who does what can be hell.</p>
<p>My last project ended up having over 4000 lines of code written
in 2 weeks with quite a few moving parts (2D overlays, Away3D
content and dynamic data) and without this structure I wouldn't be
able to manage. Might not work for everything, but so far it served
me well :)</p>
<p>Hope this helps!</p>
<p>Cheers,</p>
<p>J.</p></div>Jerome Maurey-Delaunaytag:robotlegs.tenderapp.com,2009-10-18:Comment/153111512012-04-17T12:59:13Z2012-04-17T12:59:13ZInjecting a model in a view. is it ok? any alternatives?<div><p>As Joel likes to say, "it's about 'appropriate coupling' not 'no
coupling'"</p>
<p>I've used models injected into views. Even when I'm doing that I
prefer to<br>
treat the models as "read only" and use other mechanisms to set
values on<br>
the models. Once I even created a read-only interface for the
models that I<br>
used for mediator injection, just to enforce it. That did end up
being a<br>
bit more complicated from the mapping side because you can't just
use<br>
"injector.mapSingletonOf()" (since you really need two mappings per
model -- one read-only and one writeable).</p>
<p>However, I have also seen projects that end up with a lot of
tangled code<br>
that's hard to follow, that would not have been possible if we
weren't<br>
injecting models into mediators. For me personally, I've decided
that the<br>
benefit of having fewer commands and events isn't worth the risk
of<br>
complicated mediators. But that's just what works best for me.</p>
<p>Paul</p></div>Paul Robertsontag:robotlegs.tenderapp.com,2009-10-18:Comment/153111512012-04-17T23:43:14Z2012-04-17T23:43:14ZInjecting a model in a view. is it ok? any alternatives?<div><h1>spam</h1></div>Straytag:robotlegs.tenderapp.com,2009-10-18:Comment/153111512012-04-19T11:35:09Z2012-04-19T11:35:09ZInjecting a model in a view. is it ok? any alternatives?<div><p>Thanks a lot guys. For this specific scenario I think I will
inject the model to the mediator. :)</p>
<p>Till next time...</p></div>nickt