rsbaker0:Incidentally, you can even call one rule method from another, so (for example) in your dependency between status and zip code above you could conditionaly invoke the StringRequired rule method from within your hypothetical StatusMayRequireZipCode rule.
I also use this technique. Works quite nicely.
Hi Jonny,
I'm trying to write code according with your interesting idea of coupling a "StopIfNotCanWrite" rule with CanWriteProperty(..) authorization method.
What gets me confused is that if I declare a StopIfNotCanWrite rule, and AddRule(StopIfNotCanWrite, prop, 0) (with priority 0), and the rest of the rules regarding the same "prop" with priority 1, in order to short circuit the Validation Rules Manager, I must make StopIfNotCanWrite short circuit the Rules Manager and then return "false". That will leave my BO in an invalid state (exactly the opposite of what I want to accomplish). Or when I short circuit the StopIfNotCanWrite rule, I should also return "true" and inform of the writing capabilities via a custom RuleArgs?
Could you please provide a short example of this behavior?
Thanks!
Troncho
Look at the CSLA 3.x addon rules here:
The StopIfNotCanWrite rule should look like this (included in the link above):
/// <summary> /// Rule indicating whether the user is authorized /// to change the property value. /// Will always be silent and never set rule to broken. /// </summary> /// <param name="target">Target object.</param> /// <param name="e">Rule arguments.</param> /// <returns>true</returns> /// <remarks> /// Combine this property with short-circuiting to /// prevent evaluation of other rules in the case /// that the user isn't allowed to change the value. /// </remarks> public static bool StopIfNotCanWrite(object target, RuleArgs e) { bool isAuthorized = true; var business = target as BusinessBase; if (business != null && !string.IsNullOrEmpty(e.PropertyName)) isAuthorized = business.CanWriteProperty(e.PropertyName); if (!isAuthorized) { e.StopProcessing = true; } // rule should always return true (be silent) but will stop processing // if user cannot modify the value. return true; }
And I would typically always add this rule with Priority = -1 as this can be combined with DataAnnotationRules that are always added with Priority = 0 and unless specified rules is added with default Priority = 0.
Ok, now I get it
It's the rule that check on the "CanWriteProperty(..)", not the other way round.
This is much simpler than I was elaborating on my mind. I was thinking about inheriting RuleArgs to send some in/out info "from" the "CanWriteProperty()" and make the validation rule return some special ResultRuleArgs value to CanWriteProperty() in order to check if the property being evaluated is writeable.
Thanks also on the (-1) priority advice on this special rule hookup. You're right about DataAnnotations (I completely forgot about them in this case...).
And finally thanks for the link to the codeplex project. I'll check it out right now.
As usual, Jonny, your contribution is highly appreciated
Thanks,
Troncho
Just took the last whitespace from (%20) from the link you sent me, so it doesnt fail:
Jonny, can I possibly enter directly to the source Codeplex project?
Thanx,
Troncho
yes, you can download all the source from the Source tab.
I've downloaded file by file of the "3-6-3-N2" project. But I don't see a "source tab".
I entered "www.codeplex.com" with my user and I don't seem to find the project either.
Is there any special link I should address?
Thanx,
Troncho
BTW, the StopIfNotCanWrite rule works PERFECT! Thanks again!
Goto this page and then select the download button:
http://cslacontrib.codeplex.com/SourceControl/changeset/view/100173
Thanks!
Copyright (c) Marimer LLC