The Only Data Access Layer You'll Ever Need?

Putting It All Together

With the basic code described so far, you need to do significantly less work to flesh out the rest of any DAL. You will probably want a data access class for groups of business objects. These classes will contain code to initiate read and write calls and the read and write handlers themselves.

Listing 6 shows a basic CustomerAccess class that kicks off reads (and writes, not shown), and contains the read (and write) handlers. It demonstrates how to implement a read event handler and it also demonstrates how to invoke the reflection-read.

You can use this exemplar to implement all of your custom data access classes.

Tip: Although you do need to write some additional data access code for your custom objects, you could easily write a CodeDOM code generator or use a tool such as CodeRush to really speed up this part of the data access development.

Listing 7 shows the custom Customer object populated by the CustomerAccess class. If you need a collection of objects, use List<T>, the generic collection class.

Listing 7: An Example of a Basic Custom Business Object

<Serializable()> _
Public Class Customer
Private FCustomerID As String
<ColumnName("CustomerID")> _
Public Property CustomerID() As String
Get
Return FCustomerID
End Get
Set(ByVal value As String)
FCustomerID = value
End Set
End Property
Private FCompanyName As String
Public Property CompanyName() As String
Get
Return FCompanyName
End Get
Set(ByVal value As String)
FCompanyName = value
End Set
End Property
Private FContactName As String
Public Property ContactName() As String
Get
Return FContactName
End Get
Set(ByVal value As String)
FContactName = value
End Set
End Property
Private FContactTitle As String
Public Property ContactTitle() As String
Get
Return FContactTitle
End Get
Set(ByVal value As String)
FContactTitle = value
End Set
End Property
Private FAddress As String
Public Property Address() As String
Get
Return FAddress
End Get
Set(ByVal value As String)
FAddress = value
End Set
End Property
Private FCity As String
Public Property City() As String
Get
Return FCity
End Get
Set(ByVal value As String)
FCity = value
End Set
End Property
Private FRegion As String
Public Property Region() As String
Get
Return FRegion
End Get
Set(ByVal value As String)
FRegion = value
End Set
End Property
Private FPostalCode As String
Public Property PostalCode() As String
Get
Return FPostalCode
End Get
Set(ByVal value As String)
FPostalCode = value
End Set
End Property
Private FCountry As String
Public Property Country() As String
Get
Return FCountry
End Get
Set(ByVal value As String)
FCountry = value
End Set
End Property
Private FPhone As String
Public Property Phone() As String
Get
Return FPhone
End Get
Set(ByVal value As String)
FPhone = value
End Set
End Property
Private FFax As String
Public Property Fax() As String
Get
Return FFax
End Get
Set(ByVal value As String)
FFax = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal customerID As String, ByVal companyName _
As String, _
ByVal contactName As String, ByVal contactTitle As String, _
ByVal address As String, _
ByVal city As String, ByVal region As String, _
ByVal postalCode As String, _
ByVal country As String, ByVal phone As String, ByVal fax As String)
Me.FCustomerID = customerID
Me.FCompanyName = companyName
Me.FContactName = contactName
Me.FContactTitle = contactTitle
Me.FAddress = address
Me.FCity = city
Me.FRegion = region
Me.FPostalCode = postalCode
Me.FCountry = country
Me.FPhone = phone
End Sub
End Class

The DAL refers to the business object layer and not the other way around. This means that business objects don't need to carry any database knowledge (or baggage) around with them.

By using generic Lists—List(Of T)—you don't need to write custom collections. Generic lists are enumerable and bindable. That said, I often inherit from the generic List—for example, List(Of Customer)—to add additional behaviors to my custom business object collections.