How to approach ninject container/kernel in inheritance situation

Posted by Bas on Stack Overflow See other posts from Stack Overflow or by Bas
Published on 2010-06-02T13:03:34Z Indexed on 2010/06/02 14:04 UTC
Read the original article Hit count: 600

Filed under:
|
|
|

I have the following situation:

class RuleEngine {}

abstract class RuleImplementation {}
class RootRule : RuleImplementation {}
class Rule1 : RuleImplementation {}
class Rule2 : RuleImplementation {}

The RuleEngine is injected by Ninject and has a kernel at it's disposal, the role of the RuleEngine is to fire off the root rule, which on it's turn will load all the other rules also using Ninject, but using a different Module and creating a new Kernel.

Now my question is, some of the rules require some dependencies which I want to inject using Ninject. What would be the best way to create the kernel for these rules and also still do proper unit testing with it? (the kernel shouldn't become a real pain in my tests)

I've been thinking of the following possibilitys:

  • The kernel that I use in the RuleEngine class could be tossed around to RuleImplementation and thus be available for every rule. But tossing around Kernels isn't really something I wish to do.
  • When creating the rules, I could give the kernel (which creates the rules) as a constructor argument for each rule.
  • I could create a method inside the RuleImplementation which creates a kernel and makes it possible for the rules to retrieve the kernel using a get() in the abstract class

Whats the convention of passing around/creating kernels? Just create new kernels, or reuse them?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ioc-container