C#, Ninject: Where do you put the kernel and your modules?

后端 未结 3 1773

I\'m creating a tiny C# application, which currently consists of a core assembly and a winforms assembly. I realize I probably don\'t really need Ninject in a small thing like t

3条回答
  •  太阳男子
    2021-02-08 22:18

    My point of view: as Marek said, you should create some (probably static) wrapper for the Kernel, which contains the IKernel instance. It should contain the Resolve< T> method, and probably Load(INinjectModule module) method - all static.

    In each assembly, you can simply define your own INinjectModule that maps classes inside this assembly.

    The Kernel wrapper is in the 'lowest', the most common assembly (typically the one where Log and Utils are). It is because Kernel has to be accessible from all parts - so it must be in the assembly, which is referenced by all others. If you don't have one, you are always free enough to create one. This might seem a little tricky, one could expect that Kernel will be in the 'highest' assembly (the executable one). Not true.

    To register all your modules from your assemblies, simply call Kernel.Load(new XXModule) in each of them.

提交回复
热议问题