There are two general types for implementing validation logic:
Share the library. Example: "RIA Services + Silverlight".
Advantages: easy to implement.
Disadvantages: no compatibility (only .NET); the required client update for each library replacement.
Implementation of a general method check in the service part. Pros: interoperability, no need to update the client when changing the verification logic
Minuses: it can be difficult because it is only for you
If we use SOA, it’s better to use the second option, unless you only use applications in your company, where .NET is everywhere.
Example
Consider a general example. We have a windows / wpf form. And there are two fields: "last name" of type string, "age" of type int; and the "Save" button. We need to do some client side validation
1) for some users the "Save" button is disabled;
2) the surname cannot be empty, and the maximum length is 256;
3) age cannot be less than 0;
Call save method -
void Save(string surname, int age);
Create a second method in the service that returns a PermissonAnswerDTO object type with validation information;
PermissonAnswerDTO SaveValidate(string surname, int age);
and the main verification method
// If arguments are wrong [FaultContract(typeof(NotSupportedException))] // If the user have permisson to invoke this method [FaultContract(typeof(CustomNotEnoughPermission))] PermissonAnswerDTO Validate(string methodName, object[] methodParams);
Validation
Call Validate("SaveValidate", null) when the window loads. If an exception of type CustomNotEnoughPermission is selected, we block the "Save" button.
If the user can save, then call Validate("SaveValidate", object[2]{"Surname", "-60"}; user data Validate("SaveValidate", object[2]{"Surname", "-60"}; ;. -60 is invalid, so we get a response object of type PermissonAnswerDTO with the information:
ParameterName: "age", ExceptionMessage: "age cannot be less then null".
And we can gracefully show this information to the user.
My thought about this is that one day Microsoft will do it and offer a new technology, as it always does. Basically, Microsoft technologies are really not as revolutionary as they are advertised. Examples are the Windows Identity Foundation and Reactive Extensions.
Full example
[DataContract] public class ParameterExceptionExplanaitonDTO { [DataMember] public string ParameterName; [DataMember] public string ExceptionMessage; } [DataContract] public class PermissonAnswerDTO { [DataMember] public bool IsValid; [DataMember] public ParameterExceptionExplanaitonDTO[] ParameterExceptions; } public class Service1 : WcfContracts.IService1 {