StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      


12»»

Trapping Save() ErrorsExpand / Collapse
Author
Message
Posted 09/18/2007 5:36:44 PM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 08/26/2008 10:34:32 AM
Posts: 78, Visits: 242
Hi Folks

I am currently doing something similar to the following on every save() I do.

Select Case Me.Save

Case MicroFour.StrataFrame.Data.SaveUndoResult.AbortedWithBrokenRules

Throw New BrokenRulesException(Me.BrokenRules)

Case MicroFour.StrataFrame.Data.SaveUndoResult.FailedWithExceptions

Throw New ApplicationException("The save failed.")

End Select

I trap for broken rules and bubble them up with one custom exception class, and throw exceptions for the other results. Because of this I wind up with an ugly Case statement for every save. Is there any reason not to move this logic to the ErrorSaving event of the business object's themselves to centralize this or is this just the best way to do it?

Thanks!

Clay

Post #11559
Posted 09/18/2007 8:24:00 PM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 08/26/2008 10:34:32 AM
Posts: 78, Visits: 242
Hi All

I wanted to add a little more detail to what I am trying to accomplish.

When performing Save(True) operations on multiple business objects at one time and one of the Save operations fail, I want to rollback the transaction then throw a specific exceptin as described below.

  1. When a save fails due to broken rules, I want to throw a custom exception containing these broken rules to the presentation layer (we are currently converting the broken rules collection to a JSON string and passing that string).
  2. When a save failes due to any other reason, I want to rethrow that exception up the chain
  3. When either of the above happen, I want to write either the exception or the broken rules to the application log. The idea is that our presentation tier validation should nearly eliminate broken rules from being written to the app log (I hope!).

I thought about overriding the Save() method and putting the logic there. I also thought about putting the logic in the Business Rules Checked event but I'm thinking that would miss straight out failures.

What I'm looking for is mostly an idea of what would be the best practice and maybe how others are doing this type of operation,. I hope this makes sense!

Clay

Post #11562
Posted 09/19/2007 9:07:48 AM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 2 days ago @ 5:02:56 PM
Posts: 2,682, Visits: 1,882
Well, the ErrorSaving event only catches exceptions that are caused during the save, and only if the ErrorSavingMode is set to ContinueOnError.  Your best bet would be to create a BoBase class and override the OnBusinessRulesChecked method.  If the business rules are broken, then throw your exception containing the business rules. 

Another option would be to create the BoBase class and override Save().  Call MyBase.Save() and check the return value within your own Save() and check the select case there.  You could even show your form with the broken rules there if you don't mind taking that portion of the UI out of the UI...


www.bungie.net
Post #11575
Posted 09/19/2007 10:23:17 AM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 08/26/2008 10:34:32 AM
Posts: 78, Visits: 242
Hi Ben

This is what I've come up with...what do you think?

Public Overrides Function Save(ByVal b As Boolean) As MicroFour.StrataFrame.Data.SaveUndoResult

Dim r As MicroFour.StrataFrame.Data.SaveUndoResult

r = MyBase.Save(b)

If Not (r = MicroFour.StrataFrame.Data.SaveUndoResult.Success OrElse _

r = MicroFour.StrataFrame.Data.SaveUndoResult.Cancelled OrElse _

r = MicroFour.StrataFrame.Data.SaveUndoResult.CompletedWithExceptions) Then

Select Case r

Case MicroFour.StrataFrame.Data.SaveUndoResult.AbortedWithBrokenRules

Dim strJson As String = LitJson.JsonHelper.WriteJsonString(Me.BrokenRules)

Utility.SystemUtilities.ErrHandler.WriteEventLog(strJson, "", Diagnostics.EventLogEntryType.Error)

Throw New Exceptions.BrokenRulesException(strJson)

Case MicroFour.StrataFrame.Data.SaveUndoResult.FailedWithExceptions

Throw New ApplicationException()

End Select

End If

End Function

Post #11581
Posted 09/19/2007 12:49:34 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 2 days ago @ 5:02:56 PM
Posts: 2,682, Visits: 1,882
Yep, looks good to me.  Centralizes everything nice and neat.  Just make sure to change the Inherits line (or the : line) when you create a new business object from our template.


www.bungie.net
Post #11588
Posted 09/19/2007 12:55:21 PM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 08/26/2008 10:34:32 AM
Posts: 78, Visits: 242
Thanks Ben

Is the ErrorSaving event the only place I can examine the exceptions that cause FailedWithExceptions?

 

Post #11593
Posted 09/19/2007 1:00:41 PM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: 08/26/2008 10:34:32 AM
Posts: 78, Visits: 242
Hi Ben

I'm not tracking on this part...

Just make sure to change the Inherits line (or the : line) when you create a new business object from our template.

I generally create my own base classes using your templates then inherit from those.

Post #11595
Posted 09/19/2007 1:06:28 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: 2 days ago @ 5:02:56 PM
Posts: 2,682, Visits: 1,882
Well, actually, the business object isn't going to ever return FailedWithExceptions from save()...

If an exception is encountered from within the save, then the exception will just be bubbled up if the ErrorSavingMode is FailOnError and the return value will be CompletedWithExceptions if the ErrorSavingMode is ContinueOnError.  If ContinueOnError is used, then yes, the ErrorSaving event will contain the exceptions thrown. 

So, the FailedWithExceptions is used for the SaveByForm() method, not the Save() method.  So, if you're going to be saving all of the business objects at once through the form's Save() method, then I you would want to override the SaveByForm() method and do the same thing (but I don't think that one is overridable, so if you're saving through the form's Save() method, then we might need to work out something different).


www.bungie.net
Post #11596