Parsing Interface Events

[Previous] [Next]

So far in the application model, we've used two different and generalized COM interfaces: IViewer to view and IMaint to maintain information. Both these interfaces expose the GetById method.

Because this method (plus a few others) has been using the intReturnType argument to determine whether to return a recordset or an Extensible Markup Language (XML) data set, the interface event procedures have had some parsing to do. Here, for instance, is the code parsing the IMaint_GetById event in the HorseManager class:

Private Function IMaint_GetById(vntId As Variant, _ Optional intReturnType As Integer) As Variant Select Case intReturnType Case ADBReturnTypeRS ' Return a recordset. Set IMaint_GetByID = GetByIdAsRS(CLng(vntId)) Case ADBReturnTypeXML ' Not yet implemented Err.Raise vbObjectError, "HorseManager, IViewer_GetById", _ "XML method not yet available" Case Else ' Invalid intReturnType Err.Raise vbObjectError, "HorseManager, IViewer_GetById", _ "Return type supplied is not valid" End Select End Function

Looking at the corresponding interface event procedure for the IViewer interface, we can easily see that its code is exactly the same:

Private Function IViewer_GetById(vntId As Variant, _ Optional intReturnType As Integer) As Variant Select Case intReturnType Case ADBReturnTypeRS ' Return a recordset. Set IViewer_GetById = GetByIdAsRS(CLng(vntId)) Case ADBReturnTypeXML ' Not yet implemented Err.Raise vbObjectError, "HorseManager, IViewer_GetById", _ "XML method not yet available" Case Else ' Invalid intReturnType Err.Raise vbObjectError, "HorseManager, IViewer_GetById", _ "Return type supplied is not valid" End Select End Function

This repetition is kind of silly, especially since we know that we need to enhance this code to support other kinds of return values, such as XML data sets, later on. Furthermore, this method might also find its way into other interfaces. It's hardly likely that the two interfaces the class now implements will suffice in the future. So it must be better to put this code in a single place and call it from every interface event that asks for a horse by ID. We like to think of this piece of code as a parser. So we decided to create a ParseGetById method, and here it is:

Private Function ParseGetById(vntId As Variant, _ Optional intReturnType As Integer) As Variant Select Case intReturnType Case ADBReturnTypeRS ' Return a recordset. Set ParseGetById = GetByIdAsRS(CLng(vntId)) Case ADBReturnTypeXML ' Not yet implemented Err.Raise vbObjectError, "HorseManager, ParseGetById", _ "XML method not yet available" Case Else ' Invalid intReturnType Err.Raise vbObjectError, "HorseManager, ParseGetById", _ "Return type supplied is not valid" End Select End Function

This change was a minor change; all we had to do was to cut out part of the code from the GetById interface event methods and paste it into the new ParseGetById function. The only thing we had to change in the ParseGetById function was the name of the method, which occurs in four different places in the preceding code snippet, as the boldface type indicates.

The rest of this minor design improvement was even easier to accomplish. All we had to do was to make the interface event procedures call this parser. The code we created was identical in the two procedures, but in this case it was simpler. It didn't do anything other than call the parser. Here's the code for both these procedures:

Private Function IMaint_GetById(vntId As Variant, _ Optional intReturnType As Integer) As Variant Set IMaint_GetById = ParseGetById(vntId, intReturnType) End Function Private Function IViewer_GetById(vntId As Variant, _ Optional intReturnType As Integer) As Variant Set IMaint_GetByID = ParseGetById(vntId, intReturnType) End Function

Given the fact that most of our interface event procedures have some parsing to do, the decision to remove redundant code by creating parsers for all of them was easy to make. We're not going to waste space on showing how we did that. Instead, let's take a look at the other decision we made, a much bigger and more important one. Incidentally, the need for making the second decision came out of the work of implementing the first one.



Designing for scalability with Microsoft Windows DNA
Designing for Scalability with Microsoft Windows DNA (DV-MPS Designing)
ISBN: 0735609683
EAN: 2147483647
Year: 2000
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net