gpt4 book ai didi

dispose - 必须为接口(interface) 'System.IDisposable' 实现 Sub Dispose()

转载 作者:行者123 更新时间:2023-12-03 09:23:10 30 4
gpt4 key购买 nike

我正在尝试遵循以下教程并将其从 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 类时,我收到错误必须为接口(interface)“System.IDisposable”实现 Sub Dispose(),这是我的代码:

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

最佳答案

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

将光标置于前面

Implements IDisposable

然后按 Enter。

IDE 将生成该类的所有接口(interface)成员。学习自 here

关于dispose - 必须为接口(interface) 'System.IDisposable' 实现 Sub Dispose(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441325/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com