From 0b7ed671b3153d5f6b18bfabada3fdde28c6fc71 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Wed, 27 Oct 2021 16:03:15 +0200 Subject: [PATCH] start context scoping --- zero.Core/ZeroContext.cs | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/zero.Core/ZeroContext.cs b/zero.Core/ZeroContext.cs index 93d5ef3b..05f3053e 100644 --- a/zero.Core/ZeroContext.cs +++ b/zero.Core/ZeroContext.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Raven.Client.Documents.Session; +using System; using System.Collections.Concurrent; using System.Security.Claims; using System.Threading.Tasks; @@ -125,8 +126,15 @@ namespace zero.Core public void Override(Application app) { Application = app; - AppId = app.Id; - Store.ResolvedDatabase = Application.Database; + AppId = app?.Id; + Store.ResolvedDatabase = app?.Database; + } + + + /// + public ZeroContextScope CreateScope(Application app) + { + return new(this, app); } @@ -143,6 +151,29 @@ namespace zero.Core } + + public class ZeroContextScope : IDisposable + { + public IZeroContext Context { get; } + + Application _originalApp = null; + + public ZeroContextScope(IZeroContext context, Application app) + { + Context = context; + _originalApp = app; + Context.Override(app); + } + + /// + public void Dispose() + { + Context.Override(_originalApp); + } + } + + + public interface IZeroContext { /// @@ -201,6 +232,11 @@ namespace zero.Core /// void Override(Application app); + /// + /// SCOPE + /// + ZeroContextScope CreateScope(Application app); + /// /// Get a custom property from this scoped context ///