Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2131

Concurrency for BsonClassMap registration

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Icon: Major - P3 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.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            james@turnersoftware.com.au James Turner
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: