-1

Is there a best practice for iterations inside of a "using"? which is better? and maybe a why?

"using" inside of an iteration...

foreach (var currentPerson in Persons)
{
    using (var db = new SolutionModel())
    {
        //TODO: run query
    }
}

or is it better to do an iteration inside of a "using"...

using (var db = new SolutionModel())
{
    foreach (var currentPerson in Persons)
    {        
        //TODO: run query
    }
}

FYI: coding in C#

Hakubex
  • 47
  • 6
  • You probably don't want to open a new connection for each person. What you probably want is to open a connection, process each person on that connection, and then close the connection. But your chosen strategy really depends on what you're trying to accomplish. – Robert Harvey Apr 03 '18 at 18:46
  • Would you please care at least about letting us know which programming language you are using for your code? – πάντα ῥεῖ Apr 03 '18 at 19:21
  • coding in C# using .NET and Entity Frameworks – Hakubex Apr 03 '18 at 19:34
  • Another option that may or may not suite your situation is to perform just a single large query across all persons. – Erik Eidt Apr 03 '18 at 21:03
  • 1
    Possible duplicate of [How would you know if you've written readable and easily maintainable code?](https://softwareengineering.stackexchange.com/questions/141005/how-would-you-know-if-youve-written-readable-and-easily-maintainable-code) – gnat Apr 03 '18 at 21:29
  • The second one looks a lot quicker and cheaper but you should really try this on a large (database) set, time it and look at resource use. – Martin Maat Apr 04 '18 at 05:20
  • so... I came across [this](https://weblog.west-wind.com/posts/2014/Dec/21/Gotcha-Entity-Framework-gets-slow-in-long-Iteration-Loops) earlier today. Seems like sample 2 would be the norm but when it comes to Entity Framework its better to do sample 1. only way to truly find out is testing it on a large iteration of objects. I'm just not sure how large of an iteration I will come across in this specific solution. – Hakubex Apr 04 '18 at 17:26
  • 1
    @Hakubex: you missed the point. As David Arno told you, your two examples have a certain risk of not not behaving similar (for example, in when thinking about transactions and failures, and also when thinking about performance). So this is not a matter of "style" or "norm" or "best practice", it is a matter of correctness and required performance. – Doc Brown Apr 11 '18 at 11:04

1 Answers1

3

In the first case, a new SolutionModel will be created, run querywill be called once and then Dispose() will be called on it, for every item in Persons.

In the second case, just one SolutionModel will be created and run query will be called multiple times on that one instance before it's disposed..

So there is no "best practice" at play here. You need to decide which is the correct action based on what you want to happen as Persons is enumerated.

David Arno
  • 38,972
  • 9
  • 88
  • 121
  • You obviously shouldn't create `SolutionModel` for each and every person *if it is not necessary*. However I agree completely that the person who decides this is the OP. – Neil Apr 04 '18 at 09:54