As developers, we are always trying to find new ways to write well designed and clean code by adopting new styles, using design patterns, and trying new robust frameworks. In this article we will explore the dependency injection design pattern through Laravel’s IoC component and see how it can improve our design.
Dependency Injection
Dependency injection is a term coined By Martin Fowler, and it’s the act of injecting components into your application. Like Ward Cunningham said:
Dependency Injection is a key element of agile architecture.
let’s see an example:
class UserProvider{
protected $connection;
public function __construct(){
$this->connection = new Connection;
}
public function retrieveByCredentials( array $credentials ){
$user = $this->connection
->where( 'email', $credentials['email'])
->where( 'password', $credentials['password'])
->first();
return $user;
}
}
If you want to test or maintain this class, you would have to access a real database and do some queries. To avoid having to do that and to decouple the class from the rest, you have one of three options to inject the Connection
class without actually using it directly.
Continue reading %Dependency Injection with Laravel’s IoC%
more
{ 0 comments... » Dependency Injection with Laravel’s IoC read them below or add one }
Post a Comment