DataPortal.Delete succeeded?DataPortal.Delete succeeded?
Old forum URL: forums.lhotka.net/forums/t/1820.aspx
gmclean posted on Wednesday, November 22, 2006
It has been a while since I have worked on CSLA related projects so maybe I am missing something simple.
I want to use CSLA to delete a business object directly. In this BO I have defined an override for dataportal delete and it seems to work fine except for one thing. If all the data is deleted successfully I would like to pass back a boolean letting the calling program know the deletes were OK. I tried creating a public boolean property and setting it to true in the override of dataportal delete if all went well in the delete transaction. The property gets set but the value does not persist when I look at the BO in the calling program.
Any suggestions on a good way to let the calling program know the BO was deleted successfully?
Thanks for your help,
George
Q Johnson replied on Wednesday, November 22, 2006
You're clearly not going to get a return value all the way back from your DP_Delete routine and I don't see how you could pass a variable by reference in hopes of getting it back modified to suit, either.
So the only other option appears to be some sort of test to see if the persisted object still exists. This shouldn't be hard should it? Your calling routine must know the ID value of the object (else how could it have deleted it directly?). So you can either (a) rely on logic you may already have in a fetch routine that can gracefully handle the absence of the requested object or (b) create an object (Command type?) to check for it.
Happy Thanksgiving,
William replied on Wednesday, November 22, 2006
In my case, I am using stored procedure to perform the deletion. And, the stored procedure will return a status value to tell this. In case of deletion fail, my DataPortal_Delete will throw an exception to inform the UI.
Regards,
William
Bayu replied on Thursday, November 23, 2006
Hey,
Isn't the fact that you didn't catch an exception the 'success' flag that you need?
Or don't you trust your sql or sproc?
Then you could implement a shared/static Exists function like this:
Public Shared Function Exists(ByVal id As Guid) As Boolean
Dim result As ExistsCommand = DataPortal.Execute(Of ExistsCommand)(New ExistsCommand(id))
Return result.Exists
End Function
<Serializable()> _
Private Class ExistsCommand
Inherits CommandBase
Private _ID As Guid
Private _Exists As Boolean
Public Sub New(ByVal id As Guid)
Me._ID = id
End Sub
Public ReadOnly Property Exists() As Boolean
Get
Return Me._Exists
End Get
End Property
Protected Overrides Sub DataPortal_Execute()
Using cn As New MySqlConnection(Database.CrmDBConnection(True))
cn.Open()
Using cm As MySqlCommand = cn.CreateCommand
cm.CommandType = CommandType.Text
cm.CommandText = "SELECT COUNT(CustomerID) FROM Customer WHERE CustomerID = @id"
cm.Parameters.Add("?id", Me._ID)
Dim count As Integer = CInt(cm.ExecuteScalar)
Me._Exists = (count > 0)
End Using
End Using
End Sub
End Class
Note that in this case deletion takes 2 DB hits, but I guess your needs then would validate this.
Regards,
Bayu
gmclean replied on Monday, November 27, 2006
Thanks for all the suggestions. I had thought about another
trip to the database to check that the record was deleted but I was hoping to
avoid what I thought was an unnecessary database hit. I guess if
data_portal.delete throws an exception that is really all that is needed since
I can handle the exception in the calling program.
Thanks again for the suggestions,
George
Copyright (c) Marimer LLC