Click to read part one -> Part 1
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
mike mcintyre http://www.getdotnetcode.com