MVVM / WCF Validation and exception handling - General advice please

Pregunta

What is the best way to perform validation and exception handling? There seems to be many varied ways of doing it and none of them seem completely ideal.

My thinking is that I need to validate the input to the server (WCF) methods to protect the server, but I also need some kind of validation on the client. The problem I see on the client is how to communicate the problems - in some cases it's fine to have
the red border around text boxes, but what if an exception gets thrown - how do I get that exception and display an error if everything is happening through data binding?

Respuestas

2. The GUI should implement checks as ValidationRules or through IDataErrorInfo that make sure that those exceptions never get thrown.

The reasoning behind this is as follows: a webservice is intended to be called by other software, and the preferred way to tell a piece of software that it has done something wrong is an exception. As you already wrote, this is necessary for the webservice
in order to protect itself.

The GUI is intended to be used by a human being, and is supposed to "translate" between the webservice and the human. So, part of its job is to make sure that the human does not enter anything that makes the webservice throw an exception. The best way to
do that is to offer only valid options to the user, and where that isn't possible, second best are clear and helpful validation messages which tell the user what to change in order to get a valid result, before the data is sent to the webservice.

If there is too much logic in the webservice which you don't want to write again in the GUI, i.e. ViewModel, you still have to keep in mind that the GUI should be able to display localized error messages, while the webservice normally should deal with culturally
invariant data. So, if there are validation checks that can only be done by the webservice, you should have some part in the ViewModel that catches exceptions thrown by the webservice and translates them into error messages, which can be shown to the user
in a convenient place.

However, if you want to take a shortcut, you could still set ValidatesOnExceptions to true on the bindings. That would cause the Bindings to display a validation error whenever changing the bound property causes an exception.

2. The GUI should implement checks as ValidationRules or through IDataErrorInfo that make sure that those exceptions never get thrown.

The reasoning behind this is as follows: a webservice is intended to be called by other software, and the preferred way to tell a piece of software that it has done something wrong is an exception. As you already wrote, this is necessary for the webservice
in order to protect itself.

The GUI is intended to be used by a human being, and is supposed to "translate" between the webservice and the human. So, part of its job is to make sure that the human does not enter anything that makes the webservice throw an exception. The best way to
do that is to offer only valid options to the user, and where that isn't possible, second best are clear and helpful validation messages which tell the user what to change in order to get a valid result, before the data is sent to the webservice.

If there is too much logic in the webservice which you don't want to write again in the GUI, i.e. ViewModel, you still have to keep in mind that the GUI should be able to display localized error messages, while the webservice normally should deal with culturally
invariant data. So, if there are validation checks that can only be done by the webservice, you should have some part in the ViewModel that catches exceptions thrown by the webservice and translates them into error messages, which can be shown to the user
in a convenient place.

However, if you want to take a shortcut, you could still set ValidatesOnExceptions to true on the bindings. That would cause the Bindings to display a validation error whenever changing the bound property causes an exception.