Skip to content

Commit 183d72f

Browse files
committed
dictionary extensions added
1 parent b705de4 commit 183d72f

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This package currently supports:
2626
- **SignalR Extensions** for adding simple SignalR or distributed SignalR backed with Redis.
2727
- **OpenTelemetry**: Metrics, traces, and logs with Prometheus support.
2828
- **Health Checks**: Startup validation and endpoints for monitoring.
29-
- Various **Extensions and Utilities**, including enumerable, string, and queryable extensions.
29+
- Various **Extensions and Utilities**, including enumerable, string, dictionary and queryable extensions.
3030

3131
## Prerequisites
3232

@@ -613,6 +613,8 @@ This package includes various extensions and utilities to aid development:
613613
- **Enumerable Extensions:** Additional LINQ methods for collections.
614614
- **Host Environment Extensions:** Methods to simplify environment checks (e.g., `IsLocal()`, `IsQa()`).
615615
- **Queryable Extensions:** Extensions for IQueryable, such as conditional `WhereIf`.
616+
- **Dictionary Extensions:** Utility methods for dictionary manipulation in a performant way like `GetOrAdd` and
617+
`TryUpdate`.
616618
- **String Extensions:** Utility methods for string manipulation.
617619
- **Time Zone Extensions:** Methods to handle default time zones within your application. Use `.MapDefaultTimeZone()`,
618620
which
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
4+
namespace SharedKernel.Extensions;
5+
6+
public static class DictionaryExtensions
7+
{
8+
public static TValue? GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue? value)
9+
where TKey : notnull
10+
{
11+
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out var exists);
12+
13+
if (exists)
14+
{
15+
return val;
16+
}
17+
18+
val = value;
19+
return val;
20+
}
21+
22+
public static bool TryUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue value)
23+
where TKey : notnull
24+
{
25+
ref var val = ref CollectionsMarshal.GetValueRefOrNullRef(dict, key);
26+
if (Unsafe.IsNullRef(ref val))
27+
{
28+
return false;
29+
}
30+
31+
val = value;
32+
return true;
33+
}
34+
}

src/SharedKernel/SharedKernel.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.1.1</Version>
11+
<Version>1.1.2</Version>
1212
<PackageId>Pandatech.SharedKernel</PackageId>
1313
<Title>Pandatech Shared Kernel Library</Title>
1414
<PackageTags>Pandatech, shared kernel, library, OpenAPI, Swagger, utilities, scalar</PackageTags>
1515
<Description>Pandatech.SharedKernel provides centralized configurations, utilities, and extensions for ASP.NET Core projects. For more information refere to readme.md document.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel</RepositoryUrl>
17-
<PackageReleaseNotes>Added signal r incomming and outgoing message logging</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added dictinory extensions</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)