0

always read IOC (Inversion of Control) and DI(Dependency Injection) in the same context. What is exactly the difference between IOC and DI? How does IOC differ from DI? and how IOC better than other design pattern.

  • 1
    Have a look in the tag wiki of dependency injection: https://stackoverflow.com/tags/dependency-injection/info – SomeBody Feb 25 '21 at 07:21
  • Dependency Injection is the mechanism you use to achieve Inversion of Control. It provides your solution a degree of modularity that allows you easy testing of the different components (automated testing) and changind major components in the future if needed (example: DI to data access makes it an interface, changing from SQL Server to MySQL would be straighforward because both data access layers would implement the same interface). – Cleptus Feb 25 '21 at 07:21
  • @Cleptus thank you. But is that only Test-Driven is reason for chose this principle. – Naveed Shahid Feb 25 '21 at 07:39
  • "Why is it better" is a highly subjective question; and besides, it's like asking "why are fruits better than apples," since DI is a specific form of IoC. – Steven Feb 25 '21 at 07:54

2 Answers2

3

A very good explanation of IoC can be found here: tutorialsteacher

So to set this in coding terms (these are just crude examples, here for educational purpose only):

  1. Your class does some business logic and creates the appropriate objects to use and implement them.
public class Payment {
   public bool Execute(PaymentType type, decimal amount) {
        if (type == 1) {
            var payment = new PaymentService();
        } // etc
   }
}
  1. By applying IoC a factory will be responsible to create the appropriate objects to get the job done
public class Payment {
   public bool Execute(PaymentType type, decimal amount) {
        var payment = PaymentServiceFactory.Create(type);
   }
}

public class PaymentServiceFactory {
   public static PaymentService Create(PaymentType type) {
        if (type == 1) {
            return new PaymentService();
        } // etc
   }
}
  1. By using DIP (Dependency inversion principle) as defined in SOLID principles, your class now expects only abstraction (interfaces) to do the work for them.
public class Payment {
   public IPaymentService paymentService;
   public bool Execute(PaymentType type, decimal amount) {
        paymentService = PaymentServiceFactory.Create(type);
   }
}
  1. DI will now provide (instead of the factory) the proper implementations to your class via constructor or parameter injection.

enter image description here

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Thank you. So In that picture IOC and DIP working like as Principle and DI is like pattern. But how IOC container work like a Framework? – Naveed Shahid Feb 25 '21 at 07:34
  • The internal workings of IoC container depend on the implementation. In general the DI container is configured via configuration files or code, it is initialized on the appropriate hookpoint (depending on the application) and the instantiation of the classes uses the container to inject the dependencies. – Athanasios Kataras Feb 25 '21 at 07:36
2

Chapter 1 of Dependency Injection Principles, Practices, and Patterns contains a sidebar on the difference between DI and IoC:

Dependency Injection or Inversion of Control?

The term Inversion of Control originally meant any sort of programming style where an overall framework or runtime controlled the program flow. According to that definition, most software developed on the .NET Framework uses IoC. When you write an ASP.NET Core MVC application, for instance, you create controller classes with action methods, but it’s ASP.NET Core that will be calling your action methods. This means you aren’t in control — the framework is.

These days, we’re so used to working with frameworks that we don’t consider this to be special, but it’s a different model from being in full control of your code. This can still happen for a .NET application, most notably for command-line executables. As soon as Main is invoked, your code is in full control. It controls program flow, lifetime — everything. No special events are being raised and no overridden members are being invoked.

Before DI had a name, people started to refer to libraries that manage Dependencies as Inversion of Control Containers, and soon, the meaning of IoC gradually drifted towards that particular meaning: Inversion of Control over Dependencies. Always the taxonomist, Martin Fowler introduced the term Dependency Injection to specifically refer to IoC in the context of dependency management. Dependency Injection has since been widely accepted as the most correct terminology. In short, IoC is a much broader term that includes, but isn’t limited to, DI. [section 1.4.1 page 29]

Steven
  • 166,672
  • 24
  • 332
  • 435