0

Can anyone help me explaining in 'easy words'

  1. When should I use dependency injection.
  2. Why should I use it? ( Should I use it in combination OR not with MVC?)
  3. What does in it easy words ? How is it working
  4. Is it like creating a factory for recycling your code/scripts?
  5. How does it interact and how do I call it when I use a front-controler ( For example )

Those questions are already on stack, but I still have some problems understanding them. So I am really looking for a simple explanation.

I currently set up my projects the following way ( Dummy ) http://pastebin.com/WJau2CyZ And I have no clue how exact to implement DI.

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Duikboot
  • 1,093
  • 1
  • 12
  • 24
  • 1: when entity has dependencies; 2: to decouple code; 2,5: yes, you should use it; 3: decouples entities from its dependencies; 3,5: it's working well; 4: no, also factories do not recycle code; 5: you do not "call" dependency injection, it's a development method; – tereško May 17 '13 at 08:02

2 Answers2

4

1 When should I use dependency injection?

You should use dependency injection when you want to have control over which dependencies you class/method will use at runtime. The best example of this is when you might want to replace a dependency that accesses a database with one that uses memory for unit testing purposes.

2 Why should I use it?

It makes your code more testable, more flexible and more extensible.

3 What does in it easy words ? How is it working?

It usually works by passing dependencies as parameters to a method or to a class's constructor.

4 Is it like creating a factory for recycling your code/scripts?

No. See 1.

5 How does it interact and how do I call it when I use a front-controller ( For example )?

Whatever creates the class or calls the method will need to satisfy that class/method's dependencies. This might be as simple as 'newing' up a object or getting a IoC framework to wire it all up for you.

David Osborne
  • 6,436
  • 1
  • 21
  • 35
-2

Basically by using dependency injection, you will get rid of static dependencies. E.g. framework or w/e will handle dependencies for you. Its desing pattern used to prevent loose coupling and similar issues.

For more detailed info try wiki.

Regards Inty

Inty
  • 1