IsValid() on Save of an Entity

Oct 16, 2007 at 4:28 PM
I have used other 3-tier designed frameworks in the past and have recently found yours. One method that seemed to be missing was an IsValid() method that checks for broke rules on an entity being set. I would love to see a collection of BrokenRules that was accessible for an entity that could be set based on validation of each property. Also the ability to add validation rule(s) for each property that would get fired with a call to the entity IsValid() and then set the collection of brokenRules() would be helpful for BLL logic. Thoughts?
Coordinator
Oct 16, 2007 at 7:17 PM
It's on our list! I have a prototype (semi) working that uses a validation framework that does just that. It will generate attributes on properties for length and required fields (based on the database setup), and you'll be able to add your own through code. It returns a collection of validation errors.

I will hopefully have some time to get that integrated in shortly.

Thanks for the feedback!
Coordinator
Oct 16, 2007 at 7:19 PM
I forgot to add this to the first post, but you can simulate this manually for the time being using the OnSaving() event. You would override OnSaving() and then add your rules there. Then you can throw an exception if validation fails. It's manual, but it will work.
Feb 25, 2009 at 3:37 AM
Edited Feb 25, 2009 at 3:37 AM
It has been a while since this post, but I have since gotten back into the framework and have begun using it on a few projects.  One thing that I found a bit confusing with the validation is that if I create a new Customer(), it is inherently valid even though all required fields are null/string.empty.  I think this is de to the fact that there is no implementation of nullable types (yet?).  What I would expect to see is that when I create a new object, all required fields that had not been set would fire a validation error. Thoughts? For example, if the Customer object below had two columns that did not allow null (FirstName and LastName perhaps), I would expect ValidationErrors list to include messages for both fields since niether was "set".


            StringBuilder sb = new StringBuilder();
           
            try
            {
                Customer obj = new Customer();
                if (obj.IsValid)
                {
                    obj.Save();
                }
                else
                {
                    foreach (ValidationError err in obj.GetValidationErrors())
                        sb.AppendLine(err.PropertyName + " - " + err.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                sb.AppendLine(ex.Message);
            }