7

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 ?

Community
  • 1
  • 1
Filimindji
  • 1,453
  • 1
  • 16
  • 23
  • possible duplicate of [Best location for Fluent IOC configuration (Currently trying Ninject)](http://stackoverflow.com/questions/5733591/best-location-for-fluent-ioc-configuration-currently-trying-ninject) – Ruben Bartelink Dec 15 '11 at 12:31

2 Answers2

1

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.

Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • I read that the usage of ServiceLocator is actually an anti-pattern. I would like to avoid it if possible. Or is it OK for factories ? – Filimindji Dec 16 '11 at 08:58
  • 1
    I consider it an anti pattern when you use it inside your services to get dependencies instead of injecting them in a constructor or as a property, in the factory you are trying to abstract the Ioc container (so you could use a different one). – Sebastian Piu Dec 16 '11 at 09:11
1

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.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98