C
C#CrosRoad95

❔ IEnumerable and dependency injection

if i want to loop enumerable from DI multiple times
var foos = services.GetRequiredService<IEnumerable<Foo>>(); // add .ToList()?
foreach(foo in foos) {}
foreach(foo in foos) {}
foreach(foo in foos) {}
var foos = services.GetRequiredService<IEnumerable<Foo>>(); // add .ToList()?
foreach(foo in foos) {}
foreach(foo in foos) {}
foreach(foo in foos) {}
is it better to first get list? My concern is that DI will look for "Foo" multiple times even if it isn't needed
D
David_F512d ago
The main point is that you should assume that it may iterate each time, so if you don't want that, then call .ToArray() on it the first time and cache it
T
Thinker512d ago
You should pretty much always use ToList or ToArray when iterating an IEnumerable multiple times catsip
C
CrosRoad95512d ago
thats a good point
D
David_F512d ago
Depend on the contract, not on the current behavior IEnumerable contract is that it is lazily evaluated
C
CrosRoad95512d ago
i know that if i have something what produces enumerable, and something what consumes it it should not make list or array out of it i imagine it as a some kind of pipelining ( IEnumerable )
A
Anton512d ago
well, thing is, the DI container could already have cached it into an array (if the injection is not transient), so converting it a sceond time is just wasting memory, which is what the question is asking, I would assume need to look into the source code
D
David_F512d ago
@CrosRoad95 Depends on the behavior you want to implement. If your subsequent pipeline processors need fresh data each time they enumerate, then yes, you should not cache it with ToArray()
A
Anton512d ago
but consider that a microoptimization
C
CrosRoad95512d ago
on the other hand, in my case i have very few elements and code that doing loop multiple is executed very rarely
A
Anton512d ago
then you can just do it. it's gonna matter in behavior when there are transient dependencies so do call ToArray that's probably what you want what I mean is that with toarray the dependencies will be only created once, while without it it would create them on every loop lazily
MW
Monsieur Wholesome512d ago
Am I the only one baffled by the idea of storing some IEnumerable<int> in a service container
UU
Unknown User512d ago
A
Accord511d 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
❔ How did you learn . NET ?Books /youtube/udemy /other courses, what resources made you learn .NET to use as your job as web de✅ Is it possible to disable these properties on EF6? Web API 2 project, multi-layered, NF472SCENARIO - Solution type is NET Framework 4.7.2 (it is, what it is). - I have this project, I'm cons✅ This EntityFramework query can not be translated for SQLiteThe issue lies within `Intersect`. The point of the query is to fetch the current user, and to popul✅ Detect Network RequestI want make service that detect all connection to specific domain or ip? And if this request contain❔ CORS errorHello, I have Cors error on my project. I add Addpolicy in my configureServices and also add app.Us❔ Ambiguities? How?Idk how i can resolve this i don't see the problem✅ How can I get rid of this annoying pointer thing in the first row, datagridview winforms^✅ How can I use a row header of a datagridview to display the row number instead of being empty?^✅ How can i properly display this datagridviewHow can i get rid of the row select thing on the left (just white space)✅ I am trying to output a 2D array to a datagridview, but it is not outputting correctlyIt is saying System.Int32[]✅ Task not returning and stopping programHey, I am currently trying to start one task for every string in the list, but this isn't working an❔ DbContextCan i use this in a non-asp area like for example, a simple program to make few tables using DbSet✅ The specified deps.json does not exist while scaffolding DBI am going through a tutorial to create a Web API app with ASP.NET6 and MySql. I wanted to scaffold ❔ C sharp object reference errorI am trying to reference an object that I'm displaying on PageHeader.xaml and I keep getting this erInteroperability issues (PInvoke)Hi! I'm calling a native CPP library and retrieving a struct by reference which returns a struct con❔ SKIA SHARP PROBLEMError: ```cmd Dec 02 16:05:22 mourashop shopmoura-api-app[3847595]: Unhandled exception. System.Argu❔ FluentAssertions excluding collection elementsI have two IEnumerable<Claim> tmp1, tmp2 and I want to see if they are equal, except for the claims ✅ Best way to display a 2D arrayWhat would be the best way to display a 2D array in winforms c#, with the best design, or is just prWrong count for my inversions functionIt is supposed to be 10 inversion count but I am getting 13 ```csharp var array = new int✅ Need some help with classesNot too sure how to correctly use attributes from a class in a class function