Scenario:
-The app loads ViewA and its associated ViewModelA.
-ViewA has a button with command binding to a command on ViewModelA.
-ViewModelA handles the command and an instance of ViewB should be created and shown.
The question is, who should be responsible for creating the instance of ViewB?
It seems reasonable that the ViewModel should not know anything about the Views, so I see a couple of possibilities that could work:
1) ViewModelA could set a property that is monitored by ViewA so that ViewA knows to create an instance of ViewB. But this seems like it would be giving too much responsibility to ViewA.
2) You could inject some sort of ViewManager service into ViewModelA and when it handles the command from ViewA, it could use the command parameter to pass some sort of appropriate metadata into the ViewManager's LoadView(metadata) method.
Asked
Active
Viewed 790 times
2

Chris Swain
- 5,193
- 5
- 21
- 22
1 Answers
0
Look at answer from arconaut (even if it was not the accepted answer at the time, it seems to be the cleanest approach:
- In your ViewModel, use some kind of Mediator pattern (or something like an EventAggregator) to "publish" a CreateView message
- Have a class somewhere, called something like ViewManager, whose responsability would be to suscribe to all the CreateView messages, and to instantiate the corresponding Views
This is almost like your second proposal, just a bit more loosely coupled.
-
This does sound like the solution I'll go with. I'm thinking of breaking my application into different aspects or functionalities (i.e. the Login aspect, User Management aspect, Customer Management aspect, etc.). Each of these aspects of the application will have it's own AspectController object whose job will be to initialize and manage the Views/ViewModels that are related to that aspect of the application. It will listen for requests to show different Views and determine if/how to initialize and show the View requested. – Chris Swain Jan 21 '11 at 15:22
-
This would mean that the LoginAspectController would initialize and show the LoginView/LoginViewModel, and when the user clicks the "Reset Password" button, the command would be handled by the LoginViewModel and a request would be made for the ResetPasswordView/ResetPasswordViewModel. The LoginAspectController would recieve and handle the request appropriately. – Chris Swain Jan 21 '11 at 15:25
-
I'm not sure if "AspectController" is the right thing to call it, or if there is a better name. – Chris Swain Jan 21 '11 at 15:27