[CSHARP-2238] Make BsonSerializerRegistry.RegisterSerializer idempotent Created: 13/Apr/18  Updated: 30/Jan/19  Resolved: 30/Jan/19

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Josh Yaxley [X] Assignee: Vincent Kam (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB Driver 2.5


Backwards Compatibility: Fully Compatible

 Description   

Right now, if I register the same BsonSerializer twice, I'll get an exception saying that a BsonSerializer has already been registered for that type.

I'm running my tests using XUnit, and a few of them pass through the part of my project that registers a couple of custom serializers. Even though each only passes through this code once, when ran together, the static nature of BsonSerializer somehow causes them to interfere with each other, throwing the "There is already a serializer registered for type ..." exception (thrown here).

It seems that the API would be more developer friendly if this method would allow the same serializer to be registered twice. A potential fix would be to, if failing to add it to the dictionary, check if it already exists in the dictionary, and, if so, do nothing (instead of throwing).

I'm happy to do a pull request if necessary.



 Comments   
Comment by Vincent Kam (Inactive) [ 30/Jan/19 ]

Hi Yax,

Thank you for your ticket. I'm sorry to hear about your testing frustrations. You are of course correct that attempting to register a serializer twice for a given type results in an exception being thrown, and it would be nice if we could somehow detect when a given serializer is identical to an already registered serializer. However, unfortunately, determining whether two serializers are equal is non-trivial, except in the case where they happen to be the same object.  For now, I can only suggest that you refactor the project's code so that the serializer is only registered once, perhaps by using a static constructor, a lazily initialized variable, or simply using a booelan to track whether a serializer for a given type has already been registered.

 

Kind regards,

Vincent

 

Generated at Wed Feb 07 21:41:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.