I am currently doing something similar to the following on every save() I do.
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
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.SaveUndoResultr = 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) ThenSelect Case rCase MicroFour.StrataFrame.Data.SaveUndoResult.AbortedWithBrokenRulesDim strJson As String = LitJson.JsonHelper.WriteJsonString(Me.BrokenRules)Utility.SystemUtilities.ErrHandler.WriteEventLog(strJson, "", Diagnostics.EventLogEntryType.Error)Throw New Exceptions.BrokenRulesException(strJson)
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
r = MicroFour.StrataFrame.Data.SaveUndoResult.Cancelled OrElse _
r = MicroFour.StrataFrame.Data.SaveUndoResult.CompletedWithExceptions) Then
Utility.SystemUtilities.ErrHandler.WriteEventLog(strJson,
Case MicroFour.StrataFrame.Data.SaveUndoResult.FailedWithExceptions
Throw New ApplicationException()
End Select
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).