[CSHARP-4424] Add a BsonClassMap method that registers a class map only if it has not already been registered Created: 22/Nov/22  Updated: 01/Dec/22  Resolved: 01/Dec/22

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

Type: Task Priority: Unknown
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The 3 RegisterClassMap methods in BsonClassMap are meant to only be called once for a given type. Normally we expect an application to know whether or not serialization has already been configured or not, but there are potential scenarios where conditional registration might be useful (such as multiple dependencies sharing some common data models where you don't have control over which dependency gets initialized first).

It would be helpful to have a method that can be safely called without knowing in advance whether a class map for a given type has already been registered or not.

This method could be something like:

public void RegisterClassMapIfNotRegistered<TClass>(Action<BconClassMap<TClass>> classMapInitializer)

A possible alternative is to use the `TryXyz` pattern:

public bool TryRegisterClassMap<TClass>(Action<BsonClassMap<TClass>> classMapInitializer, out BsonClassMap<TClass> classMap)

But the question is whether the `bool` return value or the `out classMap` parameter add any usefulness.



 Comments   
Comment by Githook User [ 01/Dec/22 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-4424: Added TryRegisterClassMap method to BsonClassMap.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/6a203a450f97af76bcba73ea0815170bf63aa2a8

Comment by Robert Stam [ 22/Nov/22 ]

Original user PR requesting this feature:

https://github.com/mongodb/mongo-csharp-driver/pull/941

A second PR with alternative `RegisterClassMapIfNotAlreadyRegistered` method:

https://github.com/mongodb/mongo-csharp-driver/pull/948

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