How To Use Lazy Initialization with VB.NET and Visual Basic 2005

This is part one of a two part article and source code which explains the lazy initialization pattern and how to implement it in an order entry scenario with VB.NET and Visual Basic 2005. In object-oriented programming (OOP), lazy initialization is the strategy of improving application performance by delaying the creation of an object, the calculation of a value, or some other process until the first time the object is needed. In part one, lazy initialization is explained and a Customer class which implements lazy implementation is created. In part two, ADO.NET code is implemented for the Customer class’ lazy initialization processes to load orders and order lines from a database.

In object-oriented programming (OOP), lazy initialization is the strategy of improving application performance by delaying the creation of an object, the calculation of a value, or some other process until the first time the object is needed.

This pattern can increase the performance of code that consumes ‘resource-hungry’ objects. Resource-hungry objects are those objects that are costly to create in terms of the memory they require, the time it takes to create them, or both.

Order entry is an example where lazy initialization can improve performance.

Object-oriented design (OOD) for the order entry process typically calls for a customer object (Customer) which contains the customer’s order objects (Orders collection), which in turn contains order line objects (OrderLines collection).

Loading orders and order lines into a customer object is often costly in terms of the time it takes to load them and the computer memory required.

With lazy initialization, when a customer object is initialized, its orders and order lines collections are not loaded. They are only loaded when and if they are needed.

This pattern is often implemented by maintaining a flag indicating whether the initialization process has taken place. Each time the desired thing is summoned, the flag is tested. If it’s ready, it is returned. If not, it is initialized on the spot.

Customer Class from Part 1 Source Code

 

‘ Example will use the .NET 2.0 Generic List(Of T) type

‘ to implement the Order and OrderLine collections.

Imports System.Collections.Generic

 

Public Class Customer

 

    ‘ Name field.

    Private m_Name As String

    ‘ Name property.

    Public Property Name() As String

        Get

            Return Me.m_Name

        End Get

        Set(ByVal value As String)

            Me.m_Name = value

        End Set

    End Property

 

    ‘ ID field.

    Private m_ID As Integer

    ‘ ID property.

    Public Property ID() As Integer

        Get

            Return Me.m_ID

        End Get

        Set(ByVal value As Integer)

            Me.m_ID = value

        End Set

    End Property

 

    ‘ Orders field.

    ‘ Intialized to nothing when Customer object is created.

    Private m_Orders As List(Of CustomerOrder) = Nothing

 

    ‘ Lazy initialization flag for Orders collection.

    Private ordersLoaded As Boolean = False

 

    ‘ Orders property.

    Public Property Orders() As List(Of CustomerOrder)

        Get

            ‘ Lazy intialization code for Orders.

            If Not ordersLoaded Then

                ‘ Load orders here. (Will be done in part two.)

                Me.ordersLoaded = True

            End If

            Return Me.m_Orders

        End Get

        Set(ByVal value As List(Of CustomerOrder))

            Me.m_Orders = value

        End Set

    End Property

 

 

    ‘ OrderLines field.

    ‘ Intialized to nothing when Customer object is created.

    Private m_OrderLines As List(Of OrderLine) = Nothing

 

    ‘ Lazy initialization flag for OrderLines collection.

    Private ordersLinesLoaded As Boolean = False

 

    ‘ OrderLines property.

    Public Property OrderLines() As List(Of OrderLine)

        Get

            ‘ Lazy initialization code.

            If Not Me.ordersLinesLoaded Then

                ‘ Load order lines here. (Will be done in part two.”

                Me.ordersLinesLoaded = True

            End If

            Return Me.m_OrderLines

        End Get

        Set(ByVal value As List(Of OrderLine))

            Me.m_OrderLines = value

        End Set

    End Property

 

End Class

 

Public Class CustomerOrder

    ‘ Will implement in part two.

End Class

 

 

Public Class OrderLine

    ‘ Will implement in part two.

End Class

 

In part two, ADO.NET code is implemented for the Customer class’ lazy initialization processes to load orders and order lines from a database.

Example Application Screen Shot

The example application loads a list of customers from the Northwind database.

When a user clicks the row header for a customer row, a Customer object is created.

When the user clicks the LoadOrderLines button the the Customer object loads the customers orders and orders lines.

Customer Class from Part 2 Source Code

The customer class’ LoadOrders and LoadOrderLines methods are implemented.

Public Class Customer

 

    ‘ Name field.

    Private m_Name As String

    ‘ Name property.

    Public Property Name() As String

        Get

            Return Me.m_Name

        End Get

        Set(ByVal value As String)

            Me.m_Name = value

        End Set

    End Property

 

    ‘ ID field.

    Private m_ID As String

    ‘ ID property.

    Public Property ID() As String

        Get

            Return Me.m_ID

        End Get

        Set(ByVal value As String)

            Me.m_ID = value

        End Set

    End Property

 

    ‘ Constructor

    Private Sub New()

        ‘ Parameterless constructor made private so only

        ‘ a parameterized constructor can be called.

    End Sub

 

    Public Sub New(ByVal name As String, ByVal id As String)

        Me.Name = Name

        Me.ID = id

    End Sub

 

 

    ‘ Orders field.

    ‘ Intialized to nothing when Customer object is created.

    Private m_CustomerOrders As New List(Of CustomerOrder)

 

    ‘ Lazy initialization flag for Orders collection.

    Private ordersLoaded As Boolean = False

 

    ‘ Orders property.

    Public Property CustomerOrders() As List(Of CustomerOrder)

        Get

            ‘ Lazy intialization code for Orders.

            If Not ordersLoaded Then

                Me.LoadOrders()

            End If

            Return Me.m_CustomerOrders

        End Get

        Set(ByVal value As List(Of CustomerOrder))

            Me.m_CustomerOrders = value

        End Set

    End Property

 

    ‘ OrderLines field.

    ‘ Intialized to nothing when Customer object is created.

    Private m_OrderLines As New List(Of OrderLine)

 

    ‘ Lazy initialization flag for OrderLines collection.

    Private ordersLinesLoaded As Boolean = False

 

    ‘ OrderLines property.

    Public Property OrderLines() As List(Of OrderLine)

        Get

            ‘ Lazy initialization code.

            If Not Me.ordersLinesLoaded Then

                Me.ordersLinesLoaded = True

            End If

            Return Me.m_OrderLines

        End Get

        Set(ByVal value As List(Of OrderLine))

            Me.m_OrderLines = value

        End Set

    End Property

 

    Private Sub LoadOrders()

        ‘ Load customers orders here.

        Dim theNorthwindDataSet As New NorthwindDataSet

        Dim x As New NorthwindDataSetTableAdapters.OrdersTableAdapter

        x.FillByCustomerId(theNorthwindDataSet.Orders, Me.ID)

        For Each row As NorthwindDataSet.OrdersRow In theNorthwindDataSet.Orders

            Me.m_CustomerOrders.Add(New CustomerOrder(row.OrderID, row.CustomerID, row.OrderDate))

        Next

        Me.ordersLoaded = True

        Me.LoadOrderLines()

    End Sub

 

    Private Sub LoadOrderLines()

        ‘ Load orderLines here.

        Dim theNorthwindDataSet As New NorthwindDataSet

        Dim x As New NorthwindDataSetTableAdapters.Order_DetailsTableAdapter

        For Each item As CustomerOrder In Me.m_CustomerOrders

            x.FillByOrderId(theNorthwindDataSet.Order_Details, item.OrderID)

            For Each order_detail As NorthwindDataSet.Order_DetailsRow In theNorthwindDataSet.Order_Details

                Me.m_OrderLines.Add(New OrderLine(order_detail.OrderID, order_detail.ProductID, order_detail.UnitPrice))

            Next

        Next

        Me.ordersLinesLoaded = True

    End Sub

 

    Public Overrides Function ToString() As String

        Return Me.ID & ” “ & Me.Name

    End Function

 

End Class

 

Advertisements

~ bởi duriangroup trên Tháng Sáu 1, 2007.

 
%d bloggers like this: