We Love Dependency Injection in TYPO3 v10

|Daniel Goerz

Dependency injection (DI) makes code easier to maintain, test, read and extend. Until now, in TYPO3, it’s only been available in Extbase context, not in the rest of core. From TYPO3 v10, the widely adopted Symfony implementation is part of core and Extbase. It’s yet another reason to upgrade to the latest version.

What is dependency injection?

DI is a software architecture concept that avoids tightly coupling objects to their dependencies. For example, if your application needs to send email newsletters, the part that’s responsible for sending emails doesn’t need to hard-code the service used for transport. Instead, whoever instantiates the object can decide what email service to use and “inject” this dependency as a parameter. If they (or someone else) decide to use another service later on, they can inject this instead of rewriting the code.

Separation of concerns—do one job well

This supports the separation of concerns, whereby services are dedicated to doing a single, well defined, job well while outsourcing anything else to dependencies. With applications getting larger all the time, this helps break down complex into small, bite-sized chunks.

Another advantage for devs is that dependency injection makes testing easier: a technique known as mocking enables us to pass a dummy object to simulate various scenarios. So to continue the newsletter example, you don’t have to send out emails to test what happens when emails bounce. Instead, you can simulate that behaviour by injecting a dummy object instead of the transport service. The dummy object can then return whatever success or error codes you want to test against.

As mentioned above, DI also makes it easier for devs to swap out components later on. You don’t have to rewrite code but can just pass in a different service if you need to change it.

Symfony components flatten TYPO3’s learning curve

The big news around DI in TYPO3 core isn’t just DI itself, but the fact we chose to use Symfony rather than extending our existing implementation from Extbase to core. Relying on Symfony components has several advantages for TYPO3 users. It’s familiar to a wider range of coders, so it helps agencies find staff because the learning curve is flattened and the pool of suitable candidates is larger. Another boon: Symfony, including dependency injection, is already excellently documented, and documentation is central to great developer experience.

So all in all, not reinventing the wheel but using an already existing and proven solution enables the TYPO3 community and contributors to focus more on the CMS aspects of TYPO3.

DI in production at b13

We were really keen for Symfony’s DI container to be available across TYPO3, so it’s unsurprising we’re already using it in production. In one project, I configured the CacheManager as a factory in my services.yaml so that the correct CacheFrontend was injected into my controller so that I did not have to get it on my own. More details about this can be found in my blog post about the implementation.

What are you waiting for? Time to upgrade to v10

With time, more of core will be refactored to leverage dependency injection. Extensions can use it too, instead of TYPO3’s own DI they used previously. Ultimately, Symfony’s DI will replace DI in Extbase, so it’s a good idea to upgrade and write new code for v10 to save yourself rewriting it in the future.

Dependency injection is just one more example of how TYPO3 v10 is a big step in keeping our favourite CMS state of the art. Upgrade as soon as you can to get the best experience for your devs, editors, and customers so you don’t have to rewrite your code later.

Get a quote for upgrading to TYPO3 v10

Let's connect