Using Dependency Injection with Http Handlers
I am using ASP.NET MVC 2 with Ninject, and Linq2SQL behind a repo开发者_如何学编程sitory pattern, based on Rob Conery's TekPub Starter Site.
With controllers it all works fine, however I have an HTTP Handler (it serves and resizes images from a DB), and I have no idea how I use Ninject to replace my ISession interface with a concrete instance of my LinqToSQLSession.
How should I best do this?
This SO question is about using ninject w/ HttpHandlers: HttpHandler Property Injection using Ninject returning null
Wish I could give you a better answer, but hopefully that will be enough to get you going. Good luck!
Use property injection. I have used it with StructureMap, but Ninject should support it
Define a base class for your http handlers, and hook DI in there.
Update:
Check here for what's property injection vs. other types of injection: http://wiki.github.com/ninject/ninject/injection-patterns
In the base class for the handlers you need to get a hold of your ninject IKernel instance and call .Inject(this) on initialization.
精彩评论