I've been using the Ninject.Web extension to inject business objects, repositories, Entity Framework context etc into my application. This works very well using the [Inject] attribute which can be applied within a webform that inherits from PageBase. I am now running into a snag as I am trying to write a custom membership provider that needs injection done inside of it but of course this provider is not instantiated from within a webform. Forms Authentication will instantiate the object when it needs it. I am unsure how to about doing this without having access to the [Inject] attribute. I understand that there is an application level kernel somewhere, but I have no idea how to tap into it. Any suggestions would be greatly appreciated.
A:
you may need to use the Service Locator pattern, since you have no control over the creation of the membership provider.
dave thieben
2010-10-11 20:49:56
I would like to stay away from a true Service Locator at this point. At this point I am thinking that the Ninject.Web while nice doesn't really do it for me since I need to be able to inject in webforms as well as in other places. I am considering a static instance via a singleton at this point.
e36M3
2010-10-11 22:45:02
-1: SL pattern still takes control of the creation - post-injection or whatever it's correctly termed is what's wanted
Ruben Bartelink
2010-10-12 07:29:11
+1
A:
You do a IKernel.Inject
on the the instance. Have a look at the source for the Application class in the extension project you're using.
In the case of V2, it's in a KernelContainer
. So you need to do a:
KernelContainer.Inject( this )
where this
is the non-page, non application class of which you speak.
You'll need to make sure this only happens once - be careful doing this in Global
, which may get instantiated multiple times.
Also, your Application / Global
class needs to derive from NinjectHttpAppplication, but I'm sure you've that covered.
Ruben Bartelink
2010-10-11 22:47:27