Master Dependency Injection: Top 10 Interview Questions and Answers with Examples

 1. What is Dependency Injection (DI)?

Answer:

Dependency Injection is a design pattern used to achieve Inversion of Control (IoC). It allows a class to receive its dependencies from an external source rather than creating them itself. DI promotes loose coupling between classes, making the code more maintainable, testable, and flexible.


2. What are the benefits of using Dependency Injection?

Answer:

  • Loosely Coupled Code: Classes are independent of their dependencies.
  • Testability: Dependencies can be mocked or stubbed for testing.
  • Flexibility: Dependencies can be swapped with different implementations easily.
  • Maintainability: Dependency management is centralized, reducing duplication.
  • Scalability: Applications can grow without significant architectural changes.

3. What are the types of Dependency Injection?

Answer:
There are three main types of Dependency Injection:

  1. Constructor Injection: Dependencies are provided through the class constructor.


    public class Car { private readonly IEngine _engine; public Car(IEngine engine) { _engine = engine; } }
  2. Property Injection: Dependencies are set through public properties.

    public class Car { public IEngine Engine { get; set; } }
  3. Method Injection: Dependencies are passed as method parameters.

    public class Car { public void Start(IEngine engine) { engine.Run(); } }

4. What is the difference between Dependency Injection and Inversion of Control (IoC)?

Answer:

  • Inversion of Control (IoC): A broader principle where the control of object creation and management is inverted, i.e., moved away from the class using the dependency.
  • Dependency Injection (DI): A specific implementation of IoC where dependencies are injected into classes by an external source.

5. How is Dependency Injection implemented in .NET Core?

Answer:
In .NET Core, Dependency Injection is built-in and can be implemented using the following steps:

  1. Register services in Startup.cs:

    public void ConfigureServices(IServiceCollection services) { services.AddTransient<IEngine, PetrolEngine>(); services.AddScoped<Car>(); }
  2. Inject services into constructors:

    public class CarController { private readonly Car _car; public CarController(Car car) { _car = car; } }

6. What are the service lifetimes in .NET Core Dependency Injection?

Answer:
.NET Core provides three types of service lifetimes:

  1. Transient: A new instance is created every time the service is requested.

    services.AddTransient<IMyService, MyService>();
  2. Scoped: A single instance is created per request (or scope).

    services.AddScoped<IMyService, MyService>();
  3. Singleton: A single instance is shared across the entire application lifecycle.

    services.AddSingleton<IMyService, MyService>();

7. What is the difference between Constructor Injection and Property Injection?

Answer:

  • Constructor Injection: Dependencies are required and provided at object creation.
    Pros: Ensures all required dependencies are available; promotes immutability.
    Cons: Difficult to use for optional dependencies.

  • Property Injection: Dependencies are set through public properties and are optional.
    Pros: Useful for optional dependencies.
    Cons: Dependencies may not be available during object initialization.


8. What are some common issues with Dependency Injection?

Answer:

  • Over-injection: Injecting too many dependencies into a single class indicates poor design.
  • Unmanaged dependencies: Not disposing of resources properly (e.g., in Singleton lifetime).
  • Complexity: Improper usage can lead to overcomplicated code.
  • Hidden dependencies: Makes debugging harder if dependencies are deeply nested.

9. How does Dependency Injection improve unit testing?

Answer:
DI makes unit testing easier by allowing dependencies to be replaced with mock or stub objects. This isolates the class being tested, ensuring tests focus only on its behavior without interference from real dependencies.

For example:

var mockEngine = new Mock<IEngine>();
var car = new Car(mockEngine.Object); // Test Car behavior without a real Engine.

10. What is the difference between AddTransient, AddScoped, and AddSingleton in .NET Core?

Answer:

  • AddTransient: Creates a new instance of the service every time it is requested. Suitable for lightweight, stateless services.
  • AddScoped: Creates a single instance per HTTP request (or scope). Suitable for services that maintain state within a request.
  • AddSingleton: Creates a single instance throughout the application lifecycle. Suitable for heavy, shared objects like configurations.
Previous Post Next Post

نموذج الاتصال