How to write to e.PropertyName in RuleArgs

How to write to e.PropertyName in RuleArgs

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


bgilbert posted on Tuesday, May 15, 2007

I have several business objects in which I'm using ITypedList to dynamically expose sub-properties (properties of contained objects). I have validation rules associated with the contained objects, but I want to return the e.Description to binding so that it's associated with a different property, one of the ones I'm dynamically creating. In other words:

ValidationRules.AddRule(AddressOf ActivityRequired, "Activity")

    Protected Shared Function ActivityRequired(ByVal target As Object, _
            ByVal e As Csla.Validation.RuleArgs) As Boolean
        If CType(target, RoutingStep).Activity Is Nothing Then
            e.Description = "Activity required"
            Return False
        End If
        Return True
    End Function

In this example, I want the setting of the Activity object to trigger the validation rule, but I want a property called ActivityDescr to have the associated e.Descripition. I envision something like:

        If CType(target, RoutingStep).Activity Is Nothing Then
            e.PropertyName = "ActivityDescr"
            e.Description = "Activity required"
            Return False
        End If

However, PropertyName is ReadOnly.

Is there any other way/workaround to accomplish this?

david.wendelken replied on Tuesday, May 15, 2007

My first inclination is to ask you: "Which object really owns that business rule?"

Is it true that for an Activity to exist, it must have an Activity Description?

If so, the rule belongs to the ActivityDescr property inside of Activity, and not to the object that contains the Activity.

Activity (assuming it is subclassed from BusinessBase), has a collection of broken rules in it.

The object containing Activity can access that collection and display the concatenation of its own broken rules and Activity's broken rules.

Take a look at RuleBusinessBase in the CslaContrib project (link on the forum home page) for an example of how to modify BusinessBase to publically expose the broken rules collection.

 

bgilbert replied on Wednesday, May 16, 2007

David,

Thanks for your reply. I thiunk I probably didn't explain it well enough. The containing class needs to validate that it contains an Activity object. _activity is declared like this:
Private _activity As Activity = Nothing

When the containing class checks its rules, it only needs to check for the existence of an activity object. This cannot be done from within the activity class; it has to be done by the containing object. Once it determines that the activity object is nothing, I want it to return the e.Description through another property of the containing object, called ActivityDescr. This property is a "virtual" property in that it's created using a PropertyDescriptor. The PropertyDescriptor returns the Descr property of any contained activity object. However, since, when it fails, there is no activity object, there is also no Descr property. I want to somehow intervene in the BrokenRules' relationship with data binding so that my containing class can tell the UI, through the ActivityDescr property, that the object has not been set.

Thanks again for any help.

Barry

david.wendelken replied on Wednesday, May 16, 2007

bgilbert:
David,

Thanks for your reply. I thiunk I probably didn't explain it well enough. The containing class needs to validate that it contains an Activity object. _activity is declared like this:
Private _activity As Activity = Nothing

When the containing class checks its rules, it only needs to check for the existence of an activity object. This cannot be done from within the activity class; it has to be done by the containing object. Once it determines that the activity object is nothing, I want it to return the e.Description through another property of the containing object, called ActivityDescr. This property is a "virtual" property in that it's created using a PropertyDescriptor. The PropertyDescriptor returns the Descr property of any contained activity object. However, since, when it fails, there is no activity object, there is also no Descr property. I want to somehow intervene in the BrokenRules' relationship with data binding so that my containing class can tell the UI, through the ActivityDescr property, that the object has not been set.

Thanks again for any help.

Barry

Um, I think I understand... :)

There is a way to force dependencies between properties for a given rule,  ValidationRules.AddDependentProperty.

I'm in the process of writing up some tests to see if it works the way I think it does.  If I remember correctly, the ProjectTracker app has an example of it on a custom rule concerning the start and end dates.

If it works the way I suppose it does, you would put the rule on ActivityDescr and mark Activity as a dependent property.  

Whoever figures it out first should post it. :)

 

david.wendelken replied on Wednesday, May 16, 2007

Is ActivityDescr a denormalized access point to the underlying _activity.Description property?

If so, why not just put the rule on it to begin with?

bgilbert replied on Wednesday, May 16, 2007

David,
Great. I had to use both techniques. Check the rules on the ActivityDescr property and also use AddDependentProperty with Activity. It now works like a charm.

Thanks for you help.

Barry

Copyright (c) Marimer LLC