Monday, July 10, 2023

.Net Dependency Injection Options

I was asked today by a colleague about the differences between AddScoped, AddTransient, and AddSingleton. I generally know the differences, but struggled to explain them to her. Fortunately, a quick Google search lead me to a super easy answer.

First, here's the full Stack Overflow answer. There's a TL;DR right at the top that says:

  •  Transient objects are always different; a new instance is provided to every controller and every service
  • Scoped objects are the same within a request, but different across different requests
  • Singleton objects are the same for every object and every request 

That summary comes from this Microsoft tutorial on configuring Dependency Injection.

No comments:

Post a Comment