Custom rule problem

Custom rule problem

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


MeByMySelf posted on Monday, March 24, 2008

Hi,

I know that this question been asked here lots of times but I couldn't make any suggestion to work for me.
Instead of hijacking other's topics I decided to create new one.

I have simple class

    public class Document : BusinessBase<Document>
    {
        #region Business Methods
        Guid _id;
        string _name;
        Guid _folder;

/*
.........  MORE members here

*/
        public Guid FolderId
        {            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _folder;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (!_folder.Equals(value))
                {
                    _folder = value;
                    PropertyHasChanged();
                    //ValidationRules.CheckRules();

                }
            }
        }

        protected override void AddBusinessRules()
        {
           ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Name");
           ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength,
                    new Csla.Validation.CommonRules.MaxLengthRuleArgs("Name", 150));
            ValidationRules.AddRule<Document>(CheckForEmptyGuid, "FolderID");
 
        }

        private static bool CheckForEmptyGuid<T>(
            T target, Csla.Validation.RuleArgs e) where T : Document
        {
            if ( target.FolderId.Equals(Guid.Empty) )
            {
                e.Description = "The value of the FolderId should not be empty ";
                return false;
            }
            else
                return true;
        }
  }



as you can see I have added custom rule that checks if value of Guid field left empty (Guid.Empty)
But when I am testing it I the rule broken even if it actually not


            Document d = Document.NewDocument();
            d.Name = "Test";
            d.FolderId = Guid.NewGuid();
            if (d.IsValid)
                MessageBox.Show("Valid");
            else
                MessageBox.Show("Invalid - " + d.BrokenRulesCollection[0].Description);

If I uncomment     
 //ValidationRules.CheckRules();
     line in Property setter everything will be OK. It seems that PropertyChanged() does not trigger CheckRules for some reason.

as per Rocky's advise in one of the similar threads I did step-by-step execution of the code - There is no exceptions thrown.

Any advise on how to solve this problem?

Copyright (c) Marimer LLC