Save-able broken rules

Save-able broken rules

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


thomasc@magenic.com posted on Wednesday, March 21, 2007

So, I have overriden my Save as such:

public override Cart Save()

{
if (this.IsValid == false)
{
   try{
      BrokenRule rule = this.BrokenRulesCollection.GetFirstBrokenRule("SubTotal");
      //if we find the broken rule, brute-force the save. Otherwise, save the elegant way.
      return base.Save(true);
   }
catch { }
}
return base.Save();
}

I have a custom rule on my sub-total, that I want to be broken.  However, because I am using it to display a warning in my website, and want the rule in place.  However, I dont want the broken rule to impede the Save..its simply an invalid object state that I need my users to address at some point (before they place an order, and I use CSLA to enforce this rule) 

My code above looks like it might work, but if you look at BusinessBase, the Save(bool force) overload has the following:

if (forceUpdate && IsNew){........

My object is not "New", but I want to force the update.  Any tips on how to tackle this issue?

xal replied on Wednesday, March 21, 2007

You could mark the broken rule as a warning and that won't stop you from saving.
Just do this inside your rule method:

e.Severity = RuleSeverity.Warning


Andrés

SonOfPirate replied on Wednesday, March 21, 2007

FWIW - I ran into a similar situation where I allow users to save "draft" versions of the BO that may or may not violate my business rules because the BO is in an indeterminant state.  However, because the ultimate purpose of the BO was something beyond simply saving, such as submitting for approval, I was able to remove the IsValid check from the Save routine and place the check in my Submit method instead.

 

thomasc@magenic.com replied on Wednesday, March 21, 2007

e.Severity = RuleSeverity.Warning works REALLY well.  Ideal for what I'm doing.  I have a similar Rule in my order object, where I prevent the order from being placed, by leveraging default severity level, which blocks the save.

Thanks!!

SonOfPirate replied on Wednesday, March 21, 2007

I should clarify my solution because as nice as using RuleSeverity is, I found that it isn't...accurate(?).  What I mean is, the rule is only a warning when you try to save the object but it is still a true broken rule (Error) when it comes to the overall state of the object.  And even when you read Rocky's comments in the source code, it is clear that the purpose of a Warning is for a rule that does not make the object invalid.

What you are describing, and the situation I ran into, is a case where the object IS invalid but you still want to be able to persist it to the database.  Setting this rule to a Warning level means that your object will never be invalid if this rule is broken - assuming you apply the logic uniformly throughout.

The way I implemented what I described above was actually through an overload for the Save method that accepts a boolean value indicating whether or not the object should be validated before saving - much like the CausesValidation property on web Button controls.  The parameterless overload (as supplied by Rocky) defers to the new method passing true for the argument - so its behavior hasn't changed.  My new method uses the value of the parameter to decide whether to check IsValid before allowing the save; if false, it saves it regardless of the state.

This allows me to defer checking IsValid on BOs that I want to be able to save regardless of their validity.  I am then able to conditionalize other actions by checking the IsValid property at that time.  IMO, this is a cleaner and more consistent solution that provides the flexibility to handle either situation.

HTH

 

thomasc@magenic.com replied on Wednesday, March 21, 2007

In my scenario, I have a shopping cart, where the business rule is really just a warning..  When the user checks out, my site has an Order class.  Placing orders with a total dollar value of < $25 is illegal, therefore I simply have a business rule in both places.  The order has the standard severity that blocks the Save.

So, severity was the ticket for me, bigtime.  Worked immediately....and was much better than the goofy stuff I was doing to try and override the save, and figure out a way to suppress the broken rules collection from killing the Save.

Copyright (c) Marimer LLC