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:

 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:

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