SOLID & Design Patterns Interview Question #11

What is the difference between Dependency Inversion and Dependency Injection?

SOLID Principles, Design Fundamentals, Repository, Factory, Strategy & Decorator Junior Beginner

Quick Interview Answer

Dependency Inversion is a design principle about the direction of dependencies between high-level policies, abstractions, and implementation details. Dependency Injection is a technique for supplying an object's dependencies from outside the object.

Detailed Explanation

Dependency Inversion is a design principle about the direction of dependencies between high-level policies, abstractions, and implementation details. Dependency Injection is a technique for supplying an object's dependencies from outside the object.

You can use dependency injection poorly and still violate DIP, for example by injecting concrete infrastructure classes everywhere. Conversely, DIP can be implemented through other composition techniques.

Code Example

builder.Services.AddScoped<IOrderRepository, SqlOrderRepository>();
builder.Services.AddScoped<OrderService>();