Search code examples
disposeidisposableunit-of-work

Must implement Sub Dispose() for interface 'System.IDisposable'


I am trying to follow the following tutorial and convert this from C# to 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

I converted the Generic repository as follows:

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

End Class

But I am getting the error Must implement Sub Dispose() for interface 'System.IDisposable' when trying to convert the UnitOfWork class here is my code:

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

Solution

  • Found the answer changed to:

    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