Where should I define my NinjectModule and my factories?

I read a few things on this site:

  • It’s best to set up our container when the application starts.
  • It’s best to avoid our libraries depending on dependency injection infrastructure
  • We recommend that you use factories to initialize objects whose properties are defined at run time.

I am using Ninject. If I understand these recommendations, it is necessary that:

  • My libraries do not use NInject.dll
  • Therefore, my NinjectModules must be defined in my application project
  • My factories ( created by this principle ) should also be defined in the project of my application, and not directly in the library

This seems strange, especially for plants. I have many projects that use the same library. Should all these projects redefine ninject modules and factories?

What do you think?

+7
source share
2 answers

The configuration does not have to be in the application assembly. It can also be in several specialized assemblies containing only part of the configuration. But, as you mentioned, this should not be part of the implementation. If you use the same configuration across multiple projects, you can reference an existing one.

For factories in the future, you can use Ninject.Extensions.Factory so you don't have to run them yourself.

+1
source

It depends a lot on the context of your libraries, how I do it:

  • Initialize everything in Bootstrapper in the main project. Although I have several loading levels where I set up different things (mainly because I use my libraries in projects of the same type, so they have a similar configuration).

  • To keep this abstraction from the Ioc structure, I use a ServiceLocator pattern , which you can use in your factories.

+1
source

All Articles