Review generated product catalog

from Collections
C# 14 / .NET 10 advanced 6 min 5 issues to find

Review this generated product catalog before it is shared by request handlers.

Build a case-insensitive catalog for caller-supplied SKUs, cap it at 1,000 entries, load each logical SKU at most once under concurrency, and return a stable immutable snapshot sorted by SKU.

csharp
using System.Collections.Generic;

public sealed record Product(string Sku, string Name);

public sealed class ProductCatalog
{
    private readonly Dictionary<string, Product> _bySku = new();

    public Product Get(string sku)
    {
        if (!_bySku.ContainsKey(sku))
        {
            _bySku[sku] = LoadProduct(sku);
        }
        return _bySku[sku];
    }

    public IReadOnlyCollection<Product> Snapshot() => _bySku.Values;

    private static Product LoadProduct(string sku) => new(sku, "loaded");
}

generated code is illustrative, not from any one model

Open in playground
Report an error