How would you implement dependency injection in an ASP.NET Core application?
Anonymous
To answer this, I first explained what dependency injection is – essentially a design pattern that allows me to inject dependencies into a class, which helps reduce coupling between components. Then, I gave an example where I might create an interface, say IMyService, and then implement that interface in a class, MyService. After that, I would go into the Startup.cs file of my ASP.NET Core application and register the service by adding services.AddScoped(); in the ConfigureServices method. This way, whenever IMyService is required, the framework knows to provide an instance of MyService. I also mentioned how this improves testability since I can easily swap out the real implementation for a mock during testing, and it keeps the code flexible and maintainable.