Possible Duplicate:
What is Inversion of Control?
I understand what Dependency Injection (DI) is (I think!). It's basically the satisfying of dependencies that an object may have. I try to think of the code I write when using DI as service oriented and I define my code as using other services.
However, I'm currently wondering what it is exactly that we're inverting the control of, when using IoC. It's a fairly vague term and it could mean a couple of things.
But, I think it's the responsibility of creating the object (and therefore satisfying dependencies using DI) which is handled by the IoC framework.
It is still the responsibility of the application to ask for objects (ie - services) that it uses, the distinction being that it doesn't know (or care) how to create it. So, why is service locator considered an anti-pattern if all it's doing is asking for a service?
Have I got that correct? Or does it mean something else. Also, have I got the responsibilities of DI and IoC separated correctly? If I have then an IoC framework cannot function without a DI framework. Or is DI just a feature of IoC frameworks?
Inversion of control is a software design principle that asserts a program can benefit in terms of pluggability, testability, usability and loose coupling if the management of an application's flow is transferred to a different part of the application.
Inversion of Control (IoC) is a design principle that allows classes to be loosely coupled and, therefore, easier to test and maintain. IoC refers to transferring the control of objects and their dependencies from the main program to a container or framework.
In software engineering, inversion of control (IoC) is a programming principle. IoC inverts the flow of control as compared to traditional control flow. In IoC, custom-written portions of a computer program receive the flow of control from a generic framework.
Inversion of Control(IoC) is also known as Dependency injection (DI). The Spring container uses Dependency Injection (DI) to manage the components that build up an application and these objects are called Spring Beans. Spring implements DI by either an XML configuration file or annotations.
Dependency injection generally means passing a dependent object as a parameter to a method, rather than having the method create the dependent object. What it means in practice is that the method does not have a direct dependency on a particular implementation; any implementation that meets the requirements can be passed as a parameter.
Inversion of control simply recognizes that the dependency relationship is reversed. Instead of A depending on B by way of creating, implementing, or calling it directly, A receives B as a parameter, and is no longer responsible for B in any way.
Implementing parameter types as interfaces simplifies the process, and generalizes it, but it's not strictly necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With