I have a web application made in asp.net mvc, and i am using Ninject to bind the interfaces.
For now, i have this:
// Db Context
kernel.Bind<DbContext>().To<DbEntities>().InRequestScope();
// Repositories - which are using instance of DbEntities
kernel.Bind<ICustomerRepository>().To<CustomerRepository>();
kernel.Bind<IProductRepository>().To<ProductRepository>();
// Services - which are using instances of Repositories
kernel.Bind<ICustomerService>().To<CustomerService>();
kernel.Bind<IProductService>().To<ProductService>();
I am binding an DbContext to DbEntities in RequestScope because i want to use the same DbContext in the same web request. After that it should dispose it.
But how the other bindings should be? How they are by default?
For example IProductRepository which has an instance of DbContext (which is one per request), should also be InRequestScope() ?
IProductService has an instance of IProductRepository
How the bindings should be so is suitable for a web application? (and i don’t overload the server’s memory)
For MVC application your configuration is ok. There will be no big difference if you bind your repositories in the default transient scope or in the request scope. As @Mark stated in the transient scope your dependencies will be injected as new instances of bounded objects, but in transaction scope they will be created once per request. I prefer little bit more Request scope and recommend it if you want to do (for example) some per-request caching on your repository.