IValidatableObject and IDataErrorInfo

At the beginning it was IDataErrorInfo. Next , it came IValidatableObject. And , because some GUI frameworks implement one and not other, I must think to a common way to validate once .

My choice was to validate with IValidatableObject and IDataErrorInfo just re-send data.

I made an example with a class User which has UserName, UserEmail and UserPassword.

Without much ado, this is the code:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class User : IValidatableObject, IDataErrorInfo
   {
       public string UserEmail { get; set; }
       public string UserName { get; set; }
       public string UserPassword { get; set; }
 
       public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
       {
           if (string.IsNullOrEmpty(UserEmail))
               yield return new ValidationResult("UserName", new string[1] { "UserName" });
 
           if (string.IsNullOrEmpty(UserEmail))
               yield return new ValidationResult("UserEmail", new string[1] { "UserEmail" });
 
           if (string.IsNullOrEmpty(UserEmail))
               yield return new ValidationResult("UserPassword", new string[1] { "UserPassword" });
 
            
       }
 
       //IDataErrorInfo - you can copy this code to each class
       public string Error
       {
           get { return this[""]; }
       }
 
       public string this[string columnName]
       {
           get
           {
               string ret = null;
               var data = this.Validate(new ValidationContext(this, null, null));
               switch (columnName)
               {
                   case "":
 
                       foreach (var item in data)
                       {
                           ret += item.ErrorMessage + Environment.NewLine;
                       }
                       break;
                   default:
                       foreach (var item in data)
                       {
                           if (item.MemberNames.Contains(columnName))
                               ret += item.ErrorMessage + Environment.NewLine;
                       }
                       break;
               }
               return ret;
           }
       }
   }

And you can verify like this:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
User u = new User();
             
            Console.WriteLine("-----from  IDataErrorInfo");
            string err = (u as IDataErrorInfo)[""];
            Console.WriteLine(err);
             
            Console.WriteLine("-----from  IvalidatableObject");
            foreach(var item in (u as IValidatableObject).Validate(new ValidationContext(u, null, null)))
            {
                Console.WriteLine(item.MemberNames.First());
            }
 
 
            Console.WriteLine("-----from  Validator");
            try
            {
                Validator.ValidateObject(u, new ValidationContext(u, null, null), true);
            }
            catch (ValidationException EX)
            {
 
                Console.WriteLine("JUST " + EX.Message);
            }

You can find the code also there
Validation