Injecting a specific model implementation on the fly

xantrus's Avatar


25 Jul, 2011 10:28 AM

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.

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 ...

something like...

injector.mapValue(IPhotoViewModel, injector.getInstance(UserPhotoViewModel)) or
injector.mapValue(IPhotoViewModel, injector.getInstance(LocationPhotoViewModel))

is this the best way possible? I do not really want to introduce much coupling logic outside of the context, but ...

  1. 1 Posted by Michal Wroblews... on 29 Jul, 2011 12:38 PM

    Michal Wroblewski's Avatar

    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.

    I hope it's all clear, if not, give me a shot.

  2. Ondina D.F. closed this discussion on 02 Nov, 2011 05:57 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac