Base Class Validation

Topics: Developer Forum
May 23, 2007 at 5:39 PM
Hi Simon,

I have the following scenarion: -

- I have a PointOfContact class with validation rules defined (e.g. for Surname)
- I have a Resident class with validation rules defined (e.g. RoomNumber)
- Resident inherits from PointOfContact

What's the best approach if I have a Resident object that needs validation to ensure that both sets of rules are run i.e. in the above scenarion, Surname and RoomNumber rules are evaluated?

Thanks,

- Andrew

Thx
May 24, 2007 at 10:05 AM
Even if I could copy the rules from the base class (Point of Contact) to a place where they'd get executed during a Resident class validate that'd be cool:)

I can get round it but it does require some maintenance if the base class(es) validation changes, something I would rather avoid:)

Thx,

- Andrew
May 27, 2007 at 12:29 PM
Andrew

Rules are inherited. That is when applied to a type they will automatically evaluate for all children of that class. So if you apply them to the PointOfContact they should automatically be applied to Resident. If this is not working for you there may be a bug.
May 28, 2007 at 3:35 PM
Thanks Simon, I'll double check what's going on and I'll get back to you.

- Andrew
May 29, 2007 at 3:45 PM
I didn't realise rules were inherited, glad it operates like that though! :)

Reason why it wasn't working for me is that the rule group used for the Resident class wasn't represented in the validation for the Surname property in the Point of Contact validation! Oops:( Sorted now.

Thanks,

- Andrew
May 30, 2007 at 9:14 AM
Hi Simon,

I think I may have been a little hasty with my previous response......my tests were themselves faulty:(

Now they're fixed I traced through a validation on Resident to see if it was firing the Surname rule on PointOfContact which it doesn't seem to be doing.....it just fires the ResidentType validation which is declared for the Resident type.

I'm using the following call to validate my Resident object: -

IList<ValidationResult> results = PropertyValidationManager.TryValidate(entity, ruleSet, context);

Is it something to do with me using the static TryValidate method?

Any thoughts?

Thanks,

- Andrew
May 30, 2007 at 1:19 PM
Edited May 30, 2007 at 1:42 PM
Andrew
I wrote some unit tests to try and mimic what you are doing. See code below or \Validation\ValidationFramework.Tests\PropertyValidationManager\InheritanceTests.cs in the latest checkin.
But all the tests passed.
Can you give me some more info on what you are doing. Perhaps post up some code that can reproduce the problem?

  [TestFixture]
    public class InheritanceTests
    {
 
        public class BaseClass
        {
            private string property1;
 
            [RequiredStringRule]
            public string Property1
            {
                get
                {
                    return property1;
                }
                set
                {
                    property1 = value;
                }
            }
        }
 
        public class InheritedClass : BaseClass
        {
            private string property2;
 
            [RequiredStringRule]
            public string Property2
            {
                get
                {
                    return property2;
                }
                set
                {
                    property2 = value;
                }
            }
        }
 
        [Test]
        public void InstanceValidate()
        {
            BaseClass baseClass = new BaseClass();
            PropertyValidationManager baseClassPropertyValidationManager = new PropertyValidationManager(baseClass);
            baseClassPropertyValidationManager.Validate();
            Assert.AreEqual(1, baseClassPropertyValidationManager.ValidatorResultsInError.Count);
 
            InheritedClass inheritedClass = new InheritedClass();
            PropertyValidationManager inheritedClassPropertyValidationManager = new PropertyValidationManager(inheritedClass);
            inheritedClassPropertyValidationManager.Validate();
            Assert.AreEqual(2, inheritedClassPropertyValidationManager.ValidatorResultsInError.Count);
        }
 
 
        [Test]
        public void StaticValidate()
        {
            BaseClass baseClass = new BaseClass();
            IList<ValidationResult> baseClassValidationResults = PropertyValidationManager.Validate(baseClass, null, null);
            Assert.AreEqual(1, baseClassValidationResults.Count);
 
            InheritedClass inheritedClass = new InheritedClass();
            IList<ValidationResult> inheritedClassValidationResults = PropertyValidationManager.Validate(inheritedClass, null, null);
            Assert.AreEqual(2, inheritedClassValidationResults.Count);
        }
        [Test]
        public void InstanceTryValidate()
        {
            BaseClass baseClass = new BaseClass();
            PropertyValidationManager baseClassPropertyValidationManager = new PropertyValidationManager(baseClass);
            baseClassPropertyValidationManager.TryValidate();
            Assert.AreEqual(1, baseClassPropertyValidationManager.ValidatorResultsInError.Count);
 
            InheritedClass inheritedClass = new InheritedClass();
            PropertyValidationManager inheritedClassPropertyValidationManager = new PropertyValidationManager(inheritedClass);
            inheritedClassPropertyValidationManager.TryValidate();
            Assert.AreEqual(2, inheritedClassPropertyValidationManager.ValidatorResultsInError.Count);
        }
 
 
        [Test]
        public void StaticTryValidate()
        {
            BaseClass baseClass = new BaseClass();
            IList<ValidationResult> baseClassValidationResults = PropertyValidationManager.TryValidate(baseClass, null, null);
            Assert.AreEqual(1, baseClassValidationResults.Count);
 
            InheritedClass inheritedClass = new InheritedClass();
            IList<ValidationResult> inheritedClassValidationResults = PropertyValidationManager.TryValidate(inheritedClass, null, null);
            Assert.AreEqual(2, inheritedClassValidationResults.Count);
        }
    }