The BaseRepository is built into Knight Data Access Layer.
Public Class PersonRepository
Inherits BaseRepository(Of Person)
Implements IPersonRepository
End ClassPublic Sub Update(ByVal updatedPerson As Person)
Save(updatedPerson)
End Sub
Public Function [Get](ByVal id As Integer) As Person
Return LoadOne(id)
End Function
Public Function GetAll() As List(Of Person)
Return LoadAll()
End Function
Public Sub Post(ByVal person As Person)
Save(person)
End Sub
Public Sub Delete(ByVal id As Integer)
MyBase.Delete(id)
End Sub
''' <summary>
''' Implements Repository Pattern''' </summary>
''' <typeparam name="T"></typeparam>
Public
Class BaseRepository(Of T As {Class, New})#Region
"Properties"''' <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 a record depending upon if the primary keys is the default value
''' </summary>
''' <returns></returns>
Public
Sub Save(ByVal entity As T)Helper.Save(entity)
End
Sub
''' <summary>
''' Save an object with its parent and children
''' </summary>
Public
Sub SaveWithChildren(ByVal entity As T)Helper.SaveWithChildren(entity)
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(ByVal entity As T)Helper.Delete(entity)
End
Sub
''' <summary>
''' Delete the corresponding record in the database and all its children
''' </summary>
Public
Sub DeleteWithChildren(ByVal entity As T)Helper.DeleteWithChildren(entity)
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)elper.Search(searchParameters, orderBy, page, pageSize)Return H
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