What is dependency injection in .NET Core?

๐Ÿ’ก Concept: Dependency Injection in .NET Core

Dependency Injection (DI) is a design pattern that promotes loose coupling by injecting dependencies into classes rather than hard-coding them.

๐Ÿ“˜ Quick Intro

DI allows classes to declare dependencies that are provided by an external service container at runtime.

๐Ÿง  Analogy

Like a restaurant where chefs are provided ingredients rather than growing them themselves.

๐Ÿ”ง Technical Explanation

  • DI decouples class dependencies for better testability and maintainability.
  • .NET Core provides built-in DI via IServiceCollection.
  • Supports constructor, property, and method injection.
  • Helps manage object lifetimes (transient, scoped, singleton).
  • Improves modularity and adherence to SOLID principles.

๐ŸŽฏ Use Cases

  • โœ… Injecting services into controllers.
  • โœ… Unit testing with mocked dependencies.
  • โœ… Configuring logging, caching, and data access layers.
  • โœ… Managing service lifetimes efficiently.

๐Ÿ’ป Code Example


// Registering and using DI in .NET Core
public interface IMessageService {
    void Send(string message);
}

public class EmailService : IMessageService {
    public void Send(string message) {
        Console.WriteLine($""Email sent: {message}"");
    }
}

// Startup.cs or Program.cs
services.AddTransient();

public class HomeController {
    private readonly IMessageService _messageService;

    public HomeController(IMessageService messageService) {
        _messageService = messageService;
    }

    public void Notify() {
        _messageService.Send(""Hello from DI!"");
    }
}

โ“ Interview Q&A

Q1: What is dependency injection?
A: A design pattern that injects dependencies rather than hard-coding them.

Q2: What are the types of dependency injection?
A: Constructor, property, and method injection.

Q3: How does .NET Core support DI?
A: Via built-in IServiceCollection and service provider.

Q4: What are service lifetimes?
A: Transient, Scoped, Singleton.

Q5: Why use DI?
A: To improve testability and maintainability.

Q6: Can DI help with unit testing?
A: Yes, by injecting mock dependencies.

Q7: What is IoC?
A: Inversion of Control, a principle behind DI.

Q8: Is DI mandatory in .NET Core?
A: No, but highly recommended.

Q9: Can services be injected in ASP.NET Core controllers?
A: Yes.

Q10: What is the difference between transient and singleton?
A: Transient creates new instance; singleton shares one.

๐Ÿ“ MCQs

Q1. What is dependency injection?

  • Hard-coding dependencies
  • A design pattern for loose coupling
  • Direct instantiation
  • Static references

Q2. What are types of DI?

  • Constructor only
  • Property only
  • Constructor, property, method
  • Method only

Q3. How does .NET Core support DI?

  • IServiceProvider
  • IServiceCollection
  • ServiceLocator
  • None

Q4. What are service lifetimes?

  • Transient only
  • Scoped only
  • Transient, Scoped, Singleton
  • Singleton only

Q5. Why use DI?

  • Improve performance
  • Improve testability
  • Reduce code
  • Increase complexity

Q6. Can DI help unit testing?

  • No
  • Yes
  • Sometimes
  • Rarely

Q7. What is IoC?

  • Input of Control
  • Inversion of Control
  • Injection of Control
  • Initialization of Control

Q8. Is DI mandatory?

  • Yes
  • No
  • Optional
  • Always

Q9. Can services be injected into controllers?

  • No
  • Yes
  • Sometimes
  • Never

Q10. Difference between transient and singleton?

  • Transient shares
  • Singleton creates new
  • Transient creates new, singleton shares
  • Both create new

๐Ÿ’ก Bonus Insight

Dependency Injection is a fundamental design pattern that improves modularity, testability, and maintainability in .NET Core applications.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: