使用/ EF 4存储库模式 - 我是否在正确的轨道上?

时间:2011-03-10 21:33:14

标签: asp.net-mvc entity-framework repository-pattern unit-of-work

所以在过去的几周里,我一直在试图获得服务/存储库/ UnitOfWork的整个模式,我得出了以下结论,并想知道是否有专家认为我在正确的轨道。

我正在使用Ninject将这些接口注入到每个层的构造函数中。

我正在使用RepositoryBase(Of TEntity),我的所有特定存储库都将从它们派生并实现自己的接口:

接口

Public Interface IUnitOfWork
    Inherits IDisposable

    Sub Commit()

End Interface

Public Interface IRepository(Of TEntity As Class)
    Function Query(Predicate As Expressions.Expression(Of Func(Of TEntity, Boolean))) As IQueryable(Of TEntity)
    Function GetAll() As IEnumerable(Of TEntity)
    Function GetFirst(Predicate As Expressions.Expression(Of Func(Of TEntity, Boolean))) As TEntity
    Function GetSingle(Predicate As Expressions.Expression(Of Func(Of TEntity, Boolean))) As TEntity
    Sub Add(Entity As TEntity)
    Sub Attach(Entity As TEntity)
    Sub Delete(Entity As TEntity)
    Sub Save(Entity As TEntity)

End Interface

Public Interface ICategoryRepository
    Function GetCategories() As IEnumerable(Of Category)
    Function GetCategoryByID(ID As Integer) As Category
    Sub SaveCategory(Category As Category)

End Interface

存储库/ UnitOfWork实施

Public MustInherit Class RepositoryBase(Of TEntity As Class)
    Implements IRepository(Of TEntity)

    Protected Context As GTGContext
    Protected ObjectSet As ObjectSet(Of TEntity)

    Public Sub New(UnitOfWork As IUnitOfWork)
        Context = CType(UnitOfWork, UnitOfWork).Context
        ObjectSet = Context.CreateObjectSet(Of TEntity)()

    End Sub

    Protected Sub Add(Entity As TEntity) Implements Core.Interfaces.IRepository(Of TEntity).Add
        ObjectSet.AddObject(Entity)
    End Sub

    Protected Sub Attach(Entity As TEntity) Implements Core.Interfaces.IRepository(Of TEntity).Attach
        ObjectSet.Attach(Entity)
    End Sub

    Protected Sub Delete(Entity As TEntity) Implements Core.Interfaces.IRepository(Of TEntity).Delete
        ObjectSet.DeleteObject(Entity)
    End Sub

    Protected Function GetAll() As System.Collections.Generic.IEnumerable(Of TEntity) Implements Core.Interfaces.IRepository(Of TEntity).GetAll
        Return ObjectSet.AsEnumerable
    End Function

    Protected Function GetFirst(Predicate As System.Linq.Expressions.Expression(Of System.Func(Of TEntity, Boolean))) As TEntity Implements Core.Interfaces.IRepository(Of TEntity).GetFirst
        Return ObjectSet.First(Predicate)
    End Function

    Protected Function GetSingle(Predicate As System.Linq.Expressions.Expression(Of System.Func(Of TEntity, Boolean))) As TEntity Implements Core.Interfaces.IRepository(Of TEntity).GetSingle
        Return ObjectSet.Single(Predicate)
    End Function

    Protected Function Query(Predicate As System.Linq.Expressions.Expression(Of System.Func(Of TEntity, Boolean))) As System.Linq.IQueryable(Of TEntity) Implements Core.Interfaces.IRepository(Of TEntity).Query
        Return ObjectSet.Where(Predicate)
    End Function

    Protected Sub Save(Entity As TEntity) Implements Core.Interfaces.IRepository(Of TEntity).Save

    End Sub

End Class

Public Class CategoryRepository
    Inherits RepositoryBase(Of Category)
    Implements ICategoryRepository

    Public Sub New(UnitOfWork As IUnitOfWork)
        MyBase.New(UnitOfWork)

    End Sub

    Public Function GetCategories() As System.Collections.Generic.IEnumerable(Of Core.Entities.Category) Implements Core.Interfaces.ICategoryRepository.GetCategories
        'Return GetAll()
        Return Context.Categories.Include("SubCategories").AsEnumerable

    End Function

    Public Function GetCategoryByID(ID As Integer) As Core.Entities.Category Implements Core.Interfaces.ICategoryRepository.GetCategoryByID
        Return GetSingle(Function(x) x.ID = ID)

    End Function

    Public Sub SaveCategory(Category As Core.Entities.Category) Implements Core.Interfaces.ICategoryRepository.SaveCategory
        ObjectSet.First(Function(x) x.ID = Category.ID)
        ObjectSet.ApplyCurrentValues(Category)

    End Sub

End Class

Public Class UnitOfWork
    Implements IUnitOfWork

    Public Property Context As GTGContext

    Public Sub New()
        _Context = New GTGContext

    End Sub

    Public Sub Commit() Implements Core.Interfaces.IUnitOfWork.Commit
        _Context.SaveChanges()

    End Sub

#Region "IDisposable Support"

    Private _IsDisposed As Boolean

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

        _IsDisposed = True

    End Sub

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

#End Region

End Class

服务实施

Public MustInherit Class ServiceBase(Of TEntity As Class)
    Implements IService(Of TEntity)

End Class

Public Class CategoryService
    Inherits ServiceBase(Of Category)
    Implements ICategoryService

    Private Repository As ICategoryRepository

    Public Sub New(Repository As ICategoryRepository)
        Me.Repository = Repository

    End Sub

    Public Function GetCategories() As System.Collections.Generic.IEnumerable(Of Core.Entities.Category) Implements Core.Interfaces.ICategoryService.GetCategories
        Return Repository.GetCategories

    End Function

    Public Function GetCategoryByID(ID As Integer) As Core.Entities.Category Implements Core.Interfaces.ICategoryService.GetCategoryByID
        Return Repository.GetCategoryByID(ID)

    End Function

    Public Sub SaveCategory(Category As Core.Entities.Category) Implements Core.Interfaces.ICategoryService.SaveCategory
        Repository.SaveCategory(Category)

    End Sub

End Class

你在这里看到的任何瑕疵?建议?魔法? :)

2 个答案:

答案 0 :(得分:2)

好像我每天都在回答同一主题的问题。 :)

无论如何 - 关于这个问题 - 我同意@qes。

服务/存储库结合的重点是您的存储库很简单,并且由服务来提供执行查询的逻辑。您的服务应该具有“消费者”(MVC应用程序)所需的特定方法,例如:

public ICollection<Order> FindOrdersForCustomer(int customerId)
{
   return _orderRepository
      .Query()
      .Where(order => order.CustomerId == customerId)
      .ToList();
}

简单地打包电话毫无意义。您的服务应该充当MVC应用程序和底层存储库之间的外观。

基本上,您的 OrderRepository (例如)定义了对Orders的操作。 您的 OrderService 应该提供添加/检索/保存/删除订单的所有不同方式。

这就是为什么我不喜欢存储库中的“单一”,“第一”,“所有”方法。

我只有一个名为“查找”的“阅读”方法。

然后服务应该具有“Single”,“First”,“All”方法,只需使用基本的LINQ来实现Find方法。

您的存储库过于复杂IMO。

My Repository界面只有3种方法:

IQueryable<T> Find()
void Save(T entity)
void Delete(T entity)

我的服务界面有5到20之间的任何地方。

答案 1 :(得分:0)

如果只有 代码传递到存储库,那么Service类的重点是什么?