Details
-
Improvement
-
Resolution: Works as Designed
-
Major - P3
-
None
-
None
-
None
-
None
Description
Currently the implementation of BsonClassMap stores the type/map against a regular Dictionary. Under most circumstances (and as the documentation briefly touches on), this isn't a problem as you can map classes at the beginning of the program however in some cases, you can only map the class just-in-time.
Is it possible to switch from a regular Dictionary and instead to the ConcurrentDictionary? As far as I can tell, it should be a fairly simple drop-in replacement with really only two lines changed (besides the additional "using" directive):
public class BsonClassMap |
{
|
// private static fields |
private readonly static ConcurrentDictionary<Type, BsonClassMap> __classMaps = new ConcurrentDictionary<Type, BsonClassMap>(); |
...
|
public static void RegisterClassMap(BsonClassMap classMap) |
{
|
if (classMap == null) |
{
|
throw new ArgumentNullException("classMap"); |
}
|
|
|
BsonSerializer.ConfigLock.EnterWriteLock();
|
try |
{
|
// note: class maps can NOT be replaced (because derived classes refer to existing instance) |
__classMaps.TryAdd(classMap.ClassType, classMap);
|
BsonSerializer.RegisterDiscriminator(classMap.ClassType, classMap.Discriminator);
|
}
|
finally |
{
|
BsonSerializer.ConfigLock.ExitWriteLock();
|
}
|
}
|
Based on the comment in the code, I think a TryAdd is our only option as an AddOrUpdate will likely cause issues.
I've marked this as an improvement more than a bug as under most use cases, it isn't an issue.