Why so many terms to say the same thing? IoC and DIP
IoC = Inversion Of Control
DIP = Dependency Inversion Principle (D in S开发者_开发问答.O.L.I.D.)
IoC == DIP? I think so, is true.
The world of building software is already so messy, why so many words to say the same thing?
(I know DI (Dependency Injection) and it is different of DIP and IoC)
Update:
According to the answers, then we can say that: (DI) + (IoC) = (Dependency Inversion Principle) ?
Inversion of Control is the generic term. Dependency Injection is a specific type of IoC.
See Inversion of Control Containers and the Dependency Injection pattern.
The Dependency Inversion Principle is a guideline, while the other terms are descriptive of techniques. (IoC can be used to describe a principle as well, so this can be confusing.)
Used in a sentence:
Sue followed the Dependency Inversion Principle by using Dependency Injection in the constructor of her class and creating instances with an Inversion of Control container.
Update:
I do not feel that (DI) + (IoC) = (Dependency Inversion Principle) is accurate. That's like saying (Apple) + (Food) = (Good Nutrition). Each term has a specific meaning.
I don't think you're going to get an authoritative answer on this one because the term "IoC" is kind of overloaded and to say "the one true meaning of IoC is..." is kind of pedantic. But I'll share my opinion anyway :)
Dependency Inversion is about depending on an abstraction. Consider a HelloWorld
class that depends on an IStringWriter
, and an implementation class ConsoleStringWriter
.
Inversion of Control is when the framework/infrastructure invokes application code, rather than the other way around. For example, when a user closes a WPF app, you don't call the framework, it raises an event that you can subscribe to.
They are often combined. For example, Hibernate depends on the abstraction defined by its Interceptor interface in order to implement IoC. From an Interceptor's point of view, control is inverted - Hibernate calls the Interceptor. Another example you see all over is IHandle<T>
where T is an Event or a Command or a Message - the infrastructure calls the handler at the right time.
Its confusing because we call them "IoC containers", but you can do DI without doing IoC. If you inject a ConsoleStringWriter
into a HelloWorld
I don't really think of this as IoC because there is no "framework" or "infrastructure". But that's because Hello World is trivial - as an application becomes more complex the need for IoC increases.
See also this question and accepted answer.
精彩评论