Fun with events
After seeing how much fun everybody else had with events, I tried to get my own version together.
I tried my luck with delegating the actual event handling to EventHandlers, which do the actual job. So the called QueryOpen Method looks like this:
Sub delegate_Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
Forall evhandler In listEventHandlers
Call evHandler.Queryopen(Source , Mode, Isnewdoc , Continue )
If Not Continue Then Exit Sub
End Forall
End Sub
The actuall handlers extend a basic EventHandler (which does nothing in each object method) with the required functionality. The rest of the design is modeled after the MVC Pattern as described in this presentation/example DB.
Fun: binding object methods to an event and not having the object around at "call time" (like when you forget to declare your object in Declaration and just do a <code>dim controller as New BaseDocumentController(source)</code> on the PostOpen-Event of the Form) will do funny things in the debugger: after the QueryClose Event the client goes into a endless loop. Without debugger, just nothing happens.
This is the complete code for Tims example:
Private Const ERR_ABSTRACT_INSTANTIATION = 1000
Private Const MSG_ABSTRACT_INSTANTIATION = | Attempt to instantiate as an object instance the abstract class |
Public Class BaseDocumentController
%REM
The Controller Part of MVC
Binds to a UIDocument, delegates all events to the EventHandlers
%END REM
Private fUIDoc As NotesUIDocument
Private fModel As BaseDocumentModel
Private fIsInitialized As Boolean
Private listEventHandlers List As AbstractDocumentEventHandler
Sub new(uidoc As NotesUIDocument)
If Not uidoc Is Nothing Then
Call initializeWithUIDoc(uidoc)
End If
End Sub
Public Function getUIDoc() As NotesUIDocument
Set getUIDoc = fUIDoc
End Function
Public Function initializeWithUIDoc(uidoc As NotesUIDocument)
If uiDoc Is Nothing Then
Error 1111, "Need a UI Document"
End If
Set fUIDoc = uidoc
Set fModel = New BaseDocumentModel( fUIDoc.Document)
fIsInitialized = True
Call bindEvents()
Call setupEventHandler()
End Function
%REM
adds the default EventHandler (Validation) the to eventhandler List
Can be overwritten to extend the EventHandler List, needs to call
BaseDocumentController..setupEventHandler() to get the basic EventHandler
setup or you need to add validation yourself
%END REM
Function setupEventHandler()
Dim evHandler As New ValidationHandler(fModel)
Call Me.addEventHandler( "validation", evHandler)
Dim evHandler2 As New AuditTrailHandler(fModel)
Call Me.addEventHandler( "AuditTrail", evHandler2)
End Function
Public Function addEventHandler(evName As String, evHandler As AbstractDocumentEventHandler)
Set listEventHandlers(evName) = evHandler
End Function
Private Function bindEvents()
On Event Queryopen From fUIDoc Call delegate_Queryopen
On Event Postopen From fUIDoc Call delegate_Postopen
On Event Querymodechange From fUIDoc Call delegate_Querymodechange
On Event Postmodechange From fUIDoc Call delegate_Postmodechange
On Event Queryrecalc From fUIDoc Call delegate_Queryrecalc
On Event Postrecalc From fUIDoc Call delegate_Postrecalc
On Event Querysave From fUIDoc Call delegate_Querysave
On Event Postsave From fUIDoc Call delegate_Postsave
On Event Querysend
|