Do I need to create an interface for every service class to follow good design principles? [duplicate]
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? 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!
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?
- 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!