custom NoDuplicate rule and the call of ValidationRules.CheckRules()

custom NoDuplicate rule and the call of ValidationRules.CheckRules()

Old forum URL: forums.lhotka.net/forums/t/2578.aspx


kids_pro posted on Thursday, March 22, 2007

I tried to implement a simple custom validation rule "NoDuplicate" but it doesn't seem to work well.
I got the following error message when I tried to load the Parent object.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 192:        private bool NoDuplicate(object sender, Csla.Validation.RuleArgs e) {
Line 193: Beneficiaries parent = (Beneficiaries)this.Parent;
Line 194: foreach (Beneficiary b in parent) {
Line 195: if (b.VillGis == _villGis && !ReferenceEquals(b, this)) {
Line 196: e.Description = "This village already exist in beneficiary list.";

I add the NoDuplicate like this:

        protected override void AddInstanceBusinessRules() {
            ValidationRules.AddInstanceRule(NoDuplicate, "VillGis");
        }

To clarify here is the class diagram

Project Diagram

I don't know if it necessary to call MarkAsChild() in the Beneficiary constructor or not?
If this error cause by my call to ValidationRules.CheckRules() in the Beneficiary constructor?

My NoDuplicate rule is simple:
Project has beneficiary which identify by location, and the location can't appear more than one.

RockfordLhotka replied on Friday, March 23, 2007

You can't use the Parent property in the constructor because it hasn't been set yet. BLB doesn't call SetParent() until after the child object has been created.

kids_pro replied on Friday, March 23, 2007

Is that mean I do not necessary call MarkAsChild() in this constructor overload?

Copyright (c) Marimer LLC