| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System.Threading;
- using Abp.Dependency;
- using Castle.Core;
- using Castle.Core.Logging;
- namespace Abp.Domain.Uow
- {
- /// <summary>
- /// CallContext implementation of <see cref="ICurrentUnitOfWorkProvider"/>.
- /// This is the default implementation.
- /// </summary>
- public class AsyncLocalCurrentUnitOfWorkProvider : ICurrentUnitOfWorkProvider, ITransientDependency
- {
- /// <inheritdoc />
- [DoNotWire]
- public IUnitOfWork Current
- {
- get { return GetCurrentUow(); }
- set { SetCurrentUow(value); }
- }
- public ILogger Logger { get; set; }
- private static readonly AsyncLocal<LocalUowWrapper> AsyncLocalUow = new AsyncLocal<LocalUowWrapper>();
- public AsyncLocalCurrentUnitOfWorkProvider()
- {
- Logger = NullLogger.Instance;
- }
- private static IUnitOfWork GetCurrentUow()
- {
- var uow = AsyncLocalUow.Value?.UnitOfWork;
- if (uow == null)
- {
- return null;
- }
- if (uow.IsDisposed)
- {
- AsyncLocalUow.Value = null;
- return null;
- }
- return uow;
- }
- private static void SetCurrentUow(IUnitOfWork value)
- {
- lock (AsyncLocalUow)
- {
- if (value == null)
- {
- if (AsyncLocalUow.Value == null)
- {
- return;
- }
- if (AsyncLocalUow.Value.UnitOfWork?.Outer == null)
- {
- AsyncLocalUow.Value.UnitOfWork = null;
- AsyncLocalUow.Value = null;
- return;
- }
- AsyncLocalUow.Value.UnitOfWork = AsyncLocalUow.Value.UnitOfWork.Outer;
- }
- else
- {
- if (AsyncLocalUow.Value?.UnitOfWork == null)
- {
- if (AsyncLocalUow.Value != null)
- {
- AsyncLocalUow.Value.UnitOfWork = value;
- }
- AsyncLocalUow.Value = new LocalUowWrapper(value);
- return;
- }
- value.Outer = AsyncLocalUow.Value.UnitOfWork;
- AsyncLocalUow.Value.UnitOfWork = value;
- }
- }
- }
- private class LocalUowWrapper
- {
- public IUnitOfWork UnitOfWork { get; set; }
- public LocalUowWrapper(IUnitOfWork unitOfWork)
- {
- UnitOfWork = unitOfWork;
- }
- }
- }
- }
|