Originally posted to the Sidebar by "Welbog"...
I'm working on bug patrol for a generic data-entry app. It has a grid view that lets users input data, as well as a set of other views in addition to the grid, such as a regular winform-like deal. One of the things the app has is a trigger-like system, in which classes of a certain interface are called at certain points in the life of a record. So if a record is deleted from any view, data about this deletion is passed to an object invoked via reflection. The idea being the 'trigger' doesn't have to care about what view the user is using, just the data.
Anyway, onto the anonymized code. This is the "BeforeDelete" function, which lets the action decide whether or not the delete action should be permitted. If it returns true, the delete can continue. If it returns false, it must stop.
Public Function BeforeDelete(ByVal controller As Object, _ ByVal GUIDs() As String) As Boolean Implements BeforeDelete Dim RecordFound As Boolean If TypeOf controller Is SpecificController Then Dim SpecController As SpecificController = CType(controller, SpecificController) Dim RowIndex As Integer = SpecController.View.Grid.ActiveRowIndex SpecController.View.Grid.ActiveRowIndex = RowIndex Dim ItemGuid As String = CStr(SpecController.View.Grid.Cell(RowIndex, 3).Value) RecordFound = DataFacade.GetRecordCount(ItemGuid) If RecordFound = False Then Throw Exception(...) Else Return True End If End If End FunctionWTFs:
- Throwing an exception instead of returning false, which is what it should be doing.
- DataFacade.GetRecordCount returns a boolean. I have no idea why this is. I don't think I want to know.
- Getting the active row and then immediately setting the active row to that row. WTF?
- The biggest WTF of all, is that this function goes to look up the value of a GUID in the grid, and specifically the grid, so that this event can be bypassed in the other view types. Guess what ItemGuid contains once it gets to the GetRecordCount call? Yeah. It contains GUIDs(0). Even better: you can delete multiple records from the grid, and this function ignores that and only tries to delete the active one.