- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
I’ve been working as a .NET + Angular developer for over 6 years, and if there’s one thing I’ve learned, it’s this: Dependency Injection can be both your best friend and your worst nightmare.
When I started, I thought DI would make my life easier (and in many cases, it did). But along the way, I also created some absolute disasters 🤦♂️—the kind of mistakes that broke builds, confused teammates, and even made me laugh at my own code later on.
So today, I’m sharing some of the biggest Dependency Injection fails I’ve personally faced in .NET. Hopefully you can learn from my mistakes (and maybe have a chuckle too). Let’s go 👇
1. Injecting Everything Into Everything 🐍
public class UserController
{
public UserController(
IUserService userService,
IOrderService orderService,
IEmailService emailService,
INotificationService notificationService,
ILoggingService loggingService)
{
// constructor longer than my weekend
}
}
🔴 Fail: Constructor parameters longer than a shopping list.
✅ Fix: Split responsibilities, use service facades, or rethink your design.
2. Singleton… with Scoped Dependencies 😱
services.AddSingleton<UserManager>(); services.AddScoped<IUserRepository, UserRepository>();
🔴 Fail: Injecting a scoped service into a singleton leads to unpredictable runtime errors.
✅ Fix: Keep lifetimes consistent. If it’s a singleton, it can’t depend on scoped or transient services.
3. Forgetting to Register a Service 🤷
public class PaymentService : IPaymentService
{
// implementation...
}
And then in startup:
// oops, forgot to add: services.AddScoped<IPaymentService, PaymentService>();
🔴 Fail: Application runs fine… until you hit the endpoint and get:
System.InvalidOperationException: Unable to resolve service for type 'IPaymentService'
✅ Fix: Always double-check your Startup.cs or Program.cs registrations.
4. Registering the Same Service 10 Times 🔁
services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>(); services.AddScoped<IUserService, UserService>();
🔴 Fail: Yes, .NET will take the last registration, but now you’ve got a messy, confusing setup.
✅ Fix: Register once. If you must override, document it clearly.
5. Injecting the DbContext Everywhere 🍝
public class ReportGenerator
{
private readonly AppDbContext _db;
public ReportGenerator(AppDbContext db)
{
_db = db;
}
}
🔴 Fail: DbContext is everywhere, even in classes that shouldn’t touch the database.
✅ Fix: Introduce repositories or service layers instead of letting DbContext leak into every class.
6. Service Locator Anti-Pattern 🔍
public class UserService
{
private readonly IServiceProvider _provider;
public UserService(IServiceProvider provider)
{
_provider = provider;
}
public void DoSomething()
{
var repo = _provider.GetService<IUserRepository>();
}
}
🔴 Fail: This completely defeats the purpose of DI. It’s basically a global variable disguised as DI.
✅ Fix: Inject the dependencies you need directly instead of using the service locator.
Conclusion 🎯
Dependency Injection in .NET is amazing when done right—but it can also lead to chaos when misused. Avoid over-injecting, keep service lifetimes consistent, and resist the urge to use the service locator. By dodging these fails, you’ll have cleaner code, happier teammates, and fewer runtime surprises.
💬 Have you ever hit a hilarious DI fail in your .NET projects? Drop it in the comments—I’d love to see your stories!
- Get link
- X
- Other Apps



Comments
Post a Comment