Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is Inversion of Control [duplicate]

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?

like image 862
Antony Scott Avatar asked Dec 23 '10 19:12

Antony Scott


People also ask

What is Inversion of Control in simple terms?

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.

What is Inversion of Control and how it will use?

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.

What is the main principle of Inversion of Control?

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.

What exactly is IoC and DI How are they related?

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.


1 Answers

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.

like image 156
Cylon Cat Avatar answered Sep 19 '22 21:09

Cylon Cat