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 IntegerPublic
Property FirstName() As StringPublic
Property LastName() As StringPublic
Property DateCreated() As Date<DefaultValue(1)>
Public
Property PersonTypeId() As IntegerEnd
Class
Private
vendor As New Vendor()vendor.FirstName =
"John"vendor.LastName =
"Smith"vendor.DateCreated =
Date.Now
'Save the vendor
vendor.Save()
''' <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})
"Properties"#Region
''' <summary>
''' Expose data helper that has the full range of capabilities
''' </summary>
Public
ReadOnly Property Helper() As IDataHelperGet
Return DataHelper.SessionFactory()
End
GetEnd
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()Me)Helper.Save(
End
Sub
''' <summary>
''' Save an object with its parent and children
''' </summary>
Public
Sub SaveWithChildren()Me)Helper.SaveWithChildren(
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()Me)Helper.Delete(
End
Sub
''' <summary>
''' Delete the corresponding record in the database and all its children
''' </summary>
Public
Sub DeleteWithChildren()Me)Helper.DeleteWithChildren(
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)Helper.Search(searchParameters, orderBy, page, pageSize)Return
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 LongHelper.SearchCount(searchParameters)Return
End
Function
''' <summary>
''' Load all the records from a table into a class list
''' </summary>
''' <returns></returns>
Public
Function LoadAll() As List(Of T)Helper.LoadAll(Of T)()Return
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)Helper.LoadAll(Of T)(orderBy)Return
End
Function
''' <summary>
''' Load the first record in a table
''' </summary>
''' <returns></returns>
Public
Function LoadOne() As THelper.LoadOne(Of T)()Return
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 THelper.LoadOne(Of T)(parameters)Return
End
Function
''' <summary>
''' Load a record by primary key
''' </summary>
''' <returns></returns>
Public
Function LoadOne(ByVal primaryKey As Object) As THelper.LoadByPrimaryKey(Of T)(primaryKey)Return
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 THelper.LoadWithChildren(Of T)(primaryKeyValue)Return
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)Helper.LoadPage(Of T)(orderBy, page, pageSize)Return
End
Function
''' <summary>
''' Load the primary key and the first text field.
''' </summary>
''' <returns></returns>
Public
Function LoadLookup() As List(Of T)Helper.LoadLookup(Of T)()Return
End
Function
''' <summary>
''' Return the count of records in the table
''' </summary>
''' <returns></returns>
Public
Function Count() As LongHelper.Count(Of T)()Return
End
Function
''' <summary>
''' LINQ Provider
''' </summary>
''' <returns></returns>
Public
Function CreateQuery() As IQueryable(Of T)Helper.CreateQuery(Of T)()Return
End
Function#End Region
End
Class