EditableBusinessList: Delete child object - execute business rules

EditableBusinessList: Delete child object - execute business rules

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


cdkisa posted on Thursday, April 15, 2010

I am trying to delete a child object from an editable list. I have some business logic that I want executed when a user tries to 'delete' a record that checks whether or not the record is in use (referential integrity).

However, given the code example below, my overridden methods are not being called. Am I doing something wrong? Is there a better way to do this?

Example:
'In the BLL...
Public Class MyListClass
	Inherits MyBaseListClass(Of MyListClass, MyChildClass)
	
	'Overridden to get item by key value
	Default Public Overloads ReadOnly Property Item(ByVal keyValue As Object) As MyChildClass
		Get
			For Each child As MyChildClass In Me
				If child.KeyProperty.Equals(keyValue) Then
					Return child
				End If
			Next
			Return Nothing
		End Get
	End Property
	
	'Overridden to implement business logic
	Public Overloads Function Remove(ByVal child As MyChildClass) As Boolean
		If MyChildClass.CanDeleteRecord(child.KeyProperty)
			MyBase.Remove(child)
		Else
			Throw New BusinessException(ExceptionMessages.GetString("RecordCannotBeDeleted"))
		End If
	End Function
	'Overridden to implement authorization logic
	Public Overloads Function Remove(ByVal keyValue As Object) As Boolean
		If MyChildClass.CanDeleteObject(keyValue) Then
			Remove(Me(ageGroupNo))
		Else
			Throw New System.UnauthorizedAccessException(ExceptionMessages.GetString("UnauthorizedDeleteAccess", "MyChildClass"))
		End If
	End Function
	
	'...other code
	
End Class
'In the UIL...
'Class for editing data grids
Public Class UserControlListBase(Of T As MyBaseListClass(Of T, C), C As MyBaseClass(Of C))
	Inherits UserControl
	'Holds the current business object 
	Protected Property BusinessObject() As T
		Get
			Dim result As Object = Nothing
			Try
				result = CType(Session("CurrentBO"), T)
			Catch ex As Exception
				result = Nothing
			End Try
			Return result
		End Get
		Set(ByVal value As T)
			Session("CurrentBO") = value
		End Set
	End Property
	'Occurs when a user chooses to delete a record in the grid
	Protected Sub dataGrid_RowDeleting(ByVal sender As Object, ByVal e As DeletingEventArgs)
		Try
			Dim keyValue As Object = e.Keys(Me._keyFieldName)
			Dim child As C = Me.BusinessObject(keyValue)
			
                        'This should throw an exception stating that the record cannot be deleted
			Me.BusinessObject.Remove(child)
			
		Catch ex As Exception
			e.Cancel = True
			HandleException(ex)
		End Try
	End Sub
	
	'...other code
	
End Class

Copyright (c) Marimer LLC