[CSHARP-2143] How to access IMongoDatabase from IBsonSerializer Created: 06/Jan/18  Updated: 27/Oct/23  Resolved: 31/Jan/18

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

Type: Task Priority: Major - P3
Reporter: Marcos Almeida Jr. Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Comments   
Comment by Robert Stam [ 31/Jan/18 ]

This is not possible because serialization is a general purpose mechanism that is handled at a lower level than the driver (IMongoDatabase is defined in MongoDB.Driver.dll but IBsonSerializer is in MongoDB.Bson.dll).

My recommendation is that you not think about trying to access a database from a Serialize or Deserialize method. Serialization should be a pure data conversion from one representation to another (e.g. from in-memory to BSON) and should not require access to any database.

You could create a custom serializer and pass in a reference to an IMongoDatabase to your custom serializer's constructor (so it would be part of the state of serializer), but still I don't recommend it.

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