The BaseRepository is built into Knight Data Access Layer.
Public Class PersonRepository
Inherits BaseRepository(Of Person)
Implements IPersonRepository
Public 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
End Class
''' <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 IDataHelper
Get
Return DataHelper.SessionFactory()
End Get
End 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)
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