必须实现Sub Dispose()for interface' System.IDisposable'

时间:2014-12-12 10:12:11

标签: dispose idisposable unit-of-work

我正在尝试按照以下教程将其从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

我按如下方式转换了Generic存储库:

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

结束班

但是我收到错误必须实现Sub Dispose()for interface' System.IDisposable' 当尝试转换UnitOfWork类时,这里是我的代码:

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

2 个答案:

答案 0 :(得分:2)

将代码复制到项目中时会发生这种情况。

将光标放在

前面
Implements IDisposable

然后按Enter键。

IDE将为该类生成所有接口成员。从中学到了 here

答案 1 :(得分:1)

发现答案改为:

Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)

    If Not disposed Then
        If disposing Then
            context.Dispose()
        End If
    End If
    disposed = True
End Sub

Public Overloads Sub Dispose() Implements System.IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub