{"id":24271705,"url":"https://github.com/soenneker/soenneker.utils.singletondictionary","last_synced_at":"2026-01-08T17:20:55.166Z","repository":{"id":152068818,"uuid":"612798867","full_name":"soenneker/soenneker.utils.singletondictionary","owner":"soenneker","description":"An externally initializing singleton dictionary that uses double-check asynchronous locking, with optional async and sync disposal","archived":false,"fork":false,"pushed_at":"2024-10-29T03:43:15.000Z","size":667,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-30T03:37:43.927Z","etag":null,"topics":["async","csharp","dictionary","dotnet","lock","singleton","sync"],"latest_commit_sha":null,"homepage":"https://soenneker.com","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soenneker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-12T02:08:37.000Z","updated_at":"2024-10-29T03:34:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"f84af446-4fb7-41f5-a79b-80f8adc01737","html_url":"https://github.com/soenneker/soenneker.utils.singletondictionary","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.singletondictionary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.singletondictionary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.singletondictionary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fsoenneker.utils.singletondictionary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soenneker","download_url":"https://codeload.github.com/soenneker/soenneker.utils.singletondictionary/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234080110,"owners_count":18776392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","csharp","dictionary","dotnet","lock","singleton","sync"],"created_at":"2025-01-15T17:59:20.000Z","updated_at":"2026-01-02T21:10:21.013Z","avatar_url":"https://github.com/soenneker.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿[![](https://img.shields.io/nuget/v/Soenneker.Utils.SingletonDictionary.svg?style=for-the-badge)](https://www.nuget.org/packages/Soenneker.Utils.SingletonDictionary/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/soenneker.utils.singletondictionary/publish-package.yml?style=for-the-badge)](https://github.com/soenneker/soenneker.utils.singletondictionary/actions/workflows/publish-package.yml)\n[![](https://img.shields.io/nuget/dt/Soenneker.Utils.SingletonDictionary.svg?style=for-the-badge)](https://www.nuget.org/packages/Soenneker.Utils.SingletonDictionary/)\n\n# ![](https://user-images.githubusercontent.com/4441470/224455560-91ed3ee7-f510-4041-a8d2-3fc093025112.png) Soenneker.Utils.SingletonDictionary\n\n### A flexible singleton dictionary with double-check async locking, sync/async disposal, and external initialization\n\n---\n\n## Features\n\n* ✅ Async and sync initialization patterns\n* ✅ Optional cancellation support\n* ✅ Async and sync access methods\n* ✅ Fully disposable (sync and async)\n* ✅ Thread-safe with `AsyncLock` from [Nito.AsyncEx](https://github.com/StephenCleary/AsyncEx)\n\n---\n\n## Installation\n\n```bash\ndotnet add package Soenneker.Utils.SingletonDictionary\n```\n\n---\n\n## ✨ Example Usage\n\nHere’s an example using `SingletonDictionary` to manage singleton `HttpClient` instances keyed by configuration (e.g. timeout):\n\n```csharp\npublic class HttpRequester : IDisposable, IAsyncDisposable\n{\n    private readonly SingletonDictionary\u003cHttpClient\u003e _clients;\n\n    public HttpRequester()\n    {\n        _clients = new SingletonDictionary\u003cHttpClient\u003e((args) =\u003e\n        {\n            var socketsHandler = new SocketsHttpHandler\n            {\n                PooledConnectionLifetime = TimeSpan.FromMinutes(10),\n                MaxConnectionsPerServer = 10\n            };\n\n            var client = new HttpClient(socketsHandler)\n            {\n                Timeout = TimeSpan.FromSeconds((int)args[0])\n            };\n\n            return client;\n        });\n    }\n\n    public async ValueTask Get()\n    {\n        var client = await _clients.Get(\"100\", 100);\n        await client.GetAsync(\"https://google.com\");\n    }\n\n    public async ValueTask DisposeAsync()\n    {\n        GC.SuppressFinalize(this);\n        await _clients.DisposeAsync();\n    }\n\n    public void Dispose()\n    {\n        GC.SuppressFinalize(this);\n        _clients.Dispose();\n    }\n}\n```\n\n---\n\n## 🔍 Internals\n\n`SingletonDictionary\u003cT\u003e` is backed by a `ConcurrentDictionary\u003cstring, T\u003e` and supports:\n\n* Multiple constructor overloads for async/sync factory functions\n* Internal double-checked locking on access\n* Deferred/lazy factory execution\n* Proper disposal of values (both sync and async interfaces)\n* Safe mutation via `SetInitialization` before first use\n\nExample constructor overloads include:\n\n```csharp\nnew SingletonDictionary\u003cT\u003e(Func\u003cstring, object[], ValueTask\u003cT\u003e\u003e factory);\nnew SingletonDictionary\u003cT\u003e(Func\u003cobject[], T\u003e factory);\nnew SingletonDictionary\u003cT\u003e(Func\u003cstring, CancellationToken, object[], ValueTask\u003cT\u003e\u003e factory);\n// And more...\n```\n\nYou can also initialize manually:\n\n```csharp\nvar dict = new SingletonDictionary\u003cMyService\u003e();\ndict.SetInitialization((args) =\u003e new MyService(args));\n```\n\n---\n\n## 🛡️ Thread Safety\n\nThis library uses [`AsyncLock`](https://github.com/StephenCleary/AsyncEx) for safe concurrent access in async contexts, and synchronously via `Lock()` for blocking methods. This avoids race conditions and guarantees safe singleton creation.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.utils.singletondictionary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoenneker%2Fsoenneker.utils.singletondictionary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fsoenneker.utils.singletondictionary/lists"}