Ok, so i have asked a few questions on this before, but I really am just having a hrad time understanding this.
I am using the Service/Repository/EF 4 w/Pocos approach, and I have Ninject setup and injecting the controllers with the services but I am trying to figure out where to inject the context?
I want to be able to use multiple services on the controllers which in turn might access multiple repositories using the same context so all the changes would be persisted at once.
I studied the UnitOfWork pattern, but I don't understand how the MVC (controllers) would implement this as they only know of the service layer and the domain entities.
Edit
As Mohamed suggested below, inject the context into the repositories and then use a per request instance of it. How do you configure the binding in the MVC app? I would assume something like this:
Bind(Of IContext).To(MyDataContext)
Problem is, the MVC app knows nothing of the context, right?
Edit 2
Public Class ProductController
Private _Service As IProductService
Public Sub New(Service As IProductService)
_Service = Service
End Sub
End Class
Public Class NinjectWebModule
Public Sub New()
Bind(Of IProductService).To(ProductService)
End Sub
End Class
Public Interface IProductService
End Interface
Public Class ProductService
Implements IProductService
Private _Repository As IRepository(Of Product)
Public Sub New(Repository As IRepository(Of Product))
_Repository = Repository
End Sub
End Class
Public Class NinjectServiceModule
Public Sub New()
Bind(Of IRepository(Of Product)).To(EFRepository(Of Product))
End Sub
End Class
Public Interface IRepository(Of T As Class)
End Interface
Public Class EFRepository(Of T As Class)
Implements IRepository(Of T)
Private _UnitOfWork As MyUnitOfWork
Public Sub New (UnitOfWork As IUnitOfWork)
_UnitOfWork = UnitOfWork
End Sub
End Class
Public Class NinjectRepositoryModule
Public Sub New()
Bind(Of IUnitOfWork).To(EFUnitOfWork).InRequestScope()
End Sub
End Class
Public Interface IUnitOfWork
Sub Commit()
End Interface
Public Class EFUnitOfWork()
Implements IUnitOfWork
Public Property Context As MyContextType
Public Sub New()
_Context = New MyContextType
End Sub
End Class
I would then register all three modules from the MVC app?