Click here to Skip to main content
15,915,019 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.

 
GeneralRe: Dependency Injection/IoC PinPopular
Sander Rossel10-Oct-18 8:37
professionalSander Rossel10-Oct-18 8:37 
GeneralRe: Dependency Injection/IoC Pin
Marc Clifton10-Oct-18 9:10
mvaMarc Clifton10-Oct-18 9:10 
GeneralRe: Dependency Injection/IoC Pin
Slacker00710-Oct-18 9:42
professionalSlacker00710-Oct-18 9:42 
GeneralRe: Dependency Injection/IoC Pin
Marc Clifton11-Oct-18 4:01
mvaMarc Clifton11-Oct-18 4:01 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:07
TNCaver11-Oct-18 4:07 
GeneralRe: Dependency Injection/IoC Pin
TNCaver11-Oct-18 4:09
TNCaver11-Oct-18 4:09 
GeneralRe: Dependency Injection/IoC Pin
Slacker00711-Oct-18 5:44
professionalSlacker00711-Oct-18 5:44 
GeneralRe: Dependency Injection/IoC Pin
Jon McKee10-Oct-18 10:26
professionalJon McKee10-Oct-18 10:26 
DI is a very useful tool as many others have already pointed out. But it's just that, a tool. Don't fall into the "everything looks like a nail because I just learned how to use a hammer" trap. Also don't confuse DI the concept with DI frameworks. DI frameworks are not required to implement the concept of DI. The frameworks are for a very specific situation (very complex DI "webs") and are grossly overused. All you're doing is offloading coupling to external code you have no control over and incorporating extra dependencies into your software. Make sure that trade-off is worth it.

So consider the details of the situation. In the case of a database class, even if your software only uses a single database now, there are multiple kinds that may be supported in the future. So an interface would be the way to go. Since we're using an interface anyways, why couple a class that uses the database class with a concrete derivative of that interface? DI is the way to go here... maybe.

You also need to consider how your software is going to be used. Is it an application, framework, library, service, etc? If the end-user isn't using the class directly and you want to keep things as loosely coupled as possible internally, you may not have an issue with needing to type something like new Something(new MssqlDb(new AccessInfo()), new RegionFormatter(someRegion), ...more stuff). If the end-user is going to be using this class directly, they'll probably appreciate some default new Something() which is going to require coupling to concrete classes in that constructor. Consider how ugly and bloated code would look if, say, .NET removed all but the most complex constructors for their entire framework just to be "less coupled."

I guess what it boils down to is that whether it's useful depends on the specifics. Use common sense, use experience, and use others' advice. There is no perfect solution to every problem; it's often a cost-benefit analysis.

EDIT: Also as Marc above pointed out, sometimes the factory pattern is the best solution. I tend to think of this as a form of DI except instead of needing a concrete class passed in, you need information that can be used to generate that concrete class. In both cases, the class using the interface isn't coupled to the concrete class.
GeneralRe: Dependency Injection/IoC Pin
Matthew Dennis10-Oct-18 12:30
sysadminMatthew Dennis10-Oct-18 12:30 
GeneralRe: Dependency Injection/IoC Pin
GuyThiebaut10-Oct-18 20:35
professionalGuyThiebaut10-Oct-18 20:35 
GeneralRe: Dependency Injection/IoC Pin
englebart11-Oct-18 4:33
professionalenglebart11-Oct-18 4:33 
GeneralRe: Dependency Injection/IoC Pin
Steve Naidamast11-Oct-18 5:46
professionalSteve Naidamast11-Oct-18 5:46 
GeneralRe: Dependency Injection/IoC Pin
RandyBuchholz12-Oct-18 12:34
RandyBuchholz12-Oct-18 12:34 
GeneralThought of the Day Pin
OriginalGriff10-Oct-18 5:00
mveOriginalGriff10-Oct-18 5:00 
GeneralRe: Thought of the Day Pin
Johnny J.10-Oct-18 5:05
professionalJohnny J.10-Oct-18 5:05 
GeneralRe: Thought of the Day Pin
W Balboos, GHB10-Oct-18 5:21
W Balboos, GHB10-Oct-18 5:21 
GeneralRe: Thought of the Day Pin
megaadam10-Oct-18 5:32
professionalmegaadam10-Oct-18 5:32 
GeneralRe: Thought of the Day Pin
PIEBALDconsult10-Oct-18 5:35
mvePIEBALDconsult10-Oct-18 5:35 
GeneralRe: Thought of the Day Pin
DRHuff10-Oct-18 7:49
DRHuff10-Oct-18 7:49 
QuestionCan you actually HEAR the blood running through your veins... Pin
Johnny J.10-Oct-18 3:52
professionalJohnny J.10-Oct-18 3:52 
AnswerRe: Can you actually HEAR the blood running through your veins... Pin
Kschuler10-Oct-18 4:02
Kschuler10-Oct-18 4:02 
GeneralRe: Can you actually HEAR the blood running through your veins... Pin
Johnny J.10-Oct-18 4:03
professionalJohnny J.10-Oct-18 4:03 
AnswerRe: Can you actually HEAR the blood running through your veins... Pin
W Balboos, GHB10-Oct-18 4:12
W Balboos, GHB10-Oct-18 4:12 
GeneralRe: Can you actually HEAR the blood running through your veins... Pin
Johnny J.10-Oct-18 4:14
professionalJohnny J.10-Oct-18 4:14 
AnswerRe: Can you actually HEAR the blood running through your veins... Pin
theoldfool10-Oct-18 4:25
professionaltheoldfool10-Oct-18 4:25 

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.