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
mike mcintyre http://www.getdotnetcode.com