tags:

views:

64

answers:

2

Have a problem following the MVP or MVC design pattern (applies to either one). I can't figure out how to cleanly prompt for user input from the model layer? Depending upon certain values in the model, I may need to prompt the user for input during the middle of a process.

For example, we'll take a hypothetical PO entry process. Say after the user hits a button in the view it calls the presenter passing in the PO details from the view. The presenter then calls the model to validate and insert the new PO into a collection of POs. One of the validation checks in the model is to make sure another PO has not already been entered with the same items. If one has, the app needs to prompt the user to confirm the PO is not a duplicate. The app is currently deep into the model. How do I go all the way back up to the view to retrieve the operator input, then return to the model code where it left off to finish the PO entry process?

A: 

In a paper I read on presenter first it was suggested that the presenter had dependencies on the model and on the view and subscribed to events from both the model and the view.
This would mean that you could raise an event from the model at the point where the processing cannot continue. The presenter would handle the event from the model by calling some method on the view (which would prompt the user). The return value from the method on the model would then be returned to the model (an in/out parameter to the event like the EventArguments subclasses used by .NET).

Hamish Smith
A: 

You might want to look at some form of notification interaction between your model and the other components of your UI pattern.

Martin Fowler wrote about it here.

As far as being deep in the model, getting user input, then going back into the model: don't. Your controller is responsible for modifications to your model, and should do the validation before attempting to change your model. This may require separating your validation from your update code.

micahtan
Can you put logic in the controller layer to hit the model for validation, then if that passes hit the model for the update? The way I understood the layer to work is that its only responsiblity is to update the view. All of the application logic would reside within the model. Maybe that is one of the differences between the MVP and MVC patterns? It makes sense to me that the controller layer could do something like:If model.NewPOIsValid(PO) ThenModel.AddPO(PO)End if