How should I use ninject in a multiproject mvc app?
My app is set up this way Web Data Services POCO Entities
Controllers use services (so they should be injected) Services use Repositories (which I assume should also be injected)
I have this already set up so that the Controllers receive the ser开发者_JS百科vice they need through Ninject but I not sure how to get this done with the services =>repositories
any help with this?
You could use the ninject.web.mvc extension. It contains a sample application which illustrates how you could register the container in Global.asax
.
Bob has several blogs about repository pattern with Ninject and NHibernate. It's pretty much the same for all other OR Mappers:
http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to-fluent-nhibernate-and-mysql/
http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/
http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/
Simply set up your services' dependencies as well as the controller's dependencies. Ninject will walk the dependency chain and resolve all of them.
for example,
ProductController
has dependency on IProductService
IProductService
is implemented with ProductService
that has a dependency on IProductRepository
IProductRepository
is implemented with NHibernateProductRepository
that has a dependency on ISession
.
when your NinjectControllerFactory
attempts to resolve ProductController
, it sees the dependency on IProductService
. it resolves that dependency as ProductService
, and sees that it has a dependency on IProductRepository
. and it will continue on down the chain until it can resolve completely an argument.
so the important part is to Bind ANY dependencies, not just those in a Controller.
精彩评论