SOLID & Design Patterns Interview Question #10

What is the Dependency Inversion Principle (DIP)?

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

Quick Interview Answer

The Dependency Inversion Principle says high-level policy should not depend directly on low-level implementation details; both should depend on abstractions. It also says abstractions should not depend on details, while details should depend on abstractions.

Detailed Explanation

The Dependency Inversion Principle says high-level policy should not depend directly on low-level implementation details; both should depend on abstractions. It also says abstractions should not depend on details, while details should depend on abstractions.

In .NET applications, constructor injection with interfaces is a common implementation technique. DIP is the design principle; dependency injection is one mechanism that can help implement it.

Code Example

public class ReportService
{
    private readonly IReportRepository _repository;

    public ReportService(IReportRepository repository)
    {
        _repository = repository;
    }
}