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,现在我的存储库层中有一个紧密的耦合,这是不好的吗?
2) 我正在使用 UnitOfWork 模式。它应该放在哪里?它具有属性 Context As ObjectContext,因此它也与 EF 紧密耦合。糟糕吗?
3) 我如何使用 DI 使这更容易?
我希望这尽可能松散耦合以进行测试。有什么建议吗?
---------- 编辑 ----------
请告诉我这里是否正确。另外,服务将注入 IRepository(Of Category),它如何区分 EFRepository(Of T) 的具体类别?UnitOfWork 和 Service 呢?
一旦有人帮助我搞清楚了这个问题,我知道它似乎微不足道,但是我很难理解!
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