Skip to content

inefficient code example #31983

Closed
Closed

Description

@DickBaker

in "Encapsulating data" section at https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/work-with-data-in-asp-net-core-apps#encapsulating-data the code sample is inefficient since the update case would require TWO scans of the Items collection, so IMHO better to code the
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
and then test that nullable object
if(existingItem==null){_items.Add(new BasketItem{..}} else existingItem.Quantity+=quantity;
so eliminating 2nd scan, the Any() and the return statement.

Example used here of in-memory database perhaps not too bad, but devs should realise that multiple scans of a real remote relational db could incur 2 roundtrips. doh!

Actually I also quarrel with using the public Items wrapper rather than the private underlying _items List
surely this code should manipulate its private List to avoid List BCL having to brew-up a wrapper [again]?


Document Details

⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Activity

  1. added a commit that references this issue on Nov 3, 2022
    2e87a17
  2. ghost added
    in-pr
    on Nov 3, 2022
  3. added a commit that references this issue on Nov 4, 2022
    99f1b02
  4. added
    ⌚ Not Triaged
    and removed
    ⌚ Not Triaged
    on Nov 4, 2022
  5. ghost removed
    in-pr
    on Nov 4, 2022
  6. added a commit that references this issue on Nov 4, 2022
    61e97e7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions