Skip to content

Commit

Permalink
Merge pull request MapsterMapper#311 from cmann-andagon/nested-async-fix
Browse files Browse the repository at this point in the history
Fix issue regarding nested use of AdaptToTypeAsync
  • Loading branch information
chaowlert committed Feb 24, 2021
2 parents 354605d + 7321d50 commit 0edd363
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Mapster.Core/MapContext/MapContextScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,31 @@ public class MapContextScope : IDisposable
{
public MapContext Context { get; }

private readonly bool _isRootScope;
private readonly MapContext? _oldContext;

public MapContextScope()
{
var context = MapContext.Current;
if (context == null)
_oldContext = MapContext.Current;

this.Context = new MapContext();
if (_oldContext != null)
{
_isRootScope = true;
MapContext.Current = context = new MapContext();
foreach (var parameter in _oldContext.Parameters)
{
this.Context.Parameters[parameter.Key] = parameter.Value;
}
foreach (var reference in _oldContext.References)
{
this.Context.References[reference.Key] = reference.Value;
}
}
this.Context = context;

MapContext.Current = this.Context;
}

public void Dispose()
{
if (_isRootScope && ReferenceEquals(MapContext.Current, this.Context))
MapContext.Current = null;
MapContext.Current = _oldContext;
}

public static TResult GetOrAddMapReference<TResult>(ReferenceTuple key, Func<ReferenceTuple, TResult> mapFn) where TResult : notnull
Expand Down

0 comments on commit 0edd363

Please sign in to comment.