Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default constructor vs IOC container

Can someone explain to me the advantages of using an IOC container over simply hardcoding the default implementation into a default constructor?

In other words, what is wrong about this code?

public class MyClass
{
    private IMyInterface _myInterface;

    public MyClass()
    {
        _myInterface = new DefaultMyInterface();
    }

    public MyClass(IMyInterface myInterface)
    {
        _myInterface = myInterface;
    }
}

As far as I can tell, this class supports constructor injection enough so unit testing and mocking is easily done. In addition to which, the default constructor removes the computational overhead of the IOC container (not to mention the whole process is a lot more transparent).

The only benefits i can see to using an IOC container is if you need to switch out the implementation of your interfaces frequently. Am I missing something?

like image 404
Kevin Pang Avatar asked Sep 19 '08 10:09

Kevin Pang


People also ask

What is the difference between Spring container and IoC container?

An IoC container is a common characteristic of frameworks that implement IoC. In the Spring framework, the interface ApplicationContext represents the IoC container. The Spring container is responsible for instantiating, configuring and assembling objects known as beans, as well as managing their life cycles.

What is the purpose of an IoC container?

The IoC container that is also known as a DI Container is a framework for implementing automatic dependency injection very effectively. It manages the complete object creation and its lifetime, as well as it also injects the dependencies into the classes.

Is IoC a container?

IoC Container (a.k.a. DI Container) is a framework for implementing automatic dependency injection. It manages object creation and it's life-time, and also injects dependencies to the class.

Which dependency injection is better in Spring?

Use Setter injection when a number of dependencies are more or you need readability. Use Constructor Injection when Object must be created with all of its dependency.


2 Answers

The idea of IoC is to delegate part of your component's functionality to another part of the system. In IoC world, you have components that don't know about each other. Your example violates this, as you're creating tight coupling between MyClass and some implementation of IMyInterface. The main idea is that your component has no knowledge about how it will be used. In your example, your component makes some assumptions about its use.

Actually this approach can work, but mixing IoC and explicit object initialization is not a good practice IMO.

IoC gives you loose coupling by performing late binding for the price of code clarity. When you add additional behavior to this process, it makes things even more complicated and can lead to bugs when some components can potentially receive object with unwanted or unpredicted behavior.

like image 51
aku Avatar answered Oct 29 '22 07:10

aku


Pick a side :)

In short IOC is recommended. The problem with the code is that I cannot swap out the default implementation of the dependency without recompiling the code as you have stated at the end. IOC allows you to change the configuration or composition of your object in an external file without recompilation.
IOC takes over the "construction and assembly" responsibility from the rest of the code. The purpose of IOC is not to make your code testable... it is a pleasant side-effect. (Just like TDDed code leads to better design)

like image 41
Gishu Avatar answered Oct 29 '22 07:10

Gishu