Vs Registry Template Vs Locator Dialog Box Dependency Injection Container

Is there a difference between them, and not set and receive objects in the array using the key?

class Registry { private $container=array(); public static function Set($name,$object){ self::$container[$name]=$object; } public static function &Get($name){ return self::$container[$name]; } } 
+5
source share
1 answer

Registry Template

A registry template is a template used to search for an object, knowing only its name. This template stores instances of objects within itself and uses dictionary matching to retrieve these instances later.

DI Container / DI Template

The DI container contains a registry that has a mapping of object types to abstractions. It is more advanced in that when an object is resolved, it is created, and they all depend on the objects.

When you request an object from a DI container, you get a graph of the object , starting with the object you request as the root. Each dependent object is automatically entered by recursively navigating through the constructor of each class, starting with classes that do not have dependencies, and instantiating each object using the registry as a guide.

Injection Dependency is a template that does not necessarily use a DI container . The DI template consists of a root , which is located at the starting point of the application. The root of the composition is where the types are registered and where an instance of the root object is created . When an instance of the root object is created, the application starts by itself. The application itself does not have a reference to the DI container and is not tightly connected with it.

Service locator

A service locator is considered by many people to be anti-pattern . The idea is that you either enter the container into your object, or use a static reference to the DI container to create instances at runtime.

The main difference is that the application clearly depends on (thus, closely related to) the DI container.

Another disadvantage of using Service Locator is that since you are introducing a DI container, it is impossible to see from the class constructors which interfaces it depends on. Instead, you need to consult the documentation or analyze the source code to determine what class dependencies are.

Despite what is considered an anti-pattern, there are still situations where it makes sense to use. However, it should be considered the last resort after exhausting all other options (the surrounding context, injection of properties, etc.).

+16
source

Source: https://habr.com/ru/post/1210714/


All Articles