Model view controller 实体框架4上下文?

Model view controller 实体框架4上下文?,model-view-controller,entity-framework-4,repository-pattern,unit-of-work,objectcontext,Model View Controller,Entity Framework 4,Repository Pattern,Unit Of Work,Objectcontext,我只是想了解一些关于其他人在使用EF4时如何管理他们的环境的反馈。我正在创建一个MVC应用程序,使用(我认为:))工作单元、服务层、存储库、EF4W/POCO技术 我的控制器使用接受UOW的服务,UOW然后利用存储库从EF获取POCO 这是否正确实施 请看下面的内容,如有任何反馈,我们将不胜感激 控制器 Public Class MyController Function ListCustomers() As ActionResult Dim _UOW = New Uni

我只是想了解一些关于其他人在使用EF4时如何管理他们的环境的反馈。我正在创建一个MVC应用程序,使用(我认为:))工作单元、服务层、存储库、EF4W/POCO技术

我的控制器使用接受UOW的服务,UOW然后利用存储库从EF获取POCO

这是否正确实施

请看下面的内容,如有任何反馈,我们将不胜感激

控制器

Public Class MyController
    Function ListCustomers() As ActionResult
        Dim _UOW = New UnitOfWork
        Dim _Service = New CustomerService(_UOW)
        Dim _Model = New CustomersViewModel
        _Model.Customers = _Service.GetCustomers
        _UOW.Dispose()

        Return View(_Model)
    End Function
End Class
Public Interface IUnitOfWork
    Property Context As GTGContext
    Sub Committ()

End Interface

Public Class UnitOfWork
    Implements IDisposable, IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Committ() Implements IUnitOfWork.Committ
        _Context.SaveChanges()

    End Sub

#Region "IDisposable Support"

    Private _IsDisposed As Boolean

    Protected Overridable Sub Dispose(ByVal Disposing As Boolean)
        If (Not _IsDisposed) Then
            If (Disposing) Then
                If (_Context IsNot Nothing) Then
                    _Context.Dispose()
                End If
            End If

            'TODO: Free unmanaged resources (unmanaged objects) and override Finalize() below.
        End If

        _IsDisposed = True

    End Sub

    'TODO: Override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

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

    End Sub

#End Region

End Class
Public Class CustomerService
    Private _UOW As IUnitOfWork
    Private _Repo As Repository(Of Customer)

    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
        _Repo = New Repository(Of Customer)(_UOW)
    End Sub

    Public Function GetCustoemrs() As IQueryable(Of Customer)
        ' Any Business Logic Here
        Return _Repo.GetCustomers()
    End Function

End Class
Imports System.Data.Objects

Namespace Repositories
    Public Interface IRepository(Of T As Class)
        ReadOnly Property ObjectSet As IObjectSet(Of T)
        ReadOnly Property UnitOfWork As IUnitOfWork
        Function Query(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As IQueryable(Of T)
        Function GetFirst(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Function GetSingle(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Sub Add(ByVal Entity As T)
        Sub Attach(ByVal Entity As T)
        Sub Delete(ByVal Entity As T)
        Sub SaveChanges()

    End Interface

    Public Class Repository(Of T As Class)
        Implements IRepository(Of T)

#Region "Private Members/Properties"

        Private _ObjectSet As IObjectSet(Of T)
        Private ReadOnly Property ObjectSet As System.Data.Objects.IObjectSet(Of T) Implements IRepository(Of T).ObjectSet
            Get
                If (_ObjectSet Is Nothing) Then
                    _ObjectSet = UnitOfWork.Context.CreateObjectSet(Of T)()
                End If
                Return _ObjectSet
            End Get
        End Property

        Private _UnitOfWork As IUnitOfWork
        Private ReadOnly Property UnitOfWork As IUnitOfWork Implements IRepository(Of T).UnitOfWork
            Get
                Return _UnitOfWork
            End Get
        End Property

#End Region

#Region "Constructor(s)"

        Public Sub New(ByVal UnitOfWork As IUnitOfWork)
            If (UnitOfWork Is Nothing) Then
                Throw New ArgumentNullException("UnitOfWork")
            End If
            _UnitOfWork = UnitOfWork

        End Sub

#End Region

#Region "IRepository(Of T)"

        Public Sub Add(ByVal Entity As T) Implements IRepository(Of T).Add
            ObjectSet.AddObject(Entity)

        End Sub

        Public Sub Attach(ByVal Entity As T) Implements IRepository(Of T).Attach
            ObjectSet.Attach(Entity)
            UnitOfWork.Context.ObjectStateManager.ChangeObjectState(Entity, EntityState.Modified)

        End Sub

        Public Sub Delete(ByVal Entity As T) Implements IRepository(Of T).Delete
            ObjectSet.DeleteObject(Entity)

        End Sub

        Public Function GetFirst(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetFirst
            Return ObjectSet.FirstOrDefault(Expression)

        End Function

        Public Function GetSingle(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetSingle
            Return ObjectSet.SingleOrDefault(Expression)

        End Function

        Public Function Query(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements IRepository(Of T).Query
            Return ObjectSet.Where(Expression)

        End Function

        Public Sub SaveChanges() Implements IRepository(Of T).SaveChanges
            UnitOfWork.Committ()

        End Sub

#End Region

    End Class
End Namespace
工作单元

Public Class MyController
    Function ListCustomers() As ActionResult
        Dim _UOW = New UnitOfWork
        Dim _Service = New CustomerService(_UOW)
        Dim _Model = New CustomersViewModel
        _Model.Customers = _Service.GetCustomers
        _UOW.Dispose()

        Return View(_Model)
    End Function
End Class
Public Interface IUnitOfWork
    Property Context As GTGContext
    Sub Committ()

End Interface

Public Class UnitOfWork
    Implements IDisposable, IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Committ() Implements IUnitOfWork.Committ
        _Context.SaveChanges()

    End Sub

#Region "IDisposable Support"

    Private _IsDisposed As Boolean

    Protected Overridable Sub Dispose(ByVal Disposing As Boolean)
        If (Not _IsDisposed) Then
            If (Disposing) Then
                If (_Context IsNot Nothing) Then
                    _Context.Dispose()
                End If
            End If

            'TODO: Free unmanaged resources (unmanaged objects) and override Finalize() below.
        End If

        _IsDisposed = True

    End Sub

    'TODO: Override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

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

    End Sub

#End Region

End Class
Public Class CustomerService
    Private _UOW As IUnitOfWork
    Private _Repo As Repository(Of Customer)

    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
        _Repo = New Repository(Of Customer)(_UOW)
    End Sub

    Public Function GetCustoemrs() As IQueryable(Of Customer)
        ' Any Business Logic Here
        Return _Repo.GetCustomers()
    End Function

End Class
Imports System.Data.Objects

Namespace Repositories
    Public Interface IRepository(Of T As Class)
        ReadOnly Property ObjectSet As IObjectSet(Of T)
        ReadOnly Property UnitOfWork As IUnitOfWork
        Function Query(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As IQueryable(Of T)
        Function GetFirst(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Function GetSingle(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Sub Add(ByVal Entity As T)
        Sub Attach(ByVal Entity As T)
        Sub Delete(ByVal Entity As T)
        Sub SaveChanges()

    End Interface

    Public Class Repository(Of T As Class)
        Implements IRepository(Of T)

#Region "Private Members/Properties"

        Private _ObjectSet As IObjectSet(Of T)
        Private ReadOnly Property ObjectSet As System.Data.Objects.IObjectSet(Of T) Implements IRepository(Of T).ObjectSet
            Get
                If (_ObjectSet Is Nothing) Then
                    _ObjectSet = UnitOfWork.Context.CreateObjectSet(Of T)()
                End If
                Return _ObjectSet
            End Get
        End Property

        Private _UnitOfWork As IUnitOfWork
        Private ReadOnly Property UnitOfWork As IUnitOfWork Implements IRepository(Of T).UnitOfWork
            Get
                Return _UnitOfWork
            End Get
        End Property

#End Region

#Region "Constructor(s)"

        Public Sub New(ByVal UnitOfWork As IUnitOfWork)
            If (UnitOfWork Is Nothing) Then
                Throw New ArgumentNullException("UnitOfWork")
            End If
            _UnitOfWork = UnitOfWork

        End Sub

#End Region

#Region "IRepository(Of T)"

        Public Sub Add(ByVal Entity As T) Implements IRepository(Of T).Add
            ObjectSet.AddObject(Entity)

        End Sub

        Public Sub Attach(ByVal Entity As T) Implements IRepository(Of T).Attach
            ObjectSet.Attach(Entity)
            UnitOfWork.Context.ObjectStateManager.ChangeObjectState(Entity, EntityState.Modified)

        End Sub

        Public Sub Delete(ByVal Entity As T) Implements IRepository(Of T).Delete
            ObjectSet.DeleteObject(Entity)

        End Sub

        Public Function GetFirst(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetFirst
            Return ObjectSet.FirstOrDefault(Expression)

        End Function

        Public Function GetSingle(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetSingle
            Return ObjectSet.SingleOrDefault(Expression)

        End Function

        Public Function Query(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements IRepository(Of T).Query
            Return ObjectSet.Where(Expression)

        End Function

        Public Sub SaveChanges() Implements IRepository(Of T).SaveChanges
            UnitOfWork.Committ()

        End Sub

#End Region

    End Class
End Namespace
服务

Public Class MyController
    Function ListCustomers() As ActionResult
        Dim _UOW = New UnitOfWork
        Dim _Service = New CustomerService(_UOW)
        Dim _Model = New CustomersViewModel
        _Model.Customers = _Service.GetCustomers
        _UOW.Dispose()

        Return View(_Model)
    End Function
End Class
Public Interface IUnitOfWork
    Property Context As GTGContext
    Sub Committ()

End Interface

Public Class UnitOfWork
    Implements IDisposable, IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Committ() Implements IUnitOfWork.Committ
        _Context.SaveChanges()

    End Sub

#Region "IDisposable Support"

    Private _IsDisposed As Boolean

    Protected Overridable Sub Dispose(ByVal Disposing As Boolean)
        If (Not _IsDisposed) Then
            If (Disposing) Then
                If (_Context IsNot Nothing) Then
                    _Context.Dispose()
                End If
            End If

            'TODO: Free unmanaged resources (unmanaged objects) and override Finalize() below.
        End If

        _IsDisposed = True

    End Sub

    'TODO: Override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

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

    End Sub

#End Region

End Class
Public Class CustomerService
    Private _UOW As IUnitOfWork
    Private _Repo As Repository(Of Customer)

    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
        _Repo = New Repository(Of Customer)(_UOW)
    End Sub

    Public Function GetCustoemrs() As IQueryable(Of Customer)
        ' Any Business Logic Here
        Return _Repo.GetCustomers()
    End Function

End Class
Imports System.Data.Objects

Namespace Repositories
    Public Interface IRepository(Of T As Class)
        ReadOnly Property ObjectSet As IObjectSet(Of T)
        ReadOnly Property UnitOfWork As IUnitOfWork
        Function Query(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As IQueryable(Of T)
        Function GetFirst(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Function GetSingle(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Sub Add(ByVal Entity As T)
        Sub Attach(ByVal Entity As T)
        Sub Delete(ByVal Entity As T)
        Sub SaveChanges()

    End Interface

    Public Class Repository(Of T As Class)
        Implements IRepository(Of T)

#Region "Private Members/Properties"

        Private _ObjectSet As IObjectSet(Of T)
        Private ReadOnly Property ObjectSet As System.Data.Objects.IObjectSet(Of T) Implements IRepository(Of T).ObjectSet
            Get
                If (_ObjectSet Is Nothing) Then
                    _ObjectSet = UnitOfWork.Context.CreateObjectSet(Of T)()
                End If
                Return _ObjectSet
            End Get
        End Property

        Private _UnitOfWork As IUnitOfWork
        Private ReadOnly Property UnitOfWork As IUnitOfWork Implements IRepository(Of T).UnitOfWork
            Get
                Return _UnitOfWork
            End Get
        End Property

#End Region

#Region "Constructor(s)"

        Public Sub New(ByVal UnitOfWork As IUnitOfWork)
            If (UnitOfWork Is Nothing) Then
                Throw New ArgumentNullException("UnitOfWork")
            End If
            _UnitOfWork = UnitOfWork

        End Sub

#End Region

#Region "IRepository(Of T)"

        Public Sub Add(ByVal Entity As T) Implements IRepository(Of T).Add
            ObjectSet.AddObject(Entity)

        End Sub

        Public Sub Attach(ByVal Entity As T) Implements IRepository(Of T).Attach
            ObjectSet.Attach(Entity)
            UnitOfWork.Context.ObjectStateManager.ChangeObjectState(Entity, EntityState.Modified)

        End Sub

        Public Sub Delete(ByVal Entity As T) Implements IRepository(Of T).Delete
            ObjectSet.DeleteObject(Entity)

        End Sub

        Public Function GetFirst(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetFirst
            Return ObjectSet.FirstOrDefault(Expression)

        End Function

        Public Function GetSingle(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetSingle
            Return ObjectSet.SingleOrDefault(Expression)

        End Function

        Public Function Query(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements IRepository(Of T).Query
            Return ObjectSet.Where(Expression)

        End Function

        Public Sub SaveChanges() Implements IRepository(Of T).SaveChanges
            UnitOfWork.Committ()

        End Sub

#End Region

    End Class
End Namespace
存储库

Public Class MyController
    Function ListCustomers() As ActionResult
        Dim _UOW = New UnitOfWork
        Dim _Service = New CustomerService(_UOW)
        Dim _Model = New CustomersViewModel
        _Model.Customers = _Service.GetCustomers
        _UOW.Dispose()

        Return View(_Model)
    End Function
End Class
Public Interface IUnitOfWork
    Property Context As GTGContext
    Sub Committ()

End Interface

Public Class UnitOfWork
    Implements IDisposable, IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Committ() Implements IUnitOfWork.Committ
        _Context.SaveChanges()

    End Sub

#Region "IDisposable Support"

    Private _IsDisposed As Boolean

    Protected Overridable Sub Dispose(ByVal Disposing As Boolean)
        If (Not _IsDisposed) Then
            If (Disposing) Then
                If (_Context IsNot Nothing) Then
                    _Context.Dispose()
                End If
            End If

            'TODO: Free unmanaged resources (unmanaged objects) and override Finalize() below.
        End If

        _IsDisposed = True

    End Sub

    'TODO: Override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

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

    End Sub

#End Region

End Class
Public Class CustomerService
    Private _UOW As IUnitOfWork
    Private _Repo As Repository(Of Customer)

    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
        _Repo = New Repository(Of Customer)(_UOW)
    End Sub

    Public Function GetCustoemrs() As IQueryable(Of Customer)
        ' Any Business Logic Here
        Return _Repo.GetCustomers()
    End Function

End Class
Imports System.Data.Objects

Namespace Repositories
    Public Interface IRepository(Of T As Class)
        ReadOnly Property ObjectSet As IObjectSet(Of T)
        ReadOnly Property UnitOfWork As IUnitOfWork
        Function Query(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As IQueryable(Of T)
        Function GetFirst(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Function GetSingle(ByVal Expression As Expressions.Expression(Of Func(Of T, Boolean))) As T
        Sub Add(ByVal Entity As T)
        Sub Attach(ByVal Entity As T)
        Sub Delete(ByVal Entity As T)
        Sub SaveChanges()

    End Interface

    Public Class Repository(Of T As Class)
        Implements IRepository(Of T)

#Region "Private Members/Properties"

        Private _ObjectSet As IObjectSet(Of T)
        Private ReadOnly Property ObjectSet As System.Data.Objects.IObjectSet(Of T) Implements IRepository(Of T).ObjectSet
            Get
                If (_ObjectSet Is Nothing) Then
                    _ObjectSet = UnitOfWork.Context.CreateObjectSet(Of T)()
                End If
                Return _ObjectSet
            End Get
        End Property

        Private _UnitOfWork As IUnitOfWork
        Private ReadOnly Property UnitOfWork As IUnitOfWork Implements IRepository(Of T).UnitOfWork
            Get
                Return _UnitOfWork
            End Get
        End Property

#End Region

#Region "Constructor(s)"

        Public Sub New(ByVal UnitOfWork As IUnitOfWork)
            If (UnitOfWork Is Nothing) Then
                Throw New ArgumentNullException("UnitOfWork")
            End If
            _UnitOfWork = UnitOfWork

        End Sub

#End Region

#Region "IRepository(Of T)"

        Public Sub Add(ByVal Entity As T) Implements IRepository(Of T).Add
            ObjectSet.AddObject(Entity)

        End Sub

        Public Sub Attach(ByVal Entity As T) Implements IRepository(Of T).Attach
            ObjectSet.Attach(Entity)
            UnitOfWork.Context.ObjectStateManager.ChangeObjectState(Entity, EntityState.Modified)

        End Sub

        Public Sub Delete(ByVal Entity As T) Implements IRepository(Of T).Delete
            ObjectSet.DeleteObject(Entity)

        End Sub

        Public Function GetFirst(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetFirst
            Return ObjectSet.FirstOrDefault(Expression)

        End Function

        Public Function GetSingle(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As T Implements IRepository(Of T).GetSingle
            Return ObjectSet.SingleOrDefault(Expression)

        End Function

        Public Function Query(ByVal Expression As System.Linq.Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements IRepository(Of T).Query
            Return ObjectSet.Where(Expression)

        End Function

        Public Sub SaveChanges() Implements IRepository(Of T).SaveChanges
            UnitOfWork.Committ()

        End Sub

#End Region

    End Class
End Namespace

让我告诉你,很长一段时间以来,我一直在为该采取什么方法而苦恼,主要是在StackOverflow上:)

我决定实现这篇关于管理对象上下文的优秀文章(注意ammended版本允许多个上下文)

然后我实现了如下外观:

public class MyEntityFacade : FacadeBase<MyEntities, MyEntity>
{
    public object GetAll()
    {
        return this.ObjectContext.MyEntities.ToList();
    }

    public bool HasChild(int parentId)
    {
        return this.ObjectContext.MyEntityChild.Any(c => c.parentId == parentId);
    }
}
公共类MyEntityFacade:FacadeBase
{
公共对象GetAll()
{
返回this.ObjectContext.MyEntities.ToList();
}
公共bool HasChild(int parentId)
{
返回this.ObjectContext.MyEntityChild.Any(c=>c.parentId==parentId);
}
}
当然,它很老了,但是天哪,它真的有用

看,所有这些服务层/工作单元/存储库的东西都是为了支持它而不得不编写大量代码(当然EF4应该减少代码膨胀!)。每次添加一个新实体,你猜怎么着?甚至更乏味的代码膨胀

使用上面的方法,我将所有的facade/unit of work支持代码塞进一个可重用的类库中,我所要做的就是编写facade代码,它只涉及实际执行某些操作的代码,并且可以在多个上下文中工作

为我工作。。。哦,别用“门面”这个词来抨击我。我从未真正上过学;)

谢谢, 理查德


祝你周末愉快

从你的总结来看,这听起来是对的,除了你的
Committ()
method;-)中的输入错误之外,我使用了类似的方法