Wake up Neo... Dependencies have you

Preview:

Citation preview

Ivan Mosiev —

software architect,

AltexSoft

i.k.mosev@gmail.com

http://verber.kh.ua

@polny_otec

Wake up, Neo...

Dependencies have you

Ideal world

Ideal Unit

Welcome to the real world

Real Unit

Dependencies

Imagine, there is no dependencies

Imaginary Unit

Indirect Input

Indirect Output

Indirect Input

$internal = $Dependency->something;

function dependency() { return $something; }

function dependency() { throw new Exception_WTF; }

function dependency(&$by_ref) { $by_ref++; }

Indirect Output

$Dep->something = $internal;

$Dep->doSomething($internal);

Stub

Dummy

Fake

Spy

Mock

function runMatrix() {

$agents = array();

while (TRUE) {

$agents[] = new AgentSmith();

}

}

This is my world, my world!

Dependency Injection

Refactoring: step 0

function doSomething() {$auth = new Service();

…return $profit;

}

Refactoring: step 1

private function getService() {return new Service();

}

function doSomething() {$service = $this->getService();…return $profit;

}

Refactoring: step 2

private $_service;

private function getService() {If (!$this->_service)

$this->_service = new Service();return $this->_service;

}

function setService(IService $service) {$this->_service = $service;

}

do { $the->same();

} while ($deps_count > 0);

function getService() {

if (!$this->_service)

$this->_service = return Service();

return $this->_service;

}

Inversion of Control

IoC Container store information about dependencies

Dependencies instantiated by Container recursively

Dependencies injected into Object by Service Container

Benefits?

Mock and test everything

Loose coupling

Components reuse

Now you know kung fu

Recommended