Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Question: When should I define a service?

Parent Feed: 

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.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web