Do I need to create an interface for every service class to follow good design principles?

I'm working on a custom Magento 2 module in my internship, and I'm trying to follow SOLID principles in my code. Right now, my controller actions handle everything"getting request data, processing it, and returning a response. To follow the Single Responsibility Principle, I'm moving the logic out of the controllers and into service classes. But now I'm wondering about the Dependency Inversion Principle. I read that you should "depend on abstractions, not on concrete classes," which I think means I should create an interface for each service and inject that into the controller instead of the actual class. So I’m not sure: Should I create an interface for every service class, even if I only have one implementation? Do I even need a service class if there is relatively little logic? Would it not make sense to sometimes skip the service class to prevent unnecessary clutter? When is it okay to just inject the class directly without an interface? Is it a bad practice if I skip the interface for now? I’m looking for advice from more experienced developers on what’s reasonable to do in a real project, especially for someone who's still learning. Thanks!

May 14, 2025 - 13:12
 0

I'm working on a custom Magento 2 module in my internship, and I'm trying to follow SOLID principles in my code.

Right now, my controller actions handle everything"getting request data, processing it, and returning a response. To follow the Single Responsibility Principle, I'm moving the logic out of the controllers and into service classes.

But now I'm wondering about the Dependency Inversion Principle. I read that you should "depend on abstractions, not on concrete classes," which I think means I should create an interface for each service and inject that into the controller instead of the actual class.

So I’m not sure:

  • Should I create an interface for every service class, even if I only have one implementation?
  • Do I even need a service class if there is relatively little logic? Would it not make sense to sometimes skip the service class to prevent unnecessary clutter?
  • When is it okay to just inject the class directly without an interface?
  • Is it a bad practice if I skip the interface for now?

I’m looking for advice from more experienced developers on what’s reasonable to do in a real project, especially for someone who's still learning. Thanks!