I've been programming for some time but never got interested in knowing in theory what each concept means, I may be using a variety of programming concepts, but without knowing it.
Service Locator: For me, refers to a record of shortcuts to speed up development by reducing the amount of code. One question is: may Locator refer to namespaces/classes only, or I can have a registry of variables?
Here is my understanding of it:
$locator = new ServiceLocator()
$locator->set('app', new System\Application());
$locator->set('db', new System\Pdo());
// Get the objects
$locator->get('db')->connect();
$locator->get('app')->run();
Dependency Injection (and Dependency Injection Container): Injecting objects within objects, allowing faster access to these regardless of the factory pattern. And DI Container?
Here is my understanding of it:
$app = new System\Application(System\Config::load());
Inversion of Control: Don't understand this Design Pattern (or understand but don't know if what I do is IoC)
Then, in theory (preferably with simple examples), what does each of these concepts mean? Am I correct, or what is wrong / can be improved?
Thanks!
With service locator the application class asks for it explicitly by a message to the locator. With injection there is no explicit request, the service appears in the application class – hence the inversion of control.
Dependency Injection is the method of providing the dependencies and Inversion of Control is the end result of Dependency Injection. IoC is a design principle where the control flow of the program is inverted. Dependency Injection is one of the subtypes of the IOC principle.
To implement the IoC, you have the choice of two main patterns: Service Locator and Dependency Injection. The Service Locator allows you to "resolve" a dependency within a class and the Dependency Injection allows you to "inject" a dependency from outside the class.
Inversion of Control(IoC) is also known as Dependency injection (DI). The Spring container uses Dependency Injection (DI) to manage the components that build up an application and these objects are called Spring Beans. Spring implements DI by either an XML configuration file or annotations.
Service Location and Dependency Injection is at first for decoupling classes so that they can be easily tested and changed.
When you compare the register and resolve parts of an IoC Container with a Service Locator it seems to be the same.
You can use an IoC Container as a Service Locator, which is considered to be an anti pattern. When you use Service Location you always have to call the Service Locator actively all over your architecture. So you decouple your classes, but on the other hand you couple them all to the Service Locator. Furthermore dependency discovery is more difficult with a Service Locator, because you are hiding dependencies. Whereas with Dependency Injection you make the dependencies "public" by using Constructor Injection.
When you use an IoC Container you use Dependency Injection (Constructor Injection or Property Injection). The IoC Container is now able to resovle the dependency graph by looking at the constructor parameters and create the whole dependency graph. This is called auto-wiring. A Service Locator is not able to auto-wiring dependencies. As I already mentioned, you are not forced to use auto-wiring, you could easily use the IoC container like a Service Locator by simple calling the IoC Container in each class directly, BUT YOU SHOULD NOT!
See also: https://stackoverflow.com/a/11319026/175399
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