Why you need layers in your application code

You need to add an integration with a new service or supplier to your application code.

You should avoid writing code that interacts directly with that service or supplier - a.k.a tightly-coupled code.

What if you change to a different service or supplier in the future?

What if they release a new version of their API that includes breaking changes?

If your code is tightly coupled to a single implementation, you'd need to rewrite the code, and there wouldn't be a way to easily switch from the old version to the new one.

By introducing an adapter layer, you make your code more loosely coupled.

You can have multiple implementations, with one for each different supplier, service, or version.

You can have a consistent interface and API for every implementation, making them hot-swappable and making it easy to switch between different implementations.

If you change supplier or they release a new version, you write a new implementation for it and switch when you're ready.

Also, as every implementation satisfies a common interface, it's easy to make a fake implementation that you can use for testing.

- Oliver

P.S. Do you need immediate access to an expert Drupal Developer? With my Drupal development subscription, make unlimited requests for a fixed monthly price in less time than posting to a job board!

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.