Knight Data Access Layer
Active Record Pattern VB.NET
Basic Tasks VB.NET > Patterns > Active Record Pattern VB.NET

The Active Record Pattern utilizes a base class to perform, save, updates and deletes.

See: http://en.wikipedia.org/wiki/Active_record_pattern

 

'Class that inherits BaseActiveRecord

<TableMapping("Persons")>

Public Class Vendor

Inherits BaseActiveRecord(Of Vendor)

 

<PrimaryKey(PrimaryKeyType.Identity)>

Public Property PersonId() As Integer

Public Property FirstName() As String

Public Property LastName() As String

Public Property DateCreated() As Date

<DefaultValue(1)>

Public Property PersonTypeId() As Integer

End Class

 

 

Private vendor As New Vendor()

vendor.FirstName = "John"

vendor.LastName = "Smith"

vendor.DateCreated = Date.Now

 

 

'Save the vendor

vendor.Save()

 

 

Here is the BaseActiveRecord class in its entirety:

 

''' <summary>

''' Implements active record pattern for loading, saving and deleting records

''' </summary>

''' <typeparam name="T">Generic class type</typeparam>

Public Class BaseActiveRecord(Of T As {Class, New})

 

#Region "Properties"

''' <summary>

''' Expose data helper that has the full range of capabilities

''' </summary>

Public ReadOnly Property Helper() As IDataHelper 

Get

Return DataHelper.SessionFactory()

End Get

End Property

#End Region

 

 

#Region "Methods"

 

''' <summary>

''' Insert or Update this record depending upon if the primary keys is the default value

''' </summary>

''' <returns></returns>

Public Sub Save()

Helper.Save(Me)

End Sub

 

''' <summary>

''' Save an object with its parent and children

''' </summary>

Public Sub SaveWithChildren()

Helper.SaveWithChildren(Me)

End Sub

 

''' <summary>

''' Save a list

''' </summary>

''' <param name="list">A list of items</param>

Public Sub Save(ByVal list As List(Of T))

Helper.Save(list)

End Sub

 

''' <summary>

''' Delete the corresponding record in the database

''' </summary>

Public Sub Delete()

Helper.Delete(Me)

End Sub

 

''' <summary>

''' Delete the corresponding record in the database and all its children

''' </summary>

Public Sub DeleteWithChildren()

Helper.DeleteWithChildren(Me)

End Sub

 

''' <summary>

''' Search for records based on the passed in search object and page in the database

''' </summary>

''' <param name="searchParameters">What to search for. Default values are not searched.</param>

''' <param name="orderBy">Order by SQL Clause</param>

''' <param name="page">The page number starting at 1</param>

''' <param name="pageSize">The number of records per page</param>

''' <returns></returns>

Public Function Search(ByVal searchParameters As T, ByVal orderBy As String, ByVal page As Integer, ByVal pageSize As Integer) As List(Of T)

Return Helper.Search(searchParameters, orderBy, page, pageSize)

End Function

 

''' <summary>

''' Count the number of records for a search

''' </summary>

''' <param name="searchParameters">What to search for. Default values are not searched.</param>

''' <returns></returns>

Public Function SearchCount(ByVal searchParameters As T) As Long

Return Helper.SearchCount(searchParameters)

End Function

 

''' <summary>

''' Load all the records from a table into a class list

''' </summary>

''' <returns></returns>

Public Function LoadAll() As List(Of T)

Return Helper.LoadAll(Of T)()

End Function

 

''' <summary>

''' Load all the records from a table into a class list

''' </summary>

''' <param name="orderBy">Order by SQL Clause</param>

''' <returns></returns>

Public Function LoadAll(ByVal orderBy As String) As List(Of T)

Return Helper.LoadAll(Of T)(orderBy)

End Function

 

''' <summary>

''' Load the first record in a table

''' </summary>

''' <returns></returns>

Public Function LoadOne() As T

Return Helper.LoadOne(Of T)()

End Function

 

''' <summary>

''' Load a single object by the passed parameters

''' </summary>

''' <typeparam name="T">Generic class type</typeparam>

''' <param name="parameters">The parameters to use for the where clause</param>

''' <returns></returns>

Public Function LoadOne(ByVal parameters As Dictionary(Of String, Object)) As T

Return Helper.LoadOne(Of T)(parameters)

End Function

 

''' <summary>

''' Load a record by primary key

''' </summary>

''' <returns></returns>

Public Function LoadOne(ByVal primaryKey As Object) As T

Return Helper.LoadByPrimaryKey(Of T)(primaryKey)

End Function

 

''' <summary>

''' Load a record by the primary key with all children

''' </summary>

''' <param name="primaryKeyValue">The primary key value to search for</param>

''' <returns></returns>

Public Function LoadWithChildren(ByVal primaryKeyValue As Object) As T

Return Helper.LoadWithChildren(Of T)(primaryKeyValue)

End Function

 

''' <summary>

''' Load a page of records that are ordered

''' </summary>

''' <param name="orderBy">Order by SQL Clause</param>

''' <param name="page">The page number starting at 1</param>

''' <param name="pageSize">The number of records per page</param>

''' <returns></returns>

Public Function LoadPage(ByVal orderBy As String, ByVal page As Integer, ByVal pageSize As Integer) As List(Of T)

Return Helper.LoadPage(Of T)(orderBy, page, pageSize)

End Function

 

''' <summary>

''' Load the primary key and the first text field.

''' </summary>

''' <returns></returns>

Public Function LoadLookup() As List(Of T)

Return Helper.LoadLookup(Of T)()

End Function

 

''' <summary>

''' Return the count of records in the table

''' </summary>

''' <returns></returns>

Public Function Count() As Long

Return Helper.Count(Of T)()

End Function

 

''' <summary>

''' LINQ Provider

''' </summary>

''' <returns></returns>

Public Function CreateQuery() As IQueryable(Of T)

Return Helper.CreateQuery(Of T)()

End Function

#End Region

End Class