views:

180

answers:

1

Hi all

I have simple win service, that executes few tasks periodically. How should I pass Ninject kernel to all my task classes?

Is it good idea to create static variable of base task class and initialize it on service start?

+1  A: 

Rather than a static variable on the base task class, I would favor injecting the kernel into each class instance. This provides a bit more flexibility should you ever decide that you need more than one kernel (for whatever reason). The static variable in the base class just seems yucky, for lack of a better term.

Peter Meyer
but don't you need kernel itself to inject kernel. How do u get it in that case? That is something i can't figure out right now
mamu
Yes, you do. However, what you try to do, if you can, is create the kernel at some "object root", which is typically, though not always the entry point into the code. In the case of a windows service, reacting to OnStart may be the best logical entry point may be as high up as in the main() entry point into the process. At that point, the kernel can be created and manually injected into the service, which can then pass it on down the dependency chain.
Peter Meyer
I would add, however, that if the kernel is being passed around, that sort of implies that some class somewhere will be new'ing up objects but will be utilizing the kernel in order to resolve those object dependencies. If that's the scenario, an alternate approach is to use the CommonServiceLocator library (which actually holds a static reference to the kernel by way of an adapter class.) The benefit of this approach is not having a whole bunch of your classes take a direct dependency on the Ninject kernel.
Peter Meyer