Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Protect AutoMappingStore from Threading issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Aug 30, 2010
1 parent 20fa57e commit f662081
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions source/MongoDB/Configuration/Mapping/AutoMappingStore.cs
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using MongoDB.Configuration.Mapping.Auto; using MongoDB.Configuration.Mapping.Auto;
using MongoDB.Configuration.Mapping.Model; using MongoDB.Configuration.Mapping.Model;


Expand All @@ -11,6 +12,7 @@ public class AutoMappingStore : IMappingStore
{ {
private readonly IAutoMapper _autoMapper; private readonly IAutoMapper _autoMapper;
private readonly Dictionary<Type, IClassMap> _autoMaps; private readonly Dictionary<Type, IClassMap> _autoMaps;
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly IMappingStore _wrappedMappingStore; private readonly IMappingStore _wrappedMappingStore;


/// <summary> /// <summary>
Expand Down Expand Up @@ -71,23 +73,42 @@ public AutoMappingStore(IAutoMapper autoMapper, IMappingStore mappingStore)
/// <returns></returns> /// <returns></returns>
public IClassMap GetClassMap(Type classType) public IClassMap GetClassMap(Type classType)
{ {
IClassMap classMap; try

if(_autoMaps.TryGetValue(classType, out classMap))
return classMap;

if(_wrappedMappingStore != null)
{ {
classMap = _wrappedMappingStore.GetClassMap(classType); _lock.EnterUpgradeableReadLock();
if(classMap != null)
IClassMap classMap;
if(_autoMaps.TryGetValue(classType, out classMap))
return classMap; return classMap;
}


classMap = _autoMapper.CreateClassMap(classType, GetClassMap); if(_wrappedMappingStore != null)
{
classMap = _wrappedMappingStore.GetClassMap(classType);
if(classMap != null)
return classMap;
}

classMap = _autoMapper.CreateClassMap(classType, GetClassMap);


_autoMaps.Add(classType, classMap); try
{
_lock.EnterWriteLock();


return classMap; _autoMaps.Add(classType, classMap);

return classMap;
}
finally
{
if(_lock.IsWriteLockHeld)
_lock.ExitWriteLock();
}
}
finally
{
if(_lock.IsUpgradeableReadLockHeld)
_lock.ExitUpgradeableReadLock();
}
} }
} }
} }

0 comments on commit f662081

Please sign in to comment.