Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection Container vs Registry pattern

I understand that the Dependency Injection principle is all about decoupling code. Instead of making new instances in the classes, instead you inject them, which make them loosely coupled.

Now if I have to pass on a set of objects that will be used through several classes throughout my application, I can make a container (commonly called dependency injection container).

That is exactly what i am doing because I have to pass a config object, a logger object, a translator object, etc. which will be used through several classes instances of my application. I am passing the whole container through several classes, even if not all of the classes need access to all the objects within the container. This led me to the following question: what is the difference if I create a global Registry and put the objects in there, then retrieve them like Registry::getInstance()->get('logger'); ? Wether I use a global registry or a dependency container, class isntances will have access to all the objects within the container or registry, even if they don't need to see/access all of them.

Conclusion: What is the difference if I pass a dependency injection container along my classes or a global Registry ?

like image 300
Matthew Avatar asked Jun 17 '13 07:06

Matthew


People also ask

What is the difference between dependency injection and factory pattern?

Dependency Injection is more of a architectural pattern for loosely coupling software components. Factory pattern is just one way to separate the responsibility of creating objects of other classes to another entity. Factory pattern can be called as a tool to implement DI.

What is dependency injection container?

A DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.

What type of design pattern is dependency injection?

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.

What are the different types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).


2 Answers

Note: sometimes people use Registries by different names. The common ones that i have seen as: Locator, Context and System.

Use of global Registry causes your code to be tied to the class name of said Registry. It means that you cannot test your code independently. And the way, how registry is set up, lies to you: you can never know which instance will be required.

It is quite common for people to confuse DI Containers with Registries.

DI Container is not something that you inject into a class. Instead it is an enhancement for factories: it determines that class Foo requires instance of Bar to be injected in the constructor. Then, depending on setup, it either acquires new instance of Bar or uses the existing one, to provide said dependency.

  • In a simple factory you usually have to hard-code the dependencies, that will be injected. In this situation, what factory can build, is limited by "footprint" of class's constructor.

  • When factory uses as DI container, it can produce instances with various dependencies. In this setup, your factories focus on constructing instances that are tied to some specific namespaces (or other high-level) logical groups of classes.

like image 181
tereško Avatar answered Nov 23 '22 13:11

tereško


I think the point missing here is CompositionRoot .As per DI Principle once you define your bindings in the Composition Root , then you can use the reference throughout the application. That is the value DI Container brings.

As per definition "A Composition Root is a (preferably) unique location in an application where modules are composed together."

I will add that a Composition Root also a unique location where all important decisions about your applications behavior takes place. That is what object to use in what situation.This is the heart of a OO system where interaction between the objects deliver the behavior of the system.

like image 37
Sebin Eapen George Avatar answered Nov 23 '22 13:11

Sebin Eapen George