我在我的应用程序中使用Entity Framework 4.0(VS 2010,VB.NET),我试图以正确的方式执行此操作。 我的代码如下所示:
Public Interface ICustomerRepository
Function GetAll(ByVal preferance As String) As IEnumerable(Of Customer)
End Interface
接口的实现:
Public Class CustomerRepository
Implements ICustomerRepository
Protected objectSet As IObjectSet(Of Customer)
Public Sub New(ByVal context As ObjectContext)
Me.objectSet = context.CreateObjectSet(Of Customer)()
End Sub
Public Function GetAll(ByVal preferance As String) As
System.Collections.Generic.IEnumerable(Of Customer) Implements
ICustomerRepository.GetAll
Return Me.objectSet.Where(Function(p) p.preferance = preferance).ToArray
End Function
End Class
Public Interface IUnitOfWork
ReadOnly Property Customers() As ICustomerRepository
Sub Commit()
End Interface
Public Class UnitOfWork
Implements IUnitOfWork
Private ReadOnly context As ObjectContext
Private m_customers As ICustomerRepository
Public Sub New(ByVal context As ObjectContext)
If context Is Nothing Then
Throw New ArgumentNullException("context wasn't supplied")
End If
Me.context = context
End Sub
Public ReadOnly Property Customers() As ICustomerRepository Implements
IUnitOfWork.Customers
Get
If Me.m_customers Is Nothing Then
Me.m_customers = New CustomerRepository(Me.context)
End If
Return Me.m_customer
End Get
End Property
Public Sub Commit() Implements IUnitOfWork.Commit
Me.context.SaveChanges()
End Sub
End Class
当我想在我的业务层中使用它时:
Using context As New MyDatabseEntities()
Dim uow As New UnitOfWork(context)
For Each customer In uow.Customer.GetAll()
Console.WriteLine(customer.Name)
Next
End Using
我有多个存储库,Customer只是一个(举个例子)。
我不明白......当我编写使用context as New MyDatabseEntities()时,这不是对DAL层的依赖吗?
什么是更好的解决方案?
我的存储库是属于DAL还是属于BLL?
在我的CustomerRepository类中使用受保护的objectSet作为IObjectSet(Of Customer)是否可以,或者我不应该使用Of Customer?
提前致谢!