Dependency Injection in ASP.NET Core

๐Ÿ” What is Dependency Injection?

Dependency Injection (DI) is a design pattern that allows objects to receive their dependencies from an external source rather than creating them internally. ASP.NET Core has built-in support for DI, which helps in creating loosely coupled, testable, and maintainable applications.

๐Ÿง  Analogy

Imagine you're going to a hotel. Instead of carrying your own pillow, towel, or soap, the hotel provides them to you. You're dependent on those items, but you don't create or bring them โ€” they are injected into your room by the hotel service.

โš™๏ธ How DI Works in ASP.NET Core

  • ConfigureServices in Program.cs is used to register services.
  • You use constructor injection to inject services into controllers or other classes.
public interface IMessageService {
    string GetMessage();
}

public class HelloService : IMessageService {
    public string GetMessage() => "Hello from DI!";
}

// In Program.cs
builder.Services.AddScoped<IMessageService, HelloService>();

// In Controller
public class HomeController : Controller {
    private readonly IMessageService _service;
    public HomeController(IMessageService service) {
        _service = service;
    }
}

๐ŸŽฏ Interview Q&A

  • Q: What is dependency injection?
    A: It is a technique where an object receives other objects it depends on, promoting loose coupling.
  • Q: How is DI supported in ASP.NET Core?
    A: Via built-in container using builder.Services in Program.cs.
  • Q: What are the DI lifetimes available?
    A: Transient, Scoped, Singleton.
  • Q: Can we replace the built-in DI container?
    A: Yes, with third-party containers like Autofac, Ninject.
  • Q: What is constructor injection?
    A: Injecting dependencies through a class constructor.
  • Q: Where do you register services in ASP.NET Core?
    A: In the ConfigureServices or builder.Services block in Program.cs.
  • Q: What is service registration?
    A: Telling the container how to create and manage dependencies.
  • Q: Can you inject services into middleware?
    A: Yes, via constructor injection or using the request pipeline context.
  • Q: What is the benefit of DI?
    A: Improves testability, modularity, and maintainability.
  • Q: Is DI mandatory in ASP.NET Core?
    A: No, but it's highly recommended and widely used.

๐Ÿ“ MCQs

Q1. What is Dependency Injection (DI)?

  • A way to hide code
  • Only for testing
  • A design pattern for supplying dependencies
  • A compiler feature

Q2. Which method is used to register services in ASP.NET Core?

  • Configure()
  • Main()
  • builder.Services.Add...
  • UseServices()

Q3. What is constructor injection?

  • Using static classes
  • Passing dependencies via class constructor
  • Injecting using properties
  • None of the above

Q4. Which DI lifetime creates a new instance every time?

  • Singleton
  • Scoped
  • Transient
  • Request

Q5. Which DI lifetime is shared within a request?

  • Transient
  • Scoped
  • Singleton
  • Thread

Q6. Which DI lifetime acts like a global shared service?

  • Scoped
  • Singleton
  • Transient
  • Global

Q7. Can third-party DI containers be used in ASP.NET Core?

  • No
  • Yes
  • Only in .NET Framework
  • Only with MVC

Q8. Which file typically contains service registration?

  • Startup.cs
  • HomeController.cs
  • Program.cs
  • LaunchSettings.json

Q9. Why is DI beneficial?

  • To reduce performance
  • For loose coupling and testability
  • To avoid exceptions
  • For speed

Q10. Is DI mandatory in ASP.NET Core?

  • Yes
  • No
  • No, but recommended
  • Only with Razor

๐Ÿ“„ PDF Download

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

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

Tags: