Skip to content

Commit 24efb6e

Browse files
authored
Merge pull request #33 from jonsagara/feature/emptydict
Added an EmptyDictionary class. Upgraded SE.Redis and NodaTime.
2 parents fd02a01 + 7fe264d commit 24efb6e

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<LangVersion>13.0</LangVersion>
88

99
<!-- NuGet -->
10-
<Version>3.1.5</Version>
10+
<Version>3.1.6</Version>
1111
<AssemblyVersion>3.1.0</AssemblyVersion>
1212
<FileVersion>3.1.0</FileVersion>
1313
<Authors>Jon Sagara</Authors>

src/Sagara.Core.Caching/Sagara.Core.Caching.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</ItemGroup>
3838

3939
<ItemGroup>
40-
<PackageReference Include="StackExchange.Redis" Version="2.8.22" />
40+
<PackageReference Include="StackExchange.Redis" Version="2.8.24" />
4141
</ItemGroup>
4242

4343
<ItemGroup>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections;
2+
using System.Collections.ObjectModel;
3+
using System.Diagnostics.CodeAnalysis;
4+
5+
namespace Sagara.Core.Collections;
6+
7+
/// <summary>
8+
/// Provides a cached, read-only instance for the specified key and value types.
9+
/// </summary>
10+
/// <remarks>
11+
/// <para>We're implementing <see cref="IReadOnlyDictionary{TKey, TValue}"/> to get a true read-only dictionary without
12+
/// risking various extension methods leading to runtime exceptions. For example, <see cref="ReadOnlyDictionary{TKey, TValue}"/>
13+
/// implements <see cref="IDictionary{TKey, TValue}"/>, which in turn lights up the <see cref="CollectionExtensions.TryAdd{TKey, TValue}(IDictionary{TKey, TValue}, TKey, TValue)"/>
14+
/// extension method that will throw at runtime if called on a <see cref="ReadOnlyDictionary{TKey, TValue}"/>.</para>
15+
/// </remarks>
16+
/// <typeparam name="TKey">The type of keys in the read-only dictionary.</typeparam>
17+
/// <typeparam name="TValue">The type of values in the read-only dictionary.</typeparam>
18+
public sealed class EmptyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
19+
where TKey : notnull
20+
{
21+
/// <summary>
22+
/// The one and only instance of <see cref="EmptyDictionary{TKey, TValue}"/> for this key/value type pair.
23+
/// </summary>
24+
public static readonly IReadOnlyDictionary<TKey, TValue> Instance = ReadOnlyDictionary<TKey, TValue>.Empty;
25+
26+
/// <summary>
27+
/// Don't let callers create their own instances.
28+
/// </summary>
29+
private EmptyDictionary()
30+
{
31+
// Intentionally blank.
32+
}
33+
34+
/// <inheritdoc/>
35+
public TValue this[TKey key]
36+
=> Instance[key];
37+
38+
/// <inheritdoc/>
39+
public IEnumerable<TKey> Keys
40+
=> Instance.Keys;
41+
42+
/// <inheritdoc/>
43+
public IEnumerable<TValue> Values
44+
=> Instance.Values;
45+
46+
/// <inheritdoc/>
47+
public int Count
48+
=> Instance.Count;
49+
50+
/// <inheritdoc/>
51+
public bool ContainsKey(TKey key)
52+
=> Instance.ContainsKey(key);
53+
54+
/// <inheritdoc/>
55+
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
56+
=> Instance.GetEnumerator();
57+
58+
/// <inheritdoc/>
59+
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
60+
=> Instance.TryGetValue(key, out value);
61+
62+
/// <inheritdoc/>
63+
IEnumerator IEnumerable.GetEnumerator()
64+
=> ((IEnumerable)Instance).GetEnumerator();
65+
}

src/Sagara.Core/Sagara.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<ItemGroup>
3131
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
32-
<PackageReference Include="NodaTime" Version="3.2.0" />
32+
<PackageReference Include="NodaTime" Version="3.2.1" />
3333
</ItemGroup>
3434

3535
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">

0 commit comments

Comments
 (0)