I am currently building a web app in php using Laravel-4, however, I suspect that this question is applicable to other languages and frameworks as well.
I have read about dependency injection and Facades and understand the necessary coding in each case. What I am not clear on is when you should use one over the other?
What the advantages/disadvantages or simply the reasons for using either dependency injection over the Facade design pattern?
Many thanks
Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.
In software engineering, dependency injection is a design pattern in which an object or function receives other objects or functions that it depends on. A form of inversion of control, dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs.
Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework. While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.
Facades are used to make interactions with classes easier to read and use. It makes the code appear that you're using a bunch of static methods to interact with a class without instantiating it when in actuality you are calling methods on an existing object.
Dependency injection is used to, as the name implies, inject the dependencies of a class into the class. This is done through the constructor. You inject classes into another class to allow the class to use the functionality from the injected classes. This becomes powerful when you start injecting an interface into a class. Then you can create a class based on the interface and inject it into the class. This way, if you need to change the way the injected class works, you can create a new class based on the interface and inject it. Since you code is based on the injected interface, it will ensure that the class the received the injections will continue to work without needing to be changed.
This is most notable in Laravel 4 if you create a repository which is based on an interface for the Eloquent engine. You can inject that repository into a controller and use the methods on the interface to get the info you need. then if you ever want to switch to something like Redis, all you have to do is make a new class based on that interface that uses the Redis engine instead and then inject that class. The controller will never need to be changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With