C
C#8mo ago
CrosRoad95

❔ Garbage collection question

I see in many libraries examples where in DI ".AddScoped<>", ".AddTransient<>" is used instead of ".AddSingleton<>", example in fluent assertions example says: services.AddScoped<IValidator<User>, UserValidator>(); and i wonder why? this class instance is sorto "read only" so it could be singleton? And here's my question regarding that, because singleton will create long lived instanced meanwhile the other two will live for let's say 100ms, they will be regularly cleared, less often reach further generation. I know that not all singletons are used at all time, they will live in memory for long time and cause extra pressure on garbage colletor. What do you think? is it better to use Transient/Scoped even when singleton make sense from memory point of view?
4 Replies
Angius
Angius8mo ago
Mind you, singleton services cannot use scoped or transient ones singleton -> scoped -> transient direction works transient -> scoped -> singleton does not So, perhaps, UserValidator requires some scoped service, that's why it cannot be a singleton
CrosRoad95
CrosRoad958mo ago
true too
IsNotNull
IsNotNull8mo ago
I find that many junior or mid tier application developers have a poor understanding of DI scopes and they often use transient or scoped because it causes the fewest issues and side effects. (e.g. doesn't conflict with the scoped DbContext dependencies). If you use singleton you have to organize that dependency like its stateless or global (with its concurrency safety issues). I also regularly see ambivalence from team members about garbage creation outside of hot loops...and if we're being honest it often does not create any issues. Many apps are IO / database bound. I'll prefer singleton whenever it makes sense, but I don't bother bringing the concept up with team mates very often anymore unless they ask.
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ CS0123No overload for 'CanvasMain_PreviewMouseMove' matches delegate for 'EventMouseHandler'Basically i'm trying to make a system where there is a grid with moveable objects - if you drag the ❔ EntityFramework - How do I figure out what ORM expects?```#region Assembly AgeOfSailOnline_DB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null // Age❔ what learn first as beginerr blazor or asp.neti want to go web dev route with c# and i am confused what is better learn first asp.net or blazor✅ Two calls to same microservice inside one transactionGreetings, I have a .NET Core Web client that is accessing a .NET Core API microservice (that uses ❔ Asynchronously events handling flowHi, I am writing a bot and I don't know how to organize events handling with tasks. I have class th❔ I can't make questpdf works on my .net 4.8 winforms app.First of all sorry if my post's type isn't allowed I'm a newbie. The code just does not work for me ❔ ✅ It's not writing to the console that car1 is driving```cs Car car1 = new Car("ford", "mustang", 2022, "red"); Car car2 = new Car("idk", "idk", 2001, ❔ ✅ Trying to sort and display data in datagridviewCurrently i can sort the data but it only displays the last row of data because of the for loop but ❔ Style auto apply to all target controlsI recently created this xaml style, I don't why UWP apply that thing to all the navigation view I on❔ Automatic discovery of scoped services without a marker interfaceIn the past, I had an `IService` interface which defined a (singleton at the time) service and a con