Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I define my NinjectModule and my factories?

I read a few things around on this site :

  • It is best to configure our container at the launch the application
  • It is best to avoid making our libraries dependent on a dependency injection framework
  • It is recommended to use factories to initialize objects whose properties are defined at the runtime

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

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

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

What do you think ?

like image 819
Filimindji Avatar asked Dec 15 '11 09:12

Filimindji


2 Answers

The configuration does not necessarily be in the application assembly. It can also be in several dedicated assembly containing nothing than a part of the configuration. But as you mentioned it shouldn't be part of the implementation. In case you share exactly the same configuration over several projects you can reference an existing one.

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

like image 164
Remo Gloor Avatar answered Oct 18 '22 02:10

Remo Gloor


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

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

  • To keep this abstracted from the Ioc framework, I use the ServiceLocator pattern which you could use in your factories.

like image 35
Sebastian Piu Avatar answered Oct 18 '22 04:10

Sebastian Piu