Do you know the best dependency injection container?
Updated by Brady Stroud [SSW] 6 months ago. See history
.NET IoC containers
- .NET built-in Dependency Injection (recommended)
- StructureMap (not recommended)
- Ninject (not recommended)
- (and more)
When selecting a Dependency Injection container it is worth considering a number of factors such as:
- Ease of use
- Configurability: Fluent API and/or XML Configuration
- Performance (Unless you have a very high traffic application the difference should be minimal)
- NuGet Support
The top tools all contain comparable functionality. In practice which one you use makes little difference, especially when you consider that your container choice should not leak into your domain model.
Important: Unless a specific shortfall is discovered with the container your team uses, you should continue to use the same container across all of your projects, become an expert with it and invest time on building features rather than learning new container implementations.

❌ Figure: Bad Example - Ninject and StructureMap were top containers but are no longer actively developed. Together with Autofac, they do not support the latest version of .NET
Examples of using IoC container
public class Program{private static void Main(){IContainer container = IoC.Initialize();new BadgeTaskJob(container).Run();}}
❌ Figure: Bad example - Use the StructureMap IoC container but did not do the proper dependency injection
var builder = Host.CreateApplicationBuilder(args);builder.Services.AddScoped<BadgeTaskJob>();using IHost host = builder.Build();using var scope = host.Services.CreateScope();scope.ServiceProvider.GetRequiredService<BadgeTaskJob>().Run();
✅ Figure: Good example - Use .NET built-in Dependency Injection for console app
var builder = WebApplication.CreateBuilder(args);builder.Services.AddSingleton<ITelemetryInitializer, AppInsightsTelemetryInitializer>();builder.Services.AddSingleton<AssetDomain>();var app = builder.Build();app.Run();
✅ Figure: Good example - Use ASP.Net Core built-in Dependency Injection for web app
Need help?
SSW Consulting has over 30 years of experience developing awesome software solutions.