Use ConcurrentDictionary specific methods

This commit is contained in:
Stephan
2019-04-17 14:52:38 +02:00
parent 6cb55789bd
commit 791581f502
2 changed files with 7 additions and 8 deletions

View File

@@ -107,16 +107,12 @@ namespace Umbraco.Core.Mapping
private Dictionary<Type, Func<object, MapperContext, object>> DefineCtors(Type sourceType)
{
if (!_ctors.TryGetValue(sourceType, out var sourceCtor))
sourceCtor = _ctors[sourceType] = new Dictionary<Type, Func<object, MapperContext, object>>();
return sourceCtor;
return _ctors.GetOrAdd(sourceType, _ => new Dictionary<Type, Func<object, MapperContext, object>>());
}
private Dictionary<Type, Action<object, object, MapperContext>> DefineMaps(Type sourceType)
{
if (!_maps.TryGetValue(sourceType, out var sourceMap))
sourceMap = _maps[sourceType] = new Dictionary<Type, Action<object, object, MapperContext>>();
return sourceMap;
return _maps.GetOrAdd(sourceType, _ => new Dictionary<Type, Action<object, object, MapperContext>>());
}
#endregion
@@ -332,6 +328,8 @@ namespace Umbraco.Core.Mapping
if (_ctors.TryGetValue(sourceType, out var sourceCtor) && sourceCtor.TryGetValue(targetType, out var ctor))
return ctor;
// we *may* run this more than once but it does not matter
ctor = null;
foreach (var (stype, sctors) in _ctors)
{
@@ -353,6 +351,8 @@ namespace Umbraco.Core.Mapping
if (_maps.TryGetValue(sourceType, out var sourceMap) && sourceMap.TryGetValue(targetType, out var map))
return map;
// we *may* run this more than once but it does not matter
map = null;
foreach (var (stype, smap) in _maps)
{

View File

@@ -136,6 +136,7 @@ namespace Umbraco.Tests.Mapping
void ThreadLoop()
{
// keep failing at mapping - and looping through the maps
for (var i = 0; i < 10; i++)
{
try
@@ -169,8 +170,6 @@ namespace Umbraco.Tests.Mapping
{
thread.Join();
}
Assert.IsNull(caught);
}
private class Thing1