Click here to Skip to main content
15,917,005 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
JokeRe: When to use dependency injection? Pin
Super Lloyd11-Feb-15 14:57
Super Lloyd11-Feb-15 14:57 
AnswerRe: When to use dependency injection? Pin
Brisingr Aerowing11-Feb-15 15:00
professionalBrisingr Aerowing11-Feb-15 15:00 
AnswerRe: When to use dependency injection? Pin
al13n11-Feb-15 15:20
al13n11-Feb-15 15:20 
GeneralRe: When to use dependency injection? Pin
Super Lloyd11-Feb-15 15:59
Super Lloyd11-Feb-15 15:59 
GeneralRe: When to use dependency injection? Pin
Chad3F15-Feb-15 15:07
Chad3F15-Feb-15 15:07 
AnswerRe: When to use dependency injection? Pin
tgrt11-Feb-15 17:08
tgrt11-Feb-15 17:08 
GeneralRe: When to use dependency injection? Pin
Super Lloyd11-Feb-15 18:14
Super Lloyd11-Feb-15 18:14 
AnswerRe: When to use dependency injection? PinPopular
Erik Funkenbusch11-Feb-15 18:37
Erik Funkenbusch11-Feb-15 18:37 
The rule of thumb I use is pretty simple. If i'm designing a class that depends on some other class, and there is a logical reason for that class to change either at runtime or in testing then I use dependency injection.

I also use DI when it makes sense to control something at runtime, such as a configuration entry, connection string, etc... or anything I might otherwise use a factory for.

Your example above for INowResolver seems kind of silly at first glance, but in reality, it's a great example of a DI resource. The problem with DateTime.Now is that you cannot alter this in testing, it always uses "Now" even when you might want to use a different time for "Now" (think when you want to test what happens in 2037 when the Y2037 bug hits, you can now substitute INowResolver with a different date/time for "now" to test what will happen in the future). Yes, you could write conditional code in your method to deal with this, but now you have test code in your methods, rather than in your test cases, which makes them messier and more difficult to maintain. There are also "Moles" or Proxy stubs that can replace the runtime behavior of methods like DateTime.Now, but not everyone uses those.

Your problem here is that you're not truly understanding the purpose of Dependency Injection, and as such you have a hard time understanding why it's used so much. You probably see it as a simple service locator or abstract factory service... injecting stuff so you don't have to bother typing "new". While it's true that you can use it for that, it really opens up a whole new array of possibilities when you "drink the Kool-Aid".

As for your Utility functions... in general, no.. you don't need to use DI for utility functions if there is no configuration or other dependencies involved. Particularly if they are pure static methods with no state.
--
Where are we going? And why am I in this handbasket?

GeneralRe: When to use dependency injection? Pin
Super Lloyd11-Feb-15 18:50
Super Lloyd11-Feb-15 18:50 
AnswerRe: When to use dependency injection? Pin
Dominic Burford11-Feb-15 21:14
professionalDominic Burford11-Feb-15 21:14 
GeneralRe: When to use dependency injection? Pin
Super Lloyd11-Feb-15 21:38
Super Lloyd11-Feb-15 21:38 
GeneralRe: When to use dependency injection? Pin
Dominic Burford11-Feb-15 23:09
professionalDominic Burford11-Feb-15 23:09 
GeneralRe: When to use dependency injection? Pin
jschell13-Feb-15 12:34
jschell13-Feb-15 12:34 
GeneralRe: When to use dependency injection? Pin
jschell13-Feb-15 12:29
jschell13-Feb-15 12:29 
AnswerRe: When to use dependency injection? Pin
Rob Philpott11-Feb-15 23:06
Rob Philpott11-Feb-15 23:06 
AnswerRe: When to use dependency injection? Pin
Mark_Wallace11-Feb-15 23:27
Mark_Wallace11-Feb-15 23:27 
AnswerRe: When to use dependency injection? Pin
Chris Maunder12-Feb-15 3:37
cofounderChris Maunder12-Feb-15 3:37 
AnswerRe: When to use dependency injection? Pin
MCMickJ1913-Feb-15 3:12
MCMickJ1913-Feb-15 3:12 
AnswerRe: When to use dependency injection? Pin
Ed K13-Feb-15 8:04
Ed K13-Feb-15 8:04 
GeneralRe: When to use dependency injection? Pin
Super Lloyd13-Feb-15 19:52
Super Lloyd13-Feb-15 19:52 
AnswerRe: When to use dependency injection? Pin
jschell13-Feb-15 12:27
jschell13-Feb-15 12:27 
GeneralNot much going on... Pin
Brisingr Aerowing11-Feb-15 12:10
professionalBrisingr Aerowing11-Feb-15 12:10 
GeneralRe: Not much going on... Pin
Super Lloyd11-Feb-15 16:00
Super Lloyd11-Feb-15 16:00 
GeneralRe: Not much going on... Pin
Mark_Wallace11-Feb-15 23:32
Mark_Wallace11-Feb-15 23:32 
GeneralRe: Not much going on... Pin
Brisingr Aerowing12-Feb-15 5:22
professionalBrisingr Aerowing12-Feb-15 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.