帮助使用存储库模式吗?

发布于 2024-10-18 11:16:15 字数 1703 浏览 0 评论 0原文

我正在使用实体框架以及使用 T4 模板生成的 POCO。我将生成的类放在单独的程序集中。

好的,这是一个非常简单的例子:

我在模型中有一个类别实体,它有子类别(1-Many with SubCategory)。

当我使用以下代码时,我得到ObjectContext 实例已被释放,不能再用于需要连接的操作。

Public Interface ICategoryRepository
    Inherits IRepository(Of Category)

    Function GetCategories() As IQueryable(Of Category)
    Function GetCategoryByID(ByVal ID As Integer) As Category

End Interface

Public Class CategoryRepository
    Implements ICategoryRepository

    Public Function GetCategories() As System.Linq.IQueryable(Of Business.Category) Implements ICategoryRepository.GetCategories
        Using db As New GTGContainer
            Return db.Categories
        End Using
    End Function

    Public Function GetCategoryByID(ByVal ID As Integer) As Business.Category Implements ICategoryRepository.GetCategoryByID
        Using db As New GTGContainer
            Return db.Categories.FirstOrDefault(Function(x) x.ID = ID)
        End Using
    End Function

End Class

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Private _CategoryRepository As GTG.Data.Repositories.ICategoryRepository

    Public Sub New()
        Me.New(New GTG.Data.Repositories.CategoryRepository)
    End Sub

    Public Sub New(ByVal Repository As GTG.Data.Repositories.ICategoryRepository)
        _CategoryRepository = Repository
    End Sub

    Function Index() As ActionResult
        Dim m As New HomeViewModel
        m.Categories = _CategoryRepository.GetCategories

        Return View(m)
    End Function

End Class

Public Class HomeViewModel
    Public Property Categories As List(Of GTG.Business.Category)

End Class

任何帮助都会很棒。谢谢!

I am using the Entity Framework with POCO's generated using the T4 Templates. I have the generated classes in a separate assembly.

Ok, so a very simple example:

I have a Category entity in the model which has SubCategories (1-Many with SubCategory).

When I use the following code, I get The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

Public Interface ICategoryRepository
    Inherits IRepository(Of Category)

    Function GetCategories() As IQueryable(Of Category)
    Function GetCategoryByID(ByVal ID As Integer) As Category

End Interface

Public Class CategoryRepository
    Implements ICategoryRepository

    Public Function GetCategories() As System.Linq.IQueryable(Of Business.Category) Implements ICategoryRepository.GetCategories
        Using db As New GTGContainer
            Return db.Categories
        End Using
    End Function

    Public Function GetCategoryByID(ByVal ID As Integer) As Business.Category Implements ICategoryRepository.GetCategoryByID
        Using db As New GTGContainer
            Return db.Categories.FirstOrDefault(Function(x) x.ID = ID)
        End Using
    End Function

End Class

Public Class HomeController
    Inherits System.Web.Mvc.Controller

    Private _CategoryRepository As GTG.Data.Repositories.ICategoryRepository

    Public Sub New()
        Me.New(New GTG.Data.Repositories.CategoryRepository)
    End Sub

    Public Sub New(ByVal Repository As GTG.Data.Repositories.ICategoryRepository)
        _CategoryRepository = Repository
    End Sub

    Function Index() As ActionResult
        Dim m As New HomeViewModel
        m.Categories = _CategoryRepository.GetCategories

        Return View(m)
    End Function

End Class

Public Class HomeViewModel
    Public Property Categories As List(Of GTG.Business.Category)

End Class

Any help would be great. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜无邪 2024-10-25 11:16:15

这是因为您的 GetCategories() 返回一个 IQueryable,它实际上不是内存中的集合。当您的视图尝试枚举它时,它会尝试访问数据库,但它显然无法做到这一点。简单的补救措施是通过调用 GetCategories() 中的 ToList() 将其转换为内存中集合。即 db.Categories.ToList()。

This is because your GetCategories() returns an IQueryable, which is NOT actually an in-memory collection. When your View tries to enumerate it, it attempts to hit the database, which it obviously cannot do. The easy remedy is to turn it into an in-memory collection by calling ToList() in GetCategories(). That is, db.Categories.ToList().

你怎么这么可爱啊 2024-10-25 11:16:15

让上下文贯穿存储库的生命周期。

Public Class CategoryRepository
    Implements ICategoryRepository

    Private dbContext As GTGContainer

    Public Sub New()
        dbContext = New GTGContainer
    End Sub 

    Public Function GetCategories() As System.Linq.IQueryable(Of Business.Category) Implements ICategoryRepository.GetCategories
        Return dbContext.Categories
    End Function

    Public Function GetCategoryByID(ByVal ID As Integer) As Business.Category Implements ICategoryRepository.GetCategoryByID
        Return dbContext.Categories.FirstOrDefault(Function(x) x.ID = ID)
    End Function
End Class

Let the context live through the life of repository.

Public Class CategoryRepository
    Implements ICategoryRepository

    Private dbContext As GTGContainer

    Public Sub New()
        dbContext = New GTGContainer
    End Sub 

    Public Function GetCategories() As System.Linq.IQueryable(Of Business.Category) Implements ICategoryRepository.GetCategories
        Return dbContext.Categories
    End Function

    Public Function GetCategoryByID(ByVal ID As Integer) As Business.Category Implements ICategoryRepository.GetCategoryByID
        Return dbContext.Categories.FirstOrDefault(Function(x) x.ID = ID)
    End Function
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文