tag:robotlegs.tenderapp.com,2009-10-18:/discussions/questions/613-injecting-a-specific-model-implementation-on-the-flyRobotlegs: Discussion 2018-10-18T16:35:29Ztag:robotlegs.tenderapp.com,2009-10-18:Comment/88461002011-07-25T10:28:12Z2011-07-25T10:28:13ZInjecting a specific model implementation on the fly<div><p>Is model injection on the fly possible? In other words, if I ask
for a model of the type IPhotoModel, I should get one of its
implementations based on the current state of the view. If I am
looking at a UserPage, I should get a user-specific implementation
of that model. If I am looking at a LocationPage, I should get a
location-specific implementation.</p>
<p>Currently, the only way that I see is introducing a command that
specifies the model mapping, with a concrete one based on the
current view state ...</p>
<p>something like...</p>
<p>injector.mapValue(IPhotoViewModel,
injector.getInstance(UserPhotoViewModel)) or<br>
injector.mapValue(IPhotoViewModel,
injector.getInstance(LocationPhotoViewModel))</p>
<p>is this the best way possible? I do not really want to introduce
much coupling logic outside of the context, but ...</p></div>xantrustag:robotlegs.tenderapp.com,2009-10-18:Comment/88461002011-07-29T12:38:29Z2011-07-29T12:38:29ZInjecting a specific model implementation on the fly<div><p>So far, I have only one idea based on yours. It's like a mixin
of this and previous post. You probably need something between a
model and a value object. Try to use ActiveModel which contains
current ViewModel like you suggested. It can dispatch a
event/signal informing it changed its active ViewModel. Of course
ViewModel should implemet an interface of IPhotoViewModel in your
case. That new current ViewModel is provided with the event/signal
to all interested Mediators which are listening for a change. And
change the name - remove Model from it to stop thinking about too
much coupling ;) For me it's a kind of a data provider (a bit more
of a ValueObject) rather than a model. Maybe you should think about
splitting your PhotoViewModels if it does too much you would want
to provide for a Mediator, but the interface (or a facade) is
limiting access to the model.</p>
<p>I hope it's all clear, if not, give me a shot.<br>
Mike</p></div>Michal Wroblewski