Question: When should I define a service?

We've been getting stuck into Drupal 8 development and have been grappling with the concepts of services, dependency injection and containers.

One question that's come up has been:

When should I define a service?

Sometimes it's really obvious, suppose you are defining a new way to connect to some 'resource' a bit like a database, yeah that should be a service so that I can swap it out etc. Maybe you are providing a way to optimize images, yeah, that should be a service that accepts an image and returns an optimized version of that image.

But, should all classes you write, also be a service?

Looking at core, we can see that no, they shouldn't. Most (all?) route controllers, plugins etc. are not defined as a services. But they have factories that are defined as a service. Where is the line?

Drupal core provides some classes, like ConfigImporter, that aren't in the service container? Why?
Should they be a service, or should code wanting to use them instantiate them directly?

We have our opinions, but we'd love to hear yours. Add a comment to this article to let us know what guides your decision as to whether something should be a defined service or not.

About the author

Steven has a wealth of experience building Drupal-based websites and tools associated with them. He is a maintainer of the Aegir project and has contributed to many other projects on Drupal.org. He is currently our infrastructure lead and is a lead architect on many of our projects.

He is a Master of Mathematics graduate from the University of Warwick and got involved in Drupal creating a website for the Maths society there.