78

I am exploring dependency injection and the term composition root is used all over the place. So what is it?

Luke Girvin
  • 12,672
  • 8
  • 57
  • 79
Thomas
  • 2,027
  • 1
  • 16
  • 38

2 Answers2

74

The composition root is the single place in your application where the composition of the object graphs for your application take place, using the dependency injection container (although how this is done is irrelevant, it could be using a container or could be done manually using pure DI).

There should only be one place where this happens and your container should not need to be used outside of the composition root.

Quoting from one of the answers linked to below:

In practice, this means that you should configure the container at the root of your application.

  • In a desktop app, that would be in the Main method (or very close to it)
  • In an ASP.NET (including MVC) application, that would be in Global.asax
  • In WCF, that would be in a ServiceHostFactory
  • etc.

There is a good answer here which explains a bit more about this.

See also this answer.

Community
  • 1
  • 1
Sam Holder
  • 30,911
  • 13
  • 94
  • 171
  • What about in web applications like PHP frameworks. Should the composition root be index.php? – CMCDragonkai Nov 08 '13 at 09:04
  • @CMCDragonkai although I'm not experienced with PHP, `index.php` doesn't sound like a good place to have it. – Oscar Mederos Nov 30 '13 at 20:23
  • Or something like bootstrap.php which is caled in at index.php. – CMCDragonkai Dec 01 '13 at 03:37
  • @CMCDragonkai, with PHP MVC frameworks I usually use a bootstrap include as the place to setup the dependencies. However, if not using a DI container (when doing the wiring manually), it maybe useful to do it somewhere else when you already know what controller will be used so that you only set up the related objects, otherwise you may end up instantiating lot of objects that won't be used. This is not an issue with DI containers that support lazy loading and is not an issue with small applications (with very small apps index.php may be a good place). Just remember to have only one CR. – MV. Dec 16 '13 at 02:19
  • good explanation, thanks, but what if injected service is being changed (e.g. reinitialized in one place), how to ensure it is up do date in all clients' items that need to have up to date changed injected servcie? – komizo Nov 19 '14 at 14:45
  • @lukaszk what do you mean by "being changed"? If you mean that their state is changed, services should be stateless so they can't be changed. Services responsibility is to perform an action and optionally return the result but not to store state between calls. – Constantin Galbenu Aug 30 '16 at 09:08
  • @ConstantinGALBENU thank you for clarification. It is really interesting topic. How to cope with those services that's state changes from time to time. According to your suggestion I should separate such services that are stateful and manage them other way - not via dependency injection. Do you have any good pattern or recommendation for that? – komizo Oct 18 '16 at 22:13
  • @lukaszk State should be managed/controlled only by Entities using domain invariants; if you manage state in services you would get an anemic domain model; there are so many books but I suggest you read [https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215] – Constantin Galbenu Nov 01 '16 at 15:12
4

Mark Seemann wrote a great article about Composition Root design pattern.

essential points from this article are:

A Composition Root is a (preferably) unique location in an application where modules are composed together.

Only applications should have Composition Roots. Libraries and frameworks shouldn't.

A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container.

http://blog.ploeh.dk/2011/07/28/CompositionRoot/

I wrote my own JavaScript Dependency Injection Framework called Di-Ninja with these principles in mind

https://github.com/di-ninja/di-ninja

As I know, is the only one in javascript that implement the Composition-Root design pattern and it's documentation could be another good example to demonstrate how it works.

It work with NodeJS, browser (with Webpack or UMD/AMD), and React-Native.

DevTheJo
  • 1,700
  • 2
  • 14
  • 20