This repository was archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from stoveproject/dev
dev to master
- Loading branch information
Showing
8 changed files
with
353 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Stove/Domain/Uow/CallContextDrivenAsyncLocalCurrentUnitOfWorkProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections.Concurrent; | ||
|
||
using Autofac.Extras.IocManager; | ||
|
||
using Stove.Log; | ||
using Stove.Threading; | ||
|
||
namespace Stove.Domain.Uow | ||
{ | ||
public class CallContextDrivenAsyncLocalCurrentUnitOfWorkProvider : ICurrentUnitOfWorkProvider, ITransientDependency | ||
{ | ||
private const string ContextKey = "Stove.UnitOfWork.Current"; | ||
private static readonly ConcurrentDictionary<string, IUnitOfWork> unitOfWorkDictionary = new ConcurrentDictionary<string, IUnitOfWork>(); | ||
|
||
public CallContextDrivenAsyncLocalCurrentUnitOfWorkProvider() | ||
{ | ||
Logger = NullLogger.Instance; | ||
} | ||
|
||
public ILogger Logger { get; set; } | ||
|
||
[DoNotInject] | ||
public IUnitOfWork Current | ||
{ | ||
get => GetCurrentUow(Logger); | ||
set => SetCurrentUow(value, Logger); | ||
} | ||
|
||
private static IUnitOfWork GetCurrentUow(ILogger logger) | ||
{ | ||
if (!(CallContext.GetData(ContextKey) is string unitOfWorkKey)) | ||
{ | ||
return null; | ||
} | ||
|
||
if (!unitOfWorkDictionary.TryGetValue(unitOfWorkKey, out IUnitOfWork unitOfWork)) | ||
{ | ||
CallContext.SetData(ContextKey, null); | ||
return null; | ||
} | ||
|
||
if (unitOfWork.IsDisposed) | ||
{ | ||
logger.Warn("There is a unitOfWorkKey in CallContext but the UOW was disposed!"); | ||
unitOfWorkDictionary.TryRemove(unitOfWorkKey, out unitOfWork); | ||
CallContext.SetData(ContextKey, null); | ||
return null; | ||
} | ||
|
||
return unitOfWork; | ||
} | ||
|
||
private static void SetCurrentUow(IUnitOfWork value, ILogger logger) | ||
{ | ||
if (value == null) | ||
{ | ||
ExitFromCurrentUowScope(logger); | ||
return; | ||
} | ||
|
||
if (CallContext.GetData(ContextKey) is string unitOfWorkKey) | ||
{ | ||
if (unitOfWorkDictionary.TryGetValue(unitOfWorkKey, out IUnitOfWork outer)) | ||
{ | ||
if (outer == value) | ||
{ | ||
logger.Warn("Setting the same UOW to the CallContext, no need to set again!"); | ||
return; | ||
} | ||
|
||
value.Outer = outer; | ||
} | ||
} | ||
|
||
unitOfWorkKey = value.Id; | ||
if (!unitOfWorkDictionary.TryAdd(unitOfWorkKey, value)) | ||
{ | ||
throw new StoveException("Can not set unit of work! UnitOfWorkDictionary.TryAdd returns false!"); | ||
} | ||
|
||
CallContext.SetData(ContextKey, unitOfWorkKey); | ||
} | ||
|
||
private static void ExitFromCurrentUowScope(ILogger logger) | ||
{ | ||
if (!(CallContext.GetData(ContextKey) is string unitOfWorkKey)) | ||
{ | ||
logger.Warn("There is no current UOW to exit!"); | ||
return; | ||
} | ||
|
||
if (!unitOfWorkDictionary.TryGetValue(unitOfWorkKey, out IUnitOfWork unitOfWork)) | ||
{ | ||
CallContext.SetData(ContextKey, null); | ||
return; | ||
} | ||
|
||
unitOfWorkDictionary.TryRemove(unitOfWorkKey, out unitOfWork); | ||
if (unitOfWork.Outer == null) | ||
{ | ||
CallContext.SetData(ContextKey, null); | ||
return; | ||
} | ||
|
||
//Restore outer UOW | ||
string outerUnitOfWorkKey = unitOfWork.Outer.Id; | ||
if (!unitOfWorkDictionary.TryGetValue(outerUnitOfWorkKey, out unitOfWork)) | ||
{ | ||
//No outer UOW | ||
logger.Warn("Outer UOW key could not found in UnitOfWorkDictionary!"); | ||
CallContext.SetData(ContextKey, null); | ||
return; | ||
} | ||
|
||
CallContext.SetData(ContextKey, outerUnitOfWorkKey); | ||
} | ||
} | ||
} |
Oops, something went wrong.