A Use Case of Immutable Collections

Set and Remove Cache

Let’s assume that we have a collection of Parking Lots that are being frequently queried by LotCodes to get LotNames. The list of Parking Lots won’t change often, which makes sense to store the list of data in memory.

In Startup.cs, we need to register the MemoryCache and desired services. Below is an example case that we define a LotNamesCache to retrieve and manage in memory data.

services.AddMemoryCache();
services.AddScoped<ILotNamesCache, LotNamesCache>();

The actual implementation of LotNamesCache is shown as below. We store the <LotCode, LotName> key value pairs as an immutable dictionary. A reference of using System.Collections.Immutable; is needed in order to use the ImmutableDictionary type and the ToImmutableDictionary() method.

With the Immutable Dictionary, we are not able to mess up the original copy of key value pairs.

When you manipulate an immutable dictionary a copy of the original dictionary is made, manipulations applied and a new immutable dictionary is returned.

— .Net Docs

There are other collection types in the System.Collections.Immutable Namespace, such as ImmutableArray, ImmutableList, ImmutableHashSet, and so on. We should utilize them as much as we can when dealing with immutable objects.

Unit Tests

The code snippet below shows a simple unit test for the LotNamesCache class. The unit test utilizes an InMemoryDatabase to simulate data persisted in some kind of storage.

Please note lines 26–27, even though the cachedLots adds a new key value pair, the original cachedLots doesn’t retain the new key value pair. This effect is exactly what we want, so that the cached values are consistent and can only be modified when the underlying data source has changes.