我仍然很难解决这个问题。我想像这样分离我的层(dll):
1) MyProject.Web.dll - MVC Web 应用程序(控制器、模型(编辑/视图)、视图)
2) MyProject.Services.dll - 服务层(业务逻辑)
3) MyProject.Repositories.dll - 存储库
4) MyProject。 Domain.dll - POCO 类
5) MyProject.Data.dll - EF4
工作流程:
1) 控制器调用服务来获取对象以填充视图/编辑模型。
2) 服务调用存储库来获取/持久化对象。
3) 存储库调用 EF 以从 SQL Server 获取/保留对象。
我的存储库返回 IQueryable(Of T),并在其中使用 ObjectSet(Of T)。
所以正如我所看到的,这些层完全取决于下一层和包含 POCO 类的库?
几个问题:
1) 现在我的存储库可以与 EF 一起正常工作,它们将依赖于 System.Data.Objects,现在我的存储库层与 EF 紧密耦合,这很糟糕吗?
2)我正在使用 UnitOfWork 模式。那应该住在哪里?它有一个作为 ObjectContext 的属性上下文,因此它也与 EF 紧密耦合。坏的?
3) 我如何使用 DI 使这更容易?
我希望这是一个尽可能松耦合的测试。有什么建议么?
- - - - - 编辑 - - - - -
如果我在正确的轨道上,请告诉我。此外,因此服务被注入了一个 IRepository(Of Category) 对,它如何知道它与 EFRepository(Of T) 的具体类之间的区别?与 UnitOfWork 和服务相同吗?
一旦有人帮我弄清楚我理解的地方,我知道这似乎是微不足道的,但是伙计,我有一段时间在这个问题上纠结!!
控制器
Public Class CategoryController
Private _Service As Domain.Interfaces.IService
Public Sub New(ByVal Service As Domain.Interfaces.IService)
_Service = Service
End Sub
Function ListCategories() As ActionResult
Dim Model As New CategoryViewModel
Using UOW As New Repositories.EFUnitOfWork
Mapper.Map(Of Category, CategoryViewModel)(_Service.GetCategories)
End Using
Return View(Model)
End Function
End Class
服务
Public Class CategoryService
Private Repository As Domain.Interfaces.IRepository(Of Domain.Category)
Private UnitOfWork As Domain.Interfaces.IUnitOfWork
Public Sub New(ByVal UnitOfWork As Domain.Interfaces.IUnitOfWork, ByVal Repository As Domain.Interfaces.IRepository(Of Domain.Category))
UnitOfWork = UnitOfWork
Repository = Repository
End Sub
Public Function GetCategories() As IEnumerable(Of Domain.Category)
Return Repository.GetAll()
End Function
End Class
存储库和工作单元
Public MustInherit Class RepositoryBase(Of T As Class)
Implements Domain.Interfaces.IRepository(Of T)
End Class
Public Class EFRepository(Of T As Class)
Inherits RepositoryBase(Of T)
End Class
Public Class EFUnitOfWork
Implements Domain.Interfaces.IUnitOfWork
Public Property Context As ObjectContext
Public Sub Commit() Implements Domain.Interfaces.IUnitOfWork.Commit
End Sub
End Class