Is it better to use facades or dependency injection?

By @ralphjsmit · 2021-11-09 09:12

Is it better to use facades or dependency injection?

  • By @davorminchorov · 2021-11-21 01:53

    I personally prefer to use dependency injection because it allows for a better object oriented design and better usage for design patterns when there’s complex code overall.

    When working on a long term project, let’s say more than a year and add a ton of features over time, your code will get messy and you can’t avoid that part but what you can do to fix it is to refactor it using some of the design patterns if they are applicable in the specific use case.

    I know that most people in the Laravel community promote simplicity over complexity but that can cause more issues in the long term if the structure of the project is not properly planned from the start. It becomes a nightmare to maintain the codebase and increases the chance of huge amount of bugs, as well as time to research how things work and/ or debug issues. One change can fix one thing but break another one.

    It can be demotivating to work on such a project where no one really cares for the codebase.

    If it’s a small short term project with a small team, it shouldn’t be a big deal, but if it’s something bigger for the long term, design patterns, modular structure and design principles can be very useful if applied properly.

    • By @ralphjsmit · 2021-11-22 06:57

      Thanks for your reply! So because you are injecting dependencies into constructors, this allows for a better object oriented design? Because facades are more 'hidden' throughout your code?