Click here to Skip to main content
15,892,746 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 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 
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 
Maybe this will help. IoC is a design/architecture concept, DI is an implementation approach. With IoC your are doing structural inversion on object lifecycles. Commonly an operating object will "new" any objects it needs and then invoke operations. With IoC the objects are supplied and the operating object invokes operations on this supplied object. So, behavior lives in the operating object, but the structure has been factored out to a higher level - inverted. Since Interfaces define behavior, they are a common construct in IoC/DI. The simplest implementation of IoC doesn't need DI. This is IoC:
Class Foo {
        public string Bar(Decoder decoder, string inString){ return decoder.decode(inString); }
    }
You have inverted control of lifecycle management. The caller creates and supplies the structure (the object doesn't "new" any ones it wants), and the operating object operates on these objects.

More commonly, you do this in the constructor.
Class Foo {
        readonly Decoder _decoder;
        public Foo(Decoder decoder){
           _decoder = decoder;
        }
        public string Bar(string inString){ return _decoder.decode(inString); }
    }
In some ways, the operating object is less dependent because it doesn't have to manage the selection and lifecycle management of the used object. This is IoC (hard coded) in a nutshell.

DI is one way to address hard coding. You can think of DI as a combined Lookup and Activator. You give DI a "key", it looks up the mapped object, instantiates it using an Activator, and passes out the instance. You can have 1:1 type situations, where the Key and Value are the same. I tell it Foo, and it gives me a Foo object. Or, you can have 1:Many. Interfaces help here. Because with IoC I have decoupled the structure from the behavior, and interfaces are about behavior, I can associate many Values with a Key.
interface IFoo {
    string Bar(string inString);
}

class FooOne : IFoo { }
class FooTwo : IFoo { }

lookup.Add(IFoo, FooOne);
lookup.Add(IFoo, FooTwo);
This provides another form of loosening dependencies and coupling.
class Foo{
    IFoo _foo;
    public Foo(IFoo foo){ _foo = foo }

}
The DI system "intercepts" object creation, inspects the constructor to see if any types are in the lookup, supplies those instances, and then lets the creation process resume.

To your question about "new", with "new" I have to know the class and it's details. Here I just need to know the (usually smaller) set of relevant methods on an interface. In theory, the DI system could randomly pick FooOne or FooTwo, and things should work the same. Of course, there is a lot more to this.

So, "less dependent" begs, "In what way?". From the perspective of having to know about all of the methods, we can use interfaces to minimize the set of those we need to know. A class may have 100 methods, but if IFoo only has one, we can use the class and completely ignore the other 99. Or I can use "best fit" implementations - one implementation (FooOne) may work better with small data sets, and one (FooTwo) with large data sets. I can swap them in and out in real time using DI, never touching a "new FooXxx()". Better though, is (conceptually) we can design code without ever thinking about implementation. We have factored out structure (and it's implementation), and deal only with behavior. You could write an entire application using only interfaces and test stubs. Then you could build/modify/buy classes that implement those interfaces. You could plug them in manually or use a DI system to do it dynamically.
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 
GeneralRe: Can you actually HEAR the blood running through your veins... Pin
Nelek10-Oct-18 8:58
protectorNelek10-Oct-18 8:58 
QuestionRe: Can you actually HEAR the blood running through your veins... Pin
megaadam10-Oct-18 4:32
professionalmegaadam10-Oct-18 4:32 
AnswerRe: Can you actually HEAR the blood running through your veins... Pin
Johnny J.10-Oct-18 4:55
professionalJohnny J.10-Oct-18 4:55 
JokeRe: Can you actually HEAR the blood running through your veins... Pin
Slacker00710-Oct-18 8:08
professionalSlacker00710-Oct-18 8:08 
GeneralToxic Jira Syndrome... Pin
glennPattonWork39-Oct-18 22:11
professionalglennPattonWork39-Oct-18 22:11 

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.