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

Generic ICustomBsonTypeMapper with support for inheritance

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.3
    • Component/s: BSON, Serialization
    • Labels:
      None

      Hello!

      Currently, there is no driver-supported way to convert a generic type to a BsonValue in a custom way, because BsonTypeMapper.RegisterCustomTypeMapper BsonTypeMapper.TryMapToBsonValue uses a specific type. I would like to propose the ability to register custom type mappers by their corresponding types' generic template definition (in a separate dictionary) and making a two-pass check, first using a concrete type, and second – a type template.

      Also, going up the inheritance chain when testing might be beneficial to allow for proper base class-related implementations. This of course will incur a performance hit, but considering that custom mapping is done right before failing out, I consider the impact of this possible performance being low.

      This is going to help in situations like ours, where we have many primitive-based domain types, that inherit from a certain generic base class, but have the same basic inner representation. While automatic serialization via IBsonSerializers is wonderful, sometimes manual BsonDocument manipulation is warranted, and in this case we have to resort to extension methods that aid with BsonValue conversion, but this solution is clumsy and error-prone.

      The generic template change is fully backwards compatible (if you don't take into account non-previously-working generic template registrations now working).
      The inheritance change is not backwards compatible, but I do not believe that the scope of the change will have a significant impact on real existing code.

            Assignee:
            Unassigned Unassigned
            Reporter:
            onyxmaster Aristarkh Zagorodnikov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: