Skip to main content

Question: When should I define a service?

An article from ComputerMinds - Building with Drupal in the UK since 2005
22nd Sep 2016

Steven Jones

Senior Developer
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

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.

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.