Monday, August 6, 2007

Dependency Injection

Scott wrote a nice intro article here.

Usuall code to access database from Biz Service. Shows dependency of db in bizService.

There 2 types of Dependency - Opaque / Transparent


Opaque below - hard to test...for ex. you want to test without accessing the actual db instead use a fakeDb...its hard in this case.

Transparent
- dependency in case is INVERTED, it is asigned from outside.
- it is passed as a constructor argument
- easy to test...now you can pass a fakeDb object to test

to make it even more easy to test, consider the following change:

Now lets look at various ways to instantiate the object