elias zerrouq: dependency injection and magento 2
TRANSCRIPT
![Page 1: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/1.jpg)
![Page 2: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/2.jpg)
About me
Elias Zerrouq ([email protected])
(freelance) (web-)developer from the Netherlands studying
computer science
run the magenticians.com blog (twitter @magenticians)
first time in Italy
![Page 3: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/3.jpg)
Comprehension is important
Understanding dependency injection is more
important than memorizing the usage of it by an
implementing platform, for example Magento 2.
![Page 4: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/4.jpg)
You might look at this...
...and think “Blergh, this looks ugly but all core
files work like this, so I better copy it!”
You are now using dependency injection but
have no idea how or why it works.
![Page 5: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/5.jpg)
It’s not just dependency injection
By understanding “how” and “why” things work,
you distance yourself from those who don’t.
Given the size and amount of all systems, it’s
not realistic to understand everything of every
system.
![Page 6: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/6.jpg)
Where does dependency injection
come from?
Dependency injection is a form of inversion
of control and an implementation of
dependency inversion.
The specific term “dependency injection” is
coined by Martin Fowler and “various IoC
advocates”[1].
[1] “Inversion of Control Containers and the Dependency Injection pattern”, 23 January
2004 by Martin Fowler
![Page 7: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/7.jpg)
Inversion of dependency control…
what?
Inversion of Control traditionally means that
instead of calling a component its methods,
endpoints are registered with the component and
called when needed.
In general, control being inversed is the flow of
the program.
![Page 8: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/8.jpg)
An example of inversion of control
Instead of having a sequential approach to
routing a request, the request is processed by
one or many routers.
![Page 9: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/9.jpg)
Components should rely on abstractions and
not on specific implementations. Depending on
details results in tightly coupled components.
● High level modules should not depend upon low-level modules. Both
should depend upon abstractions.
● Abstractions should never depend upon details. Details should depend
upon abstractions.
Dependency Inversion
![Page 10: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/10.jpg)
Part of SOLID
S Single responsibility principle
O Open/closed principle
L Liskov substitution principle
I Interface segregation principle
D Dependency inversion principle
![Page 11: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/11.jpg)
An example of dependency inversion
In Magento 1.X, Zend_Mail is an hard wired
dependency:
In Magento 2, this is solved by relying on abstractions for sending email.
![Page 12: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/12.jpg)
Looking back at the previous
example
Everything is abstract. No details about implementation of
● request
● router list
● router
● resulting action instance
![Page 13: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/13.jpg)
Advantages
● Everything is loosely coupled which allows
for modification of the one without impacting
the other.
● Individual components are easily testable.
● Code reuse.
● You only need to know what to supply and
what you get back.
![Page 14: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/14.jpg)
Disadvantages
Everything. Is. Abstract.
● Complexity increases.
● Thus more time required initially.
● Pre-mature abstraction.
● Overhead.
![Page 15: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/15.jpg)
Dependency injection
By relying on abstractions, at some point you
need to supply or leverage an actual
implementation (like Zend_Mail).
This is where dependency injection comes in.
![Page 16: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/16.jpg)
So what dependency injection
means
By using dependency inversion, any
component can have dependencies it needs
to function. To make sure the dependent can
function, the dependencies are injected into
the dependent.
The exact implementation of those
dependencies are picked at runtime.
![Page 17: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/17.jpg)
Common dependency injection
approaches
● Dependencies are passed during
construction of the object
● Dependencies are passed via (magic)
setters
● Dependencies are directly set to object
properties
![Page 18: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/18.jpg)
So what about Magento 2?
● Magento 2 uses constructor injection
● It rolls a custom dependency injection
framework
● It uses XML configuration
![Page 19: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/19.jpg)
Constructor Injection
![Page 20: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/20.jpg)
Common misconception
The amount of times you need to do this:
new Handler(new Dependency(
new SubDependency($argument)),new SecondDepency($data)…………
);
...is probably zero.
![Page 21: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/21.jpg)
Dependency injection can be
automated
Most dependency injection frameworks use
recursive reflection and/or pre-defined
configuration to determine which
implementations of the dependencies should
be injected.
Magento 2 does this too.
![Page 22: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/22.jpg)
Magento 2 its di.xml allows
● Type hinted interface argument preferences
● Constructor arguments
● Deferred creation of dependencies by
proxies
● Automatic creation of utility classes
(factories, repositories)
● Plugin definitions
● Constants for object configuration
![Page 23: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/23.jpg)
Example A: constructor argument
![Page 24: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/24.jpg)
Service Contracts are possible due
to dependency injection
Service contracts define a pair of recurring
interface sets which separates consumer from
implementation.
Because the consumer knows nothing about
how the service works, only what it has to
supply or gets back, the underlying
implementation can change without affecting
consumers.
![Page 25: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/25.jpg)
Example B: interface implementation
preference
In many modules, all service contracts have a
preference to a default implementation supplied by
Magento.
![Page 26: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/26.jpg)
Some things cannot be injected
Everything which has an identity (i.e. an object
representing a database entry) cannot be
directly injected. For those, factories should be
used.
Magento 2 its code generation mechanics allow
simple factories to be created with zero
configuration.
![Page 27: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/27.jpg)
Configuration allows change
In Magento 2, each module can supply different
implementations and arguments for
dependencies used by the application.
This allows to easily change behavior without
touching or rewriting core classes.
![Page 28: Elias Zerrouq: Dependency Injection and Magento 2](https://reader034.vdocuments.net/reader034/viewer/2022051617/55a51c8e1a28ab565a8b472e/html5/thumbnails/28.jpg)
Summarized
It’s all for the better.
If there is any time left, are there any questions?