Skip to main content

What is the difference between IOC and DI?

Inversion of Control (IOC) and Dependency Injection (DI) are related concepts in software development, but they are not the same thing.
 

IOC (Inversion of control) is the design target intended to achieve between the classes and their dependencies,

In other words a class should not create or control the objects it uses, but instead should rely on an external source to provide those objects. This is typically achieved by using a container or framework that creates objects and manages their lifetimes 

 

DI (Dependency Injection) is one of the design patterns used to achieve this target

There are other ways to achieve IOC which is subjected to the application 

As the name suggests the flow of controls is inverted to improve the performance, maintainability and scalability.

 

Simply put (DI) is a specific implementation of IOC where a class's dependencies (i.e. the objects it uses) are provided to it by an external source, rather than the class creating or controlling them directly. This is typically achieved by using a DI container or framework, which can be configured to create and inject the required objects into a class.


In other words, IOC is a pattern, and DI is one way of implementing that pattern. You can use IOC without using DI.



 

 In this example, an instance of the IDependency interface is passed to the constructor of the DIClass class. The DIClass class does not create or control the IDependency object, but instead relies on an external source to provide it.

Comments

Popular posts from this blog

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods