Previously I registered dependencies in IoC container at application start setting controller factory. But now I need to register some dependencies per request. Are there any ways to make it in ASP.NET MVC 3?
Previously I registered dependencies in IoC container at application start setting controller factory. But
Share
Generally, even every basic IoC container supports object lifecycle management in some way.
My favorite container is Ninject 2.0 which seemlessly integrates with ASP.NET MVC 3. It’s available as a NuGet package called
Ninject.MVC3.In the
RegisterServices(IKernel)method, you can define a scope for each type to resolve. The following code configures Ninject to return the same single instance ofSomeImplementationwhenISomeInterfaceis being resolved:Ninject lets you choose from one of the following available scopes:
InRequestScope()InThreadScope()InSingletonScope()InTransientScope()Nate Kohari wrote a blog post about that topic called Cache-and-Collect Lifecycle Management in Ninject 2.0 — you would definitely want to check that out!