【发布时间】:2011-07-13 01:52:26
【问题描述】:
因此,在过去的几周里,我一直在努力尝试了解 Service/Repository/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
您在这里看到任何缺陷吗?建议?魔法? :)
【问题讨论】:
标签: asp.net-mvc entity-framework repository-pattern unit-of-work