Junior — Middle
What tool or library have you used for dependency injection in your project?
sobes.tech AI
Answer from AI
To implement dependencies in C# projects, libraries that implement the Dependency Injection (DI) pattern are often used. One popular tool is Microsoft.Extensions.DependencyInjection — a built-in container in .NET Core.
Other well-known libraries include:
- Autofac
- Ninject
- Unity
Example of using Microsoft.Extensions.DependencyInjection:
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddTransient<IMyService, MyService>();
var provider = services.BuildServiceProvider();
var service = provider.GetService<IMyService>();
This makes it easy to manage dependencies, improve testability, and maintainability of the code.