New ReferenceConverter method in Commerce 13.21
As you might have heard - ReferenceConverter is your friend. It is very lightweight way to map between the ContentReference and code of catalog content, instead of having to load the content - which is both slow and expensive. Whenever you have chance, ReferenceConverter should be used.
But it is now even better. In Commerce 13.21, ReferenceConverter has a new method to get multiple codes at once. It looks like this
public virtual IDictionary<ContentReference, string> GetCodes(IEnumerable<ContentReference> contentLinks, CatalogContentType catalogContentType)
A common scenario for this new method is when you need to get all variants of a product (by using IRelationRepository), then get all prices or inventories for those variants. Instead of
var allVariantCodes = allVariantLinks.Select(x => _referenceConverter.GetCode(x));
You can now call
var allVariantCodes = _referenceConverter.GetCodes(allVariantLinks, CatalogContentType.CatalogEntry).Values;
The former can result in multiple database roundtrips - depending on how many variants you have. The latter always results in at most 1 database roundtrip. As we all know each roundtrip adds overhead, so there will be a performance improvement.