Upgrading COM Components

Upgrading COM+ Components

Unfortunately, because a Visual Basic .NET COM+ services application has to be implemented in quite a different manner than a Visual Basic 6 COM+ services application, the Upgrade Wizard cannot do much for your Visual Basic 6 COM+ components. It upgrades the business logic, database code, and other parts of your application, but it doesn t upgrade the transaction attributes. The implementation differences are not a one-to-one mapping, so upgrading the parts dealing with COM+ support is best left to the developer. This leaves you with a fair bit of work to do on your own. To better illustrate what is going on here, let s look at a Visual Basic 6 sample called SimpleTransaction, included on the companion CD. This is an ActiveX DLL project with a single class, SimpleClass, that is marked as RequiresNewTransaction. The SimpleClass.cls code looks like this:

Implements ObjectControl Implements IObjectConstruct Dim ctxt As ObjectContext Dim connStr As String Const m_module As String = "SimpleTransaction.SimpleClass" Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object)    connStr = pCtorObj.ConstructString End Sub Private Sub ObjectControl_Activate()    Set ctxt = GetObjectContext(m_module) End Sub Private Function ObjectControl_CanBePooled() As Boolean    ObjectControl_CanBePooled = True End Function Private Sub ObjectControl_Deactivate() End Sub Public Sub DoTasks()    On Error GoTo HandleError    Dim conn As Connection    Set conn = New Connection         Dim sqlCmd As String    sqlCmd = "This is a sample query"         conn.Open connStr    conn.Execute sqlCmd         ctxt.SetComplete    Exit Sub      HandleError:    ctxt.SetAbort End Sub

After running this sample project through the Upgrade Wizard, you have a Visual Basic .NET class that looks like this:

Option Strict Off Option Explicit On Public Class SimpleClass    Implements COMSVCSLib.ObjectControl    Implements COMSVCSLib.IObjectConstruct        Dim ctxt As COMSVCSLib.ObjectContext    Dim connStr As String        Const m_module As String = "SimpleTransaction.SimpleClass"        Private Sub IObjectConstruct_Construct(ByVal pCtorObj As Object) _      Implements COMSVCSLib.IObjectConstruct.Construct      ' UPGRADE_WARNING: Couldn't resolve default property of object       ' pCtorObj.ConstructString...      connStr = pCtorObj.ConstructString    End Sub        Private Sub ObjectControl_Activate() Implements _       COMSVCSLib.ObjectControl.Activate      ctxt = COMSVCSLibAppServer_definst.GetObjectContext(m_module)    End Sub        Private Function ObjectControl_CanBePooled() As Boolean _       Implements COMSVCSLib.ObjectControl.CanBePooled      ObjectControl_CanBePooled = True    End Function        Private Sub ObjectControl_Deactivate() _          Implements COMSVCSLib.ObjectControl.Deactivate    End Sub        Public Sub DoTasks()     On Error GoTo HandleError     Dim conn As ADODB.Connection     conn = New ADODB.Connection          Dim sqlCmd As String     sqlCmd = "This is a sample query"          conn.Open(connStr)     conn.Execute(sqlCmd)          ctxt.SetComplete()    Exit Sub        HandleError:       ctxt.SetAbort()    End Sub End Class

As you can see, the Upgrade Wizard leaves the work of implementing the COM+ parts of the class to you. While this task is quickly accomplished for this simple example, you should be aware that bigger applications will take time if the classes have a large set of transactional components. You will need to refer to the Visual Basic 6 version of each class to ensure that you transfer the correct transactional attributes to Visual Basic .NET. Otherwise, you may see unexpected and potentially undesirable behavior.

Using the previous example, let s walk through the steps necessary to get the class working:

  1. Add a reference to System.EnterpriseServices.

  2. Change the class to inherit from EnterpriseServices.ServicedComponent, and remove all COMSVCS Implements statements.

  3. Add the necessary COM+ attributes to the class (Transaction, ConstrutionEnabled, ObjectPooling, and so on).

  4. Change the COM+ interface methods Activate, Construct, and Deactivate to be member methods that override base methods on Serviced Component.

  5. Add the AutoComplete attribute to the DoTasks method. Remove the explicit commit and abort code.

After you ve completed all of these steps, the class looks like this:

Option Strict Off Option Explicit On  Imports System Imports System.EnterpriseServices <Transaction(TransactionOption.RequiresNew), ConstructionEnabled(), ObjectPooling()> Class SimpleClass    Inherits ServicedComponent    Dim connStr As String    Const m_module As String = "SimpleTransaction.SimpleClass"    Protected Overrides Sub Construct(ByVal constructionString _ As String)       connStr = constructionString    End Sub    <AutoComplete()> Public Sub DoTasks(ByVal fail As Boolean)       Dim conn As ADODB.Connection       conn = New ADODB.Connection()       Dim sqlCmd As String       sqlCmd = "This is a sample query"       conn.Open(connStr)       conn.Execute(sqlCmd)    If fail Then ContextUtil.SetAbort()    End Sub End Class

Notice that the DoTasks method does not explicitly commit or abort the transaction except when a logical condition is true. This helps to demonstrate that you can still explicitly commit or abort your transactions. If you encounter a method in which the commit and abort logic is too tangled to reasonably remove, you can forgo using the AutoComplete attribute altogether and commit your transactions manually.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

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