Olgun DUTKAN

Olgun DUTKAN

Senior Developer

How I became an engineer by going to business interviews #episode3

How I became an engineer by going to business interviews #episode3

I started to blog. It's about business interview questions. I will look for the answer to another question. That question;

"What are dependency injection and advantages?"

Dependency injection is very important in software development. Nowadays software has grown popularity and their software written by many developers. This situation caused some problem. Developers will write code to whom and by what? Developers resolved that problem with service. You can write services that run independent from each other and can inject to the service. Let me explain with this an example.

// author.php
class Author {
    private $firstName;
    private $lastName;
 
    public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
 
    public function getFirstName() {
        return $this->firstName;
    }
 
    public function getLastName() {
        return $this->lastName;
    }
}
// question.php
class Question {
    private $author;
    private $question;
    public function __construct($question, $authorFirstName, $authorLastName) {
        $this->author = new Author($authorFirstName, $authorLastName);
        $this->question = $question;
    }
 
    public function getAuthor() {
        return $this->author;
    }
 
    public function getQuestion() {
        return $this->question;
    }
}

Previously we wrote that code for getting the author of the question. But now we can write like blow

// author.php
class Author {
    private $firstName;
    private $lastName;
     
    public function __construct($firstName, $lastName) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
 
    public function getFirstName() {
        return $this->firstName;
    }
 
    public function getLastName() {
        return $this->lastName;
    }
}
// question.php
class Question {
    private $author;
    private $question;
 
    public function __construct($question, Author $author) {
        $this->author = $author;
        $this->question = $question;
    }
 
    public function getAuthor() {
        return $this->author;
    }
 
    public function getQuestion() {
        return $this->question;
    }
}

Without dependency injection code, we must call author class for author information and have to send parameter. In the dependency injection, they don't need it. Not important author class written by whom and how. Write of the author class says, "My class return always author first name and last name". You don't need to call the class and send parameter.

Advantages of Dependency Injection

  1. You can inject open source dependency injection software
  2. You can develop very fast
  3. It makes easier to switch from one service to another service

I will explain more details of these advantages in another blog. Wait for this.. :)

Oct 15, 2017