AddDependentProperty misunderstanding?

AddDependentProperty misunderstanding?

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


ajj3085 posted on Friday, October 24, 2008

Hi,

I think maybe I'm misunderstanding... but if I have the following:
            ValidationRules.AddRule<PartOrder>(
                EnsureSupplierBuiltBy,
               SupperProperty
            );
            ValidationRules.AddDependentProperty(
                BuiltByProperty,
                SupplierProperty,
                true
            );

        private static bool EnsureSupplierBuiltBy( PartOrder order, RuleArgs e ) {
            bool result;
            string supplier, builtBy;

            result = true;
            supplier = order.ReadProperty( SupplierProperty );
            builtBy = order.ReadProperty( BuiltByProperty );

            if ( string.IsNullOrEmpty( supplier ) && string.IsNullOrEmpty( builtBy ) ) {
                result = false;
                e.Description = Properties.Resources.PartOrderSupplierOrBuiltByRequired;
            }
            if ( !( string.IsNullOrEmpty( supplier ) || string.IsNullOrEmpty( builtBy ) ) ) {
                result = false;
                e.Description = Properties.Resources.PartOrderSupplierXorBuilt;
            }

            return result;
        }
Now, shouldn't the rule be run when BuiltBy is changed?  And if it is, shouldn't there be a broken rule tied to the BuiltBy property as well as the Supplier property?

Thanks
Andy

sergeyb replied on Friday, October 24, 2008

I think you need to add the same rule to your second property.  Dependent property simply means that when rules for one of them are run, rules for the other one are run as well, and BuiltBy property in your example has no rules.

 

            ValidationRules.AddRule<PartOrder>(
                EnsureSupplierBuiltBy,
               SupplierProperty
            );

            ValidationRules.AddRule<PartOrder>(
                EnsureSupplierBuiltBy,
                BuiltByProperty
            );
            ValidationRules.AddDependentProperty(
                BuiltByProperty,
                SupplierProperty,
                true
            );

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: ajj3085 [mailto:cslanet@lhotka.net]
Sent: Friday, October 24, 2008 3:34 PM
To: Sergey Barskiy
Subject: [CSLA .NET] AddDependentProperty misunderstanding?

 

Hi,

I think maybe I'm misunderstanding... but if I have the following:
            ValidationRules.AddRule<PartOrder>(
                EnsureSupplierBuiltBy,
               SupperProperty
            );
            ValidationRules.AddDependentProperty(
                BuiltByProperty,
                SupplierProperty,
                true
            );

        private static bool EnsureSupplierBuiltBy( PartOrder order, RuleArgs e ) {
            bool result;
            string supplier, builtBy;

            result = true;
            supplier = order.ReadProperty( SupplierProperty );
            builtBy = order.ReadProperty( BuiltByProperty );

            if ( string.IsNullOrEmpty( supplier ) && string.IsNullOrEmpty( builtBy ) ) {
                result = false;
                e.Description = Properties.Resources.PartOrderSupplierOrBuiltByRequired;
            }
            if ( !( string.IsNullOrEmpty( supplier ) || string.IsNullOrEmpty( builtBy ) ) ) {
                result = false;
                e.Description = Properties.Resources.PartOrderSupplierXorBuilt;
            }

            return result;
        }
Now, shouldn't the rule be run when BuiltBy is changed?  And if it is, shouldn't there be a broken rule tied to the BuiltBy property as well as the Supplier property?

Thanks
Andy


ajj3085 replied on Friday, October 24, 2008

Ok, that makes sense.   Don't know why I thought it'd work the other way... it's been a long day I guess.

rsbaker0 replied on Friday, October 24, 2008

The rule runs, but if it's broken then the error (broken rule, error provider, etc.)  get associated with original property referenced in AddRule, not the dependent property whose change caused it to fire.

All the dependent property association does is force the other rules to fire -- they are still associated with the original property.

I have taken advantage of this to associate an message with something user can see versus a hidden item that is actually broken.

 

JoeFallon1 replied on Friday, October 24, 2008

In addition to the other comments above, you have a typo in the first thread:

ValidationRules.AddRule<PartOrder>(
                EnsureSupplierBuiltBy,
               SupperProperty                   <---- TYPO
            );
            ValidationRules.AddDependentProperty(
                BuiltByProperty,
                SupplierProperty,
                true
            );

Joe

Copyright (c) Marimer LLC