How to conditionally bypass a Custom Rule

How to conditionally bypass a Custom Rule

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


WISDev posted on Friday, June 04, 2010

I have a situation where my Contact object as child to Customer object.  The contact rule requires an email address.  Everything is good and happy until a new rule is introduced in.  The new rule: when we mass import customers, the email rule should be relaxed (ignored).

I looked at the possibility to add a validation, but this would not work--or should I say--I don't think it would work.  Any suggestions???

Thanks!

RockfordLhotka replied on Friday, June 04, 2010

There are a couple options. If you want to completely turn off all rules, you can use SuppressRuleChecks (a method on ValidationRules) to turn off rule processing. This is designed for batch operations - though usually people turn off rule checking, load the object with data, then call CheckRules() to run the rules.

The other option is to make your rule smarter. That implies you have some way for the business object and/or rule to know that you are doing a batch load, so the rule can detect that condition and therefore not run.

One way to do that is by having a property on your object - though that seems a little ugly. Another is to use Csla.ApplicationContextLocalContext to store a value indicating you are in batch mode.

WISDev replied on Friday, June 04, 2010

Thank you Rocky!

This is exactly what I needed.  While trying to find an answer, I implemented the the second option you proposed.  I created a flag and turned it on during the batch processing.

-mr

Copyright (c) Marimer LLC