Entity framework 4 这是否紧密地耦合了我的层?

Entity framework 4 这是否紧密地耦合了我的层?,entity-framework-4,asp.net-mvc-3,repository-pattern,unit-of-work,Entity Framework 4,Asp.net Mvc 3,Repository Pattern,Unit Of Work,我有一个使用UnitOfWork、服务层、存储库模式和EF4作为ORM的MVC应用程序 我的问题是,这个(UnitOfWork)是否有一个属性,它是EF的上下文,并且与我的层紧密耦合 工作单元 Public Interface IUnitOfWork Inherits IDisposable Property Context As GTGContext Sub Commit() End Interface Public Class UnitOfWork Im

我有一个使用UnitOfWork、服务层、存储库模式和EF4作为ORM的MVC应用程序

我的问题是,这个(UnitOfWork)是否有一个属性,它是EF的上下文,并且与我的层紧密耦合

工作单元

Public Interface IUnitOfWork
    Inherits IDisposable

    Property Context As GTGContext
    Sub Commit()

End Interface

Public Class UnitOfWork
    Implements IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Commit() Implements IUnitOfWork.Commit
        _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
    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
    End Sub
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
End Namespace

Imports System.Data.Objects

Namespace Repositories
    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.Commit()

        End Sub

#End Region

    End Class
End Namespace
服务

Public Interface IUnitOfWork
    Inherits IDisposable

    Property Context As GTGContext
    Sub Commit()

End Interface

Public Class UnitOfWork
    Implements IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Commit() Implements IUnitOfWork.Commit
        _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
    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
    End Sub
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
End Namespace

Imports System.Data.Objects

Namespace Repositories
    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.Commit()

        End Sub

#End Region

    End Class
End Namespace
存储库

Public Interface IUnitOfWork
    Inherits IDisposable

    Property Context As GTGContext
    Sub Commit()

End Interface

Public Class UnitOfWork
    Implements IUnitOfWork

    Public Property Context As Domain.GTGContext Implements IUnitOfWork.Context

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Commit() Implements IUnitOfWork.Commit
        _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
    Public Sub New(UOW As IUnitOfWork)
        _UOW = UOW
    End Sub
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
End Namespace

Imports System.Data.Objects

Namespace Repositories
    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.Commit()

        End Sub

#End Region

    End Class
End Namespace

没关系,只要确保为它选择合适的命名,比如
UnitOfWorkEF
。您还应该从
IUnitOfWork
接口中删除EF上下文,因为它将与数据访问技术紧密耦合。相反,您可以只在特定的
UnitOfWorkEF
类中使用上下文。

没关系,只需确保为其选择正确的命名,如
UnitOfWorkEF
。您还应该从
IUnitOfWork
接口中删除EF上下文,因为它将与数据访问技术紧密耦合。相反,您可以只在特定的
UnitOfWorkEF
类中拥有上下文。

我会让
EFContext
实现
IUnitOfWork
,因为上下文是部分类


我会让
EFContext
实现
IUnitOfWork
,因为context是部分类


我应该从接口中删除上下文吗?同样在Repository界面中,我认为objectset和unitofwork可以删除,因为管理实体不需要它们?@Sam Striano,当然,你应该从界面中删除它。我没注意到。我将更新我的答案。我是否应该从接口中删除上下文?同样在Repository界面中,我认为objectset和unitofwork可以删除,因为管理实体不需要它们?@Sam Striano,当然,你应该从界面中删除它。我没注意到。我会更新我的答案。