Question

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

Was it helpful?

Solution

That's how I do it, and I believe that this is the recommended way. In fact, I think that many advanced RobotLegs users will break out most of the mappings into Commands for convenience, reuse, and to make it easier to read the program--even if the Command is only run once at startup. I've used it for things like swapping out mock services for real services--the Command that maps the dependencies is different, but everything else is the same.

I don't see this as "that much" coupling logic. The Command is merely setting up the program based on current Application state. There's not really that much difference between using a Command to change Injector state vs your own custom Model state.

You may even find that you can reuse your injection mapping Commands across Applications, whereas you might not be able to reuse the entire Context.

HTH;

Amy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top