DataPortal Exception gets "swallowed" by OnDeserialized()

DataPortal Exception gets "swallowed" by OnDeserialized()

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


BBM posted on Friday, April 18, 2008

Hi,

I uncovered a situation today while running some test code (NUnit stuff).  In its simplest form the problem is...

1)  I have a composite BO that contains an Editable Collection of EditableChild BO's.  The main BO has a factory "Get" method that returns the whole composite object from a string key.

2)  The main BO needs to react to changes in the child objects in the collection.  The child objects raise custom events to which the main BO subscribes.

3)  The main BO overrides OnDeserialized() to subscribe to the custom child events after the BO is "Fetched".

4)   I'm testing the case where the key passed in the Shared Get method does not exist in the database.  The DataPortal_Fetch override method in the BO throws an exception when it realizes the database query doesn't return anything.  It looks like this...

Using dr As New SafeDataReader(cm.ExecuteReader)

   If dr.Read() Then

      Fetch(dr)

   Else

      Throw New System.Data.DataException("Could not retrieve an Item for " & Id.ToString)

   End If

End Using

5)  Once the exception is thrown, I see CSLA catch my exception and wrap it in a CallMethodException and then wraps that in a DataPortalException.  I'm expecting the DataPortal to return Nothing at this point and the DataPortalException to be caught by my client code.

6)  However, even though the exception was thrown in middle of the Fetch process, my OnDeserialized handler is called.  That routine attempts to connect the main BO to the custom events in each child of the collection.  Since Fetch() was interrupted, the collection is not populated and the attempt to access it causes the system to throw a NullReferenceException.  That exception is what gets caught in my NUnit test.  I don't know what happens to the DataPortalException.

I guess my question is why, even though Fetch() was interrupted, does the DataPortal attempt to pass back the incomplete BO and trigger the OnDeserialized handler?

I'm using CSLA 3.03 and a "local" (same machine) Remote Dataportal (using .Net Remoting). 

Thanks for your help.

BBM

RockfordLhotka replied on Sunday, April 20, 2008

This is part of the behavior of the DataPortalException - it brings back the object as it was at the time of the exception. The purpose behind doing this is to provide you with complete information for debugging or otherwise attempting to understand the reason for the exception.

Copyright (c) Marimer LLC