From b4b88b09ae4b35b9b6a1b4f6dfc7b8791cf1dec4 Mon Sep 17 00:00:00 2001 From: Sangeeth Nandakumar Date: Sun, 21 Jan 2024 15:43:41 +0530 Subject: [PATCH] Add support for singleton based LiteDB implementation --- Twileloop.UOW.LiteDB/Support/Extensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Twileloop.UOW.LiteDB/Support/Extensions.cs b/Twileloop.UOW.LiteDB/Support/Extensions.cs index fd3da9d..3ac0c71 100644 --- a/Twileloop.UOW.LiteDB/Support/Extensions.cs +++ b/Twileloop.UOW.LiteDB/Support/Extensions.cs @@ -9,6 +9,22 @@ namespace Twileloop.UOW.LiteDB.Support public static class Extensions { + public static void AddSingletonUnitOfWork(this IServiceCollection services, Action? options = null) + { + var uowOptions = new Options(); + options?.Invoke(uowOptions); + + var contexts = new ConcurrentDictionary(); + foreach (var conn in uowOptions.Connections) + { + var db = new LiteDatabase(conn.ConnectionString); + contexts.TryAdd(conn.Name, new LiteDBContext(conn.Name, db)); + } + + services.AddSingleton(contexts); + services.AddSingleton(); + } + public static void AddUnitOfWork(this IServiceCollection services, Action? options = null) { var uowOptions = new Options();