[CSHARP-4487] Make BsonClassMap entirely non-static Created: 24/Jan/23  Updated: 09/Jan/24

Status: Backlog
Project: C# Driver
Component/s: Configuration
Affects Version/s: 2.18.0
Fix Version/s: 3.0.0

Type: Improvement Priority: Unknown
Reporter: Damian Hickey Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Implement 3.0 release
Quarter: FY24Q3
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

BsonClassMap __classMaps is static mutable, with popsicle immutablity.

This makes:

  • Testing class maps in isolation is impossible.
  • Prevents parallel testing because setup is not thread-safe and (negative productivity effect)
  • Doesn't allow runtime service resolution of things like jsonserializers.
  • Problematic when using two or more seperate mongo databases in same process space (they end up sharing ClassMaps).

Instead, perhaps, add a BsonClassMaps custom collection, move the static methods to there as non-static and allow it to be injected into a MongoClient (vis MongoClientSettings).

services.AddBsonClassMaps(map => map.RegisterClassMap<>...);



 Comments   
Comment by Damian Hickey [ 10/Mar/23 ]

Hi James

Long time no see - you may remember me from PSake days .

That sounds awesome, I would really like to see that happen.

Thanks for fast response and sorry for slow one from me.

Cheers!

Comment by James Kovacs [ 01/Feb/23 ]

Hi, Damian,

Thank you for raising this issue. Although serialization is a separate concern from the lifetime of a MongoClient we plan to address both in CSHARP-3431. We want to scope all resources to the lifetime of a MongoClient including connections, pools, serializers, etc. This would allow users to create two separate MongoClient instances with different configurations for serializing a single class. Right now to achieve this, you have to mirror your class definitions and apply different mapping configurations to each one. This would also make testing the driver much easier as you could dispose of a MongoClient and know that all configuration was cleaned up. You could also test two MongoClient instances in parallel without worrying about automatic serializer configuration from one leaking into the other.

Please watch this tickets for updates.

Sincerely,
James

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