【发布时间】:2015-12-29 09:21:24
【问题描述】:
我正在尝试按照以下教程将其从 C# 转换为 Vb.net http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
我将通用存储库转换如下:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data
Imports System.Data.Entity
Imports MarketMessages.Data
Imports System.Linq.Expressions
Public Class GenericRepository(Of TEntity As Class)
Friend context As ComplaintsDemoContext
Friend dbSet As DbSet(Of TEntity)
Public Sub New(context As ComplaintsDemoContext)
Me.context = context
Me.dbSet = context.[Set](Of TEntity)()
End Sub
Public Overridable Function [Get](Optional filter As Expression(Of Func(Of TEntity, Boolean)) = Nothing, Optional orderBy As Func(Of IQueryable(Of TEntity), IOrderedQueryable(Of TEntity)) = Nothing, Optional includeProperties As String = "") As IEnumerable(Of TEntity)
Dim query As IQueryable(Of TEntity) = dbSet
If filter IsNot Nothing Then
query = query.Where(filter)
End If
For Each includeProperty As String In includeProperties.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
query = query.Include(includeProperty)
Next
If orderBy IsNot Nothing Then
Return orderBy(query).ToList()
Else
Return query.ToList()
End If
End Function
Public Overridable Function GetByID(id As Object) As TEntity
Return dbSet.Find(id)
End Function
Public Overridable Sub Insert(entity As TEntity)
dbSet.Add(entity)
End Sub
Public Overridable Sub Delete(id As Object)
Dim entityToDelete As TEntity = dbSet.Find(id)
Delete(entityToDelete)
End Sub
Public Overridable Sub Delete(entityToDelete As TEntity)
If context.Entry(entityToDelete).State = EntityState.Detached Then
dbSet.Attach(entityToDelete)
End If
dbSet.Remove(entityToDelete)
End Sub
Public Overridable Sub Update(entityToUpdate As TEntity)
dbSet.Attach(entityToUpdate)
context.Entry(entityToUpdate).State = EntityState.Modified
End Sub
结束类
但我在尝试转换 UnitOfWork 类时收到错误 Must implement Sub Dispose() for interface 'System.IDisposable' 这是我的代码:
Public Class UnitOfWork
Implements IDisposable
Private context As New ComplaintsDemoContext()
Private m_marketMessageRepository As GenericRepository(Of MarketMessage)
Private m_marketMessageTypeRepository As GenericRepository(Of MarketMessageType)
Public ReadOnly Property MarketMessageRepository() As GenericRepository(Of MarketMessage)
Get
If Me.m_marketMessageRepository Is Nothing Then
Me.m_marketMessageRepository = New GenericRepository(Of MarketMessage)(context)
End If
Return m_marketMessageRepository
End Get
End Property
Public ReadOnly Property CourseRepository() As GenericRepository(Of MarketMessageType)
Get
If Me.m_marketMessageTypeRepository Is Nothing Then
Me.m_marketMessageTypeRepository = New GenericRepository(Of MarketMessageType)(context)
End If
Return m_marketMessageTypeRepository
End Get
End Property
Public Sub Save()
context.SaveChanges()
End Sub
Private disposed As Boolean = False
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposed Then
If disposing Then
context.Dispose()
End If
End If
Me.disposed = True
End Sub
Public Sub Dispose()
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
【问题讨论】:
标签: dispose idisposable unit-of-work