Validation with Priority help

Validation with Priority help

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


jasona22 posted on Monday, July 08, 2013

I have a rule written as such:

BusinessRules.AddRule(Of BusObj)(_statusProperty, Function(obj As BusObj) obj.Status = "some test value", "Error Message", RuleSeverity.Error)

I am not having success adding the "With { .Priority = 1 }" to this.  Where does this go?  I can use this with other definitions, but can't find any references for the above version.

 

Thanks

JonnyBee replied on Monday, July 08, 2013

Hi,

Yes, when these RuleExtensions were written we regarded them pretty much like DataAnnotations - that always have Priority = 0.
But overall the rule extensions is just a simplified way of adding Lambda rules with an optionally strong typed lambda statement.

 You could add your own RuleExtension method that accepts Priority or write is as a Lambda rule or create an ordinary BusinessRule.

jasona22 replied on Tuesday, July 09, 2013

 

So what I think you said is that I cannot add Priority=1 to this type of rule and need to write a custom rule instead.

I don't know what you mean by write my own RuleExtension.  It seems the problem with using frameworks is not only do you have to be an expert in your business, but you have to become an expert in the framework your using as well so you can extend it and write parts that don't actually exist -- especially when it almost works.

Thank you for your reply.  I will confer with my team and we will decide how to proceed.  Maybe we just don't do the priority thing for this scenario.

 

 

JonnyBee replied on Tuesday, July 09, 2013

ExtensionMethods is language feature of C# and VB.NET 

So the method your were using to register the rule is  (from an implementation perspective) Extension Methods to BusinessRules class that simplifies the use of the existing Lambda rule. These ecxtension methods is placed in the Csla.Rules.RuleExensions class. 

You _can_ look at the Csla.Rules.RuleExtensions class and create your own additional extension methods or create a lambda rule directly or create a custom rule. 

For my own preferences is typically do not use Lambda rules as they are not so easily testable - so I prefer to create separate rule classes that I can create good unit tests on.  

jasona22 replied on Tuesday, July 09, 2013

 

I completely understand not writing lambda rules as a best practice.  Besides just testing, it puts the functionality across your business library in each class instead of in a singular custom rule that is a fix once/use often scenario.   However, in this case, we are using CodeSmith to generate this particular rule.  To prevent having to CodeSmith a custom rule and the references to it, we are writing it as a lambda.  While looking at it won't seem like a reuse of code, it would still be fixed one place, regenerated and repaired everywhere (provided a bug was found). 

The only reason I wanted to have a priority = 1 on it was because we already have a "required" rule for the property and I didn't want this rule to run when the value was "empty string".  The work around is to put an String.Empty check in my lambda rule.  It solves the problem differently.

 

Thanks for the info on the Extension Methods.  I have been so tucked away in code generation (note framework usage comment from before) that I have lost touch with many of the changes.  Dang, lambda expressions are still difficult for me.  I  come from VB6 days and while do well in .net, I do not do as well with some of the newer features (to me newer).  PFM (Pure Freakin Magic) of MEF for instance.  Lambda, Extension methods, etc are all "new" to me and not a tool I always know to reach for. 

Thanks again for your help.

JonnyBee replied on Tuesday, July 09, 2013

Hi,

Just one more possible solution.

It would be perfectly valid to have a Required rule at Priority = -10 (or any negative number) tho. 

And you could also change the BusinessRules.ProcessThroughPriority setting (remember this is an instance setting and must be done in DataPortal_XYZ methods) to negative number so that the Lambda rule would  not be executed when a previous rule is broken with Severity = Error. .  

Copyright (c) Marimer LLC