ForceUpdate flag

ForceUpdate flag

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


JoeFallon1 posted on Thursday, October 30, 2008

I was trying to use forceUpdate this morning and ran into this contradiction.

I have a new BO which is invalid because the PK already exists in the database. So instead of an Insert I would like to force and Update.

So I call myBO.Save(True).

Public Function Save(ByVal forceUpdate As Boolean) As T
If forceUpdate AndAlso IsNew Then
' mark the object as old - which makes it
' not dirty
MarkOld()
' now mark the object as dirty so it can save
MarkDirty(True)
End If
Return Me.Save()
End Function

This changes the state of the BO from New to Old and keeps it Dirty. Perfect!

The problem is the last line of code:  Return Me.Save()

This calls the normal parameterless Save method which has this code:

If Not IsValid AndAlso Not IsDeleted Then
  Throw New Validation.ValidationException( My.Resources.NoSaveInvalidException)
End If

See the problem?

My BO was invalid because of a rule that checks to see if the PK is in the database. It is, so now I force an Update. Except my BO is *still* invalid!! So the Save will fail.

What I need is the opportunity to call ValidationRules.CheckRules() after the state of the BO is changed to re-run the rules. Some rules check for IsNew as part of the rule (including the PK database check rule) so a re-run of the rules would make the object valid. At that point a Save would work.

So, there are a few ways to resolve this:

1. Add the call to ValidationRules.CheckRules() to the existing Save(forceUpdate) method after the call to MarkDirty.

2. Create a new method named ForceUpdate which does everything except the save and let the developer call ValidationRules.CheckRules() and then call .Save().

#2 is more flexible but also more dangerous. I am not sure if you want to expose this new method to the UI developer or not. They have to know to call CheckRules after setting this. They might forget. Not sure if that would be an issue or not because then the current behavior would be the result.

Note: I solved the issue a different way using my internal framework code so I don't have a pressing need for this. But maybe someone else will.

Joe

ajj3085 replied on Thursday, October 30, 2008

Perhaps your validation rule can call MarkOld and MarkDirty, instead of returning that the rule is broken?

Copyright (c) Marimer LLC