Difference between dependency injection and dependency inversion - php

Two design patterns namely Dependency Injection and Dependency Inversion exist out there, Articles are there in the net trying to explain the difference. But the need to explain it in easier words is still there. Any one out there to come up to ?
I need to understand it in PHP.

(Note: This answer is language-agnostic, although the question specifically mentions PHP, but being unfamiliar with PHP, I have not provided any PHP examples).
Terminology - Dependencies and Coupling
In the context of Object-Oriented Programming, a dependency is any other object type which a class has a direct relationship with. When a class depends directly upon another object type, it can be described as being coupled to that type.
In general, any type used by a class is a dependency to some extent. There are many different ways for a class to depend upon another type, including:
Object types used by Instance variables
Object types used by Constructor parameters
Object types used by Accessor/Mutator methods
Constructors (And sometimes methods) which create new objects directly
Inheritance
The stronger the relationship between a class and its dependency, the tighter the coupling; therefore when a class depends directly upon another concrete class (such as the case with inheritance creating a direct dependency on a base class, or the case where a constructor creates new objects for its instance variables), any future changes to that direct dependency are more likely to "ripple" across in a Butterfly-effect style.
Difference Between Injection vs Inversion
Dependency Injection is an Inversion of Control technique for supplying objects ('dependencies') to a class by way of the Dependency Injection Design Pattern. Typically passing dependencies via one of the following:
A constructor
A public property or field
A public setter
The Dependency Inversion Principle (DIP) is a software design guideline which boils down to two recommendations about de-coupling a class from its concrete dependencies:
'High-level modules should not depend on low-level modules. Both should depend on abstractions.'
'Abstractions should not depend upon details. Details should depend upon abstractions.'
Or, to put it even more succinctly:
Dependency Injection is an implementation technique for populating instance variables of a class.
Dependency Inversion is a general design guideline which recommends that classes should only have direct relationships with high-level abstractions.
Dependency Injection and Inversion of Control (IoC)
Dependency Injection applies the IoC principle by ensuring classes are never responsible for creating or supplying their own dependencies (and therefore aren't responsible for the lifetime of those dependencies either).
However, IoC is not Dependency Injection - indeed, IoC as a principle has nothing particularly to do with dependencies or dependency injection per-se; Dependency Injection is a design pattern based around the principle of IoC.
IoC is seen in many other contexts, including those totally unrelated to object creation or dependencies, such as message passing via a Mediator or Message pump to trigger event handlers. Other (unrelated) examples of IoC include:
A windowed application using event handler functions/methods to handle mouse/keyboard input events.
An MVC web application using Controller Actions to handle HTTP requests.
(Updated from the original answer as a separate explanation about IoC)
Dependency Injection Pattern
Dependency Injection is a design pattern which applies the IoC principle to ensure that a class has absolutely no involvement or awareness in the creation or lifetime of objects used by its constructor or instance variables -- the "common" concern about object creation and populating instance variables is deferred to a framework instead.
That is to say, a class may specify its instance variables, but does not do any work to populate those instance variables (with the exception of using constructor parameters as a "pass-through")
A class which is designed with Dependency Injection in mind may look like this:
// Dependency Injection Example...
class Foo {
// Constructor uses DI to obtain the Meow and Woof dependencies
constructor(fred: Meow, barney: Woof) {
this.fred = fred;
this.barney = barney;
}
}
In this example, Meow and Woof are both dependencies injected via the Foo constructor.
On the other hand, a Foo class which is designed without Dependency Injection might simply create the Meow and Woof instances itself, or perhaps use some kind of service locator/factory:
// Example without Dependency Injection...
class Foo {
constructor() {
// a 'Meow' instance is created within the Foo constructor
this.fred = new Meow();
// a service locator gets a 'WoofFactory' which in-turn
// is responsible for creating a 'Woof' instance.
// This demonstrates IoC but not Dependency Injection.
var factory = TheServiceLocator.GetWoofFactory();
this.barney = factory.CreateWoof();
}
}
So dependency injection simply means that a class has deferred responsibility of obtaining or providing its own dependencies; instead that responsibility resides with whatever wants to create an instance. (Which is usually an IoC Container)
Dependency Inversion Principle (DIP)
Dependency Inversion is broadly about de-coupling concrete classes by preventing those classes having any direct reference to each other.
The DIP is primarily concerned with ensuring that a class only depends upon higher-level abstractions. For example, interfaces exist at a higher level of abstraction than a concrete class.
The DIP is not about injecting dependencies, although the dependency injection pattern is one of many techniques which can help provide the level of indirection needed to avoid depending on low-level details and coupling with other concrete classes.
Note: Dependency Inversion is often more explicit in statically typed programming languages such as C# or Java, because those languages enforce strict type-checking on variable names. On the other hand, Dependency Inversion is already passively available in dynamic languages such as Python or JavaScript because variables in those languages do not have any particular type restrictions.
Consider a scenario in a statically typed language where a class requires the ability to read a record from the application's database:
// class Foo depends upon a concrete class called SqlRecordReader.
class Foo {
reader: SqlRecordReader;
constructor(sqlReader: SqlRecordReader) {
this.reader = sqlReader;
}
doSomething() {
var records = this.reader.readAll();
// etc.
}
}
In the above example, and despite the use of Dependency Injection, class Foo still has a hard dependency on SqlRecordReader, yet the only thing it really cares about is that there exists a method called readAll() which returns some records.
Consider the situation where SQL database queries are later refactored out into separate micro-services requiring a change to the codebase; the Foo class would need to read records from a remote service instead. Or alternatively, a situation where Foo unit tests need to read data from an in-memory store or a flat file.
If, as its name suggests, the SqlRecordReader contains database and SQL logic, any move to microservices would need the Foo class to change.
Dependency Inversion guidelines suggest that SqlRecordReader should be replaced with a higher-level abstraction which only provides the readAll() method. i.e:
interface IRecordReader {
Records[] getAll();
}
class Foo {
reader: IRecordReader;
constructor(reader: IRecordReader) {
this.reader = reader;
}
}
As per DIP, the IRecordReader is a higher-level abstraction than SqlRecordReader, and forcing Footo depend onIRecordReaderinstead ofSqlRecordReader` satisfies DIP guidelines.
Why DIP Guidelines are Useful
The keyword is guideline - dependency inversion adds indirection to the design of your program. The obvious disadvantage of adding any kind of indirection is that the complexity (i.e. the cognitive "load" required for a human to understand what's going on) increases.
In many cases, indirection can make code easier to maintain (fix bugs, add enhancements) however:
In this last example, Foo might receive a SqlRecordReader, or maybe a SoapRecordReader, or perhaps a FileRecordReader, or maybe even for unit testing a MockRecordReader - the point is that it doesn't know or care anything about different possible implementations of IRecordReader - provided of course those implementations live up to the Liskov Substitution Principle.
Furthermore, it avoids the potentially dirty scenario where a developer who is in a rush to get something working might consider trying to "fudge" the Liskov principle by inheriting the SoapRecordReader or FileRecordReader from a base class SqlRecordReader.
Worse still, an inexperienced developer might even change the SqlRecordReader itself so that class has logic not only for SQL but also for SOAP endpoints, The Filesystem, and anything else which might be needed. (This kind of thing happens too often in the real world - especially in poorly maintained code, and is nearly always a Code Smell.)

See this article here
The author differentiates these two in simple words. Dependency Injection == “Gimme it” and Dependency Inversion == “Someone take care of this for me, somehow.”. In dependency inversion principle, high level module is the owner of the abstraction. Thus the detail(implementation of the abstraction) is depends on the abstraction and hence depends on the high level module. Dependency inverted!.. Dependency injection is different. The abstraction might not be kept by the high level module. Thus an abstraction given to the higher level object might not be limited to the needs of the high level module.
Dependency inversion :
You have a higher level module X and an abstraction Y which is defined by X. Z implements Y and is given to X. Thus Z is dependent of X(via the abstraction Y defined by X).
Dependency injection:
You have an higher level module X which needs functionalities A and B. Y is an abstraction which contains the functionalities A, B and C. Z implements Y. Since Z implements Y and hence have functionalities A and B, Z is given to X. Now X is dependent of Y.

Dependancy injection is ability of object to supply depndcies of other object. in simple words it means something else is dependent on something else. Example Class A uses few functions of Class B now Class A needs to create the instance of Class B here the use of DI comes in to use.
IOC is to invert the different resposibilities for example you need to work at home but you need to cook to eat now listed cooking at home you can order it online and it is available to your door step which means you can focus on your work. Here you inverted cooking responsibilities to online restorent.
The Dependency Inversion Principle (DIP) states that high level modules should not depend on low level modules; both should depend on abstractions. Abstractions should not depend on details. Details should depend upon abstractions.

Dependency Injection is one way to achieve Inversion of Control (which I am assuming you are referring to as Dependency Inversion), so the two aren’t really in competition as much as DI is a specialization of IoC. Other commonly seen ways to achieve IoC include using factories or the Service Locator pattern.

Related

Avoid long lists of arguments in constructor service Symfony

I've been using Symfony to develop my web applications but I keep running into one problem. I always end up with too much clutter in the constructor of my services since I want to be able to unit test my services properly.
Theoretical use case
Let's say I need a service which allows me to process an XML file and save it's contents into the database.
<?xml version="1.0" encoding="UTF-8" ?>
<users>
<user>
<id>1234</id>
<username>Example User</username>
<email>user#example.com</email>
<usergroup>
<id>567</id>
<name>Example User Group</name>
</usergroup>
<permissions>
<item>ALLOWED_TO_CREATE</item>
<item>ALLOWED_TO_UPDATE</item>
<item>ALLOWED_TO_DELETE</item>
<item>ALLOWED_TO_view</item>
</permissions>
</user>
</users>
Already quite a few things come to mind which you need to inject into this service:
DomCrawler (to read the XML file)
UserRepository (to get existing Users)
UserGroupRepository (to get existing UserGroups)
PermissionsRepository (to get existing permissions)
EntityManager (to persist en flush new/updated objects)
The real XML file I'm working with contains much more data which requires me to inject many more repositories and other services which handle certain logic.
Solution 1: Use Doctrine service
Inject the Doctrine service directory into my service and get the repositories via $doctrine->getRepository(User::class)
Pros
Significatly reduces the amount of arguments
Cons
Unit testing becomes a lot harder since I don't know what the service accesses anymore which renders them useless
Solution 2: Use setter injection
Remove all services and repositories from the constructor and create setter methods and call then in the services.yml
services:
AppBundle\Service\MyImportService:
calls:
- [setUserRepository, ['#app.user_repository']]
Pros
Arguably easier to read
Cons
More code
Services no longer mandatory, optional validation may be required
Question
What are solutions to make the argument lists in my services more maintainable in terms of readability and ability to unit test?
Is it even considered bad practise to have a long list of arguments?
Having many arguments in your constructor is a code smell called Constructor Over-Injection. This code smell is often an indication of the class taking on too much responsibility, meaning it violates the Single Responsibility Principle (SRP). SRP violations cause maintenance problems, which is why you should keep a close eye on them.
Although refactoring to Property Injection might reduce the number of constructor arguments and therefore the amount of clutter in the constructor, it does not solve the underlying problem, which is that this class is becoming too complex. Property Injection is therefore not a solution to Constructor Over-Injection.
The solution to this code smell is to reduce the complexity of the class at hand. There are many ways to do this, but a very common approach is the Facade Services refactoring:
Facade Service [is] closely related to Parameter Objects, but the main difference is that a Parameter Object only moves the parameters to a common root, while a Facade Service hides the aggregate behavior behind a new abstraction. While the Facade Service may start its life as a result of a pure mechanistic refactoring, it often turns out that the extracted behavior represents a Domain Concept in its own right.
Pros:
Reduces the class' complexity and thereby solves the underlying problem
Prevents depending on the Doctrine service (which is an application of the Service Locator anti-pattern)
Introduces new concepts and improvements to the Domain language.
Prevents property injection (since property injection causes Temporal Coupling)
Cons:
Facade Services might not be the right refactoring. Alternatives are Domain Events and Decorators.
These are topics that Mark and I in our book Dependency Injection Principles, Practices, and Patterns. Section 6.1, for instance, specifically talks about refactoring from Constructor Over-Injection to Façade Services or Domain Events, while chapters 9 and 10 do a deep dive into using Decorators.

Reusing Complex Object Instances in PHP

Is there a reasonable way to ensure that you aren't unnecessarily re-instantiating expensive object instances in PHP?
I have an application that deal with US States. Each state is an instance of the USState class. I'd like to ensure that the application only ever has one instance of each state object, although they are used in many places throughout the application.
I looked into the singleton pattern, but that seems to be used for times when a class only has a single instance - here there are 50 instances, but no more.
I guess I could put them in the global scope and always refer to them that way (global $california), but that seems wrong.
I would use Service Locator Pattern or Dependency Injection, which is provided by your framework. Usually I go with Zend Framework, but not knowing your circumstances, I assume you have no framework at all. In this case I would suggest very simple Dependency Injection Container, but that would add a dependency on DIC itself. Pimple is one of them.

Difference between aggregation and dependency injection

I have recently studied dependency injection design pattern .
class User
{
private $db;
public function __construct(Database $db)
{
$this->$db = $db;
}
}
I can not help but wonder that is the same thing that i learned in aggregation. Please do correct me if I am wrong . I know goals of dependency injection and aggregation are different . Is there anything that I am missing ?
Aggregation is a form of object composition. It has nothing to do with dependency injection.
In the other hand, dependency injection isn't about how objects are associated with but how to get other objects (dependencies) into a particular object. Dependencies could be aggregates, services, repositories, validators, literals...
Usually, in strongly-typed languages, dependencies are introduced as interfaces to avoid coupling your objects to implementation details. In the opposite side, in dynamically-typed languages, conventions and a strong documentation do the trick to build up a good and tightly-coupled dependency graph.
Note that a database couldn't be an aggregate. Not all associations are considered to be aggregation, while you could consider an injected dependency.
Anyway, there's some design smell in your reasoning: an user shouldn't depend on a database, but a data layer / data mapping layer would be a better candidate to be injected into your user entity if you're going to implement something like active record pattern.
I can not help but wonder that is the same thing that i learned in aggregation
Consider a Department class for example that has an array of Professor objects as an instance variable. There are two ways in which you can initialize the professor array with some Professor objects.
You could initialize the elements of the Professor array within the Department class in some method that takes no parameters by saying professors[0]=new Professor("CK"); and professors[1]=new Professor("MK");.
You could provide a constructor that takes an array of Professor type as its argument. Any class that wants to instantiate a Department would then have to also pass an array of Professor objects to the constructor.
Aggregation : It doesn't matter whether you use option 1 or 2 to define how a Department gets its professors. A professor will continue to exist even if there is no department (assuming the professor belongs to more than one department) and this is therefore said to be aggregation regardless of how the department gets its professors.
Dependency Injection If you use option 2 to create a Department instance, this will be termed as dependency injection. A department needs professors and you are fulfilling this dependency by providing it from outside the Department class.
In other words, Aggregation is a type of relationship that can be modeled either using option 1 or option 2 (or other options such as getting the professors from a database and filling the Professor array in Department inside a method). Dependency injection is way of designing your class so that its dependencies can be provided from outside the class. An aggregate relationship can be modeled to support dependency injection but that does not mean Aggregation and Dependency Injection are the same thing.
Aggregation is generally modeled as a one-to-many relationship, and furthermore, tends to imply part-whole semantics. For these reasons, a DB of Users would generally not be considered aggregation: the relationship is 1:1 and the concept of a User does not necessarily imply a DB.
Contrast this with the common aggregation example of a car and its wheels. There is a one-to-many relationship, and the concept of a Car usually implies that it has wheels.
Dependency Injection may be thought of as the way in which a relationship is created. The relationship may be 1:1 or one-to-many. It may imply certain semantics, or not. The critical feature of DI is that User does not control the creation of DB, nor initiate its creation.
In practice, aggregation relationships may often be created via dependency injection; but that does not imply the two concepts are equivalent. DI can create other kinds of relationships, and aggregation may be created without DI.
Aggregation is a way of maintaining associations between objects. You use this term when describing how you structure relations between objects. It is a domain term, the same kind of relation exists between objects in real life.
Dependency injection is a way to manage dependencies between objects in order to achieve loose coupling. The same techniques can be used in aggregates. But usually, in aggregates, it is not about coupling but about how your objects are related. This term also has analogies from real life but they are used mostly to explain what is the inversion of control, usually, there is no correspondence to your domain.
I decided to add these points.
If I think from your point of view you should be asking this question because Aggregation & Dependency Injection promotes an entity which can be put into some other entities and also can work alone.
But there is a difference. In Aggregation you are working with direct objects. i.e. objects which maintains a state, life-cycle etc.
Ex:
Bulb & Room
Instances of Bulb can be put into instances of Room.
But not only Room, some instances can be put into instances of Car, Radio etc.
And also instances of Bulb can work independently without being put into the instances of other objects too.
But in Dependency Injection we focus on class level interactions. This is somewhat diverged with pure OOP practice. Actually in DI we tend to inject stateless classes (worker classes) into some other classes. Though they look like objects they are actually just stateless classes that are being injected. And instances of that class can stand independently too. DI is widely used as an alternative for Traditional Singleton Pattern because that traditional way has some negative side-effects.
Ex:
Assume a stateless worker class called StringUtils (not the apache one which is stacked with static methods.). It can be injected into classes called NounUtils, VerbUtils etc. And also instances of StringUtils can also exist.
Hope you got an idea. :))

Polymorphism vs dependency injection in PHP?

What is the difference between polymorphism and dependency injection in PHP?
To me they seem like the same thing.
Polymorphism is the provision of a single interface to entities of different types. This means that you define one parent class, i.e. Person and derive multiple other classes off of it. I.E. Mailman, Programmer, Dentist. These child classes all have something in common with person, but they implement specialized functions as well.
Dependency Injection is a software design pattern that implements inversion of control and allows a program design to follow the dependency inversion principle. The term was coined by Martin Fowler. An injection is the passing of a dependency (a service) to a dependent object (a client). A database is a good example of this. Let's say our person class above needs to persist some data about itself. Dependency injection would involve passing a database object into the Person class to work with. The Person class doesn't worry about how the database persists its information, it is only concerned with the public api of the database. You could effectively swap out databases and as long as their apis are the same, the Person class would not care. This becomes very handy when you want to unit test your classes and need to remove the dependency on the database. You can use dependency injection to pass in a mock database that always returns dummy information.
Here are two previous stackoverflow questions related to each:
What is polymorphism, what is it for, and how is it used?
What is dependency injection?
Also, check out Martin Fowler's site for more information of this.
http://www.martinfowler.com/articles/injection.html
I think dependency injection is much simpler. Its like to inject a class(object) and call a parent method. Similar to a wrapper. Polymorphism uses abstract classes and it let you define non-existance functions.
How I understand it now:
polymorphism + injected object = dependency injection
Polymorphism - is when you create objects that implement the same interface, so all objects have the same base methods.
Dependency injection - you inject object, that can be swapped with other object. But all those object implement the same interface (like in polymorphism).

SoftwareArchitecture: Service Dependency - Inject Container or concret class

Assume the following Service classes:
class A {}
class B {}
class C {}
Now Class A gets a dependency on Class B. So i just inject Class B.
Later in the development my Class A needs Class C too.
(This could go on and on...)
Is it better to inject the Dependency Container to be able to use all services as needed. Or keep the service classes small by only injecting those classes i need?
Whats the best practice here, and why?
Currently i tend to inject each dependent service i need, mainly to see the dependency in one place (the DI annotation in the header).
Please don't close as "opinion based" this questions is for best
Practice, this have to be a opinion, but when a majority of users have
the same opinion then its a best practice.
I advise against injecting whole service container. If you do so, class dependencies get blurry (eg. you have to go through whole class code to see what dependencies this class needs) and it may lead to a mess.
Inject these dependencies you need directly. If you noticed that there are a lot dependencies in your class, it should be an alert for you that this class is doing too much (or have too many responsibilities) and you should split it (or separate responsibilities).
Don't inject the container. This is the service locator pattern, and it is usually looked at as an anti-pattern.
Reasons:
injecting the container couples your code to the container, which is bad because if you want to change the container you use, you have lots of classes that needs to be changed
it also hides the dependencies your class needs: dependencies are no longer explicit. Say I want to use your class, how do I know which dependencies I need to set in the container for your class to work?
what happens if you inject the container, and then your class gets a dependency but the dependency doesn't exist: exception
This is not just an opinion, there are good reasons justifying dependency injection over service location.
Injecting concrete classes is contradictory to Dependency Inversion Principle (DIP). In you case, class A (relatively higher class) should not depend on class B (relatively lower class); both should depend on an abstraction instead. So, the abstract class or an interface which is inherited or implemented by class B, should be injected into class A. The same is true for class A and class C.
IMHO, injecting the whole Dependency Container introduces an extra overhead to the injected class. So the class should be injected with those abstractions only as needed. In order to achieve that, it is also needed to design the class accordingly from the very beginning.

Categories