|
Imports System.Data.Entity |
|
Imports System.Linq.Expressions |
|
Imports LinqKit |
|
|
|
Public Class EFRepository(Of T As Class) |
|
Implements IRepository(Of T) |
|
|
|
Private _context As DbContext |
|
Private _table As DbSet(Of T) |
|
|
|
Public Sub New(context As DbContext) |
|
_context = context |
|
_table = context.Set(Of T)() |
|
End Sub |
|
|
|
Public Function GetRows(Optional searchCriteria As Expression(Of Func(Of T, Boolean)) = Nothing) As IEnumerable(Of T) Implements IRepository(Of T).GetRows |
|
If searchCriteria Is Nothing Then |
|
Return _table.ToList() |
|
Else |
|
Return _table.Where(searchCriteria).ToList() |
|
End If |
|
End Function |
|
|
|
Public Function GetByPage(Of TKey)(searchCriteria As Expression(Of Func(Of T, Boolean)), orderCriteria As Expression(Of Func(Of T, TKey)), orderDescending As Boolean, pageNumber As Integer, recordsPerPage As Integer) As ResultPage(Of T) Implements IRepository(Of T).GetByPage |
|
'1. Search for rows (AsExpandable used as sub-queries do Not play nice with LINQ to SQL. See here: http : //tomasp.net/blog/linq-expand.aspx |
|
Dim rawResults As IQueryable(Of T) |
|
|
|
If searchCriteria Is Nothing Then |
|
rawResults = _table |
|
Else |
|
rawResults = _table.AsExpandable().Where(searchCriteria) |
|
End If |
|
|
|
'2. Apply ordering |
|
If orderDescending = True Then |
|
rawResults = rawResults.OrderByDescending(orderCriteria) |
|
Else |
|
rawResults = rawResults.OrderBy(orderCriteria) |
|
End If |
|
|
|
'3. Skip And take pages |
|
Dim resultingRows As IQueryable(Of T) = rawResults.Skip((pageNumber - 1) * recordsPerPage).Take(recordsPerPage) |
|
|
|
'4. Return results |
|
Return New ResultPage(Of T)(resultingRows, rawResults.Count(), resultingRows.Count()) |
|
End Function |
|
|
|
Public Function GetByKey(key As Object) As T Implements IRepository(Of T).GetByKey |
|
Return _table.Find(key) |
|
End Function |
|
|
|
Public Sub Add(record As T) Implements IRepository(Of T).Add |
|
_table.Add(record) |
|
End Sub |
|
|
|
Public Sub AddMany(records As IEnumerable(Of T)) Implements IRepository(Of T).AddMany |
|
_table.AddRange(records) |
|
End Sub |
|
|
|
Public Sub Update(record As T) Implements IRepository(Of T).Update |
|
_table.Attach(record) |
|
_context.Entry(record).State = EntityState.Modified |
|
End Sub |
|
|
|
Public Sub Remove(key As Object) Implements IRepository(Of T).Remove |
|
Dim record As T = _table.Find(key) |
|
_table.Remove(record) |
|
End Sub |
|
|
|
Public Sub RemoveMany(records As IEnumerable(Of T)) Implements IRepository(Of T).RemoveMany |
|
_table.RemoveRange(records) |
|
End Sub |
|
|
|
Public Sub RemoveAll() Implements IRepository(Of T).RemoveAll |
|
_table.RemoveRange(_table) |
|
End Sub |
|
|
|
Public Sub Commit() Implements IRepository(Of T).Commit |
|
_context.SaveChanges() |
|
End Sub |
|
|
|
Public Function RowCount(Optional filterCriteria As Expression(Of Func(Of T, Boolean)) = Nothing) As Integer Implements IRepository(Of T).RowCount |
|
|
|
If filterCriteria Is Nothing Then |
|
Return _table.Count() |
|
Else |
|
Return _table.Count(filterCriteria) |
|
End If |
|
End Function |
|
End Class |