[CSHARP-2644] Add a convention for storing an _id declared as string in a POCO as ObjectId in the database Created: 20/Jun/19  Updated: 28/Oct/23  Resolved: 03/Jul/19

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.8.1
Fix Version/s: 2.9.0

Type: Improvement Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Some applications want to keep their POCO classes free of any dependency on C# driver classes. That includes the _id field which typically is an ObjectId.

One viable approach is to declare the _id as type string, and convert from ObjectId to string when deserializing and from string back to ObjectId when serializing.

This is currently possible using attributes to annotate the POCO, but that defeats the goal of keeping the POCOs free of any dependency on C# driver classes.

This ticket is to add support for a convention-only approach to mapping _id fields between string in the application and ObjectId in the database.

There is an existing convention called `StringObjectIdIdGeneratorConvention` that is related to this goal, but only addresses a subset of this goal. What this convention does is add an appropriate IdGenerator to the IdMemberMap if and only if the Id member is a string that is already configured to be stored as an ObjectId in the database. But that requires that the IdMember already be configured to be stored as an ObjectId in the database.

A useful new convention would be one that simultaneously configures the string _id field to be stored as an ObjectId in the database and configures the appropriate IdGenerator.



 Comments   
Comment by Githook User [ 03/Jul/19 ]

Author:

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

Message: CSHARP-2644: Code review changes.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/16ba84a88792c695d3135a3ec7a687e0a0716b5a

Comment by Githook User [ 03/Jul/19 ]

Author:

{'name': 'nurih', 'username': 'nurih', 'email': '1554735+nurih@users.noreply.github.com'}

Message: CSHARP-2644: Add a convention for storing an _id declared as string in a POCO as ObjectId in the database.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/dca125f1987057684d09cd5f3f29123d11dadaa5

Comment by Nuri Halperin [ 21/Jun/19 ]

Added PR to resolve this issue:

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

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