OrleansContrib / Orleans.Providers.MongoDB

A MongoDb implementation of the Orleans Providers: Membership, Storage and Reminders.
MIT License
100 stars 43 forks source link

MongoDb UUID storage type support #96

Closed randrewy closed 1 year ago

randrewy commented 2 years ago

There is a way to set guid db representation using C# MongoDb client using GuidSerializer Is there anything like that option in MongoDB Storage Provider? Any plans to add it in future?

I've tried both [BsonGuidRepresentation(GuidRepresentation.Standard)] annotation on my Guid field and global BsonDefaults.GuidRepresentation = GuidRepresentation.Standard; setting, but neither of them worked for me.

SebastianStehle commented 2 years ago

What is the current represenation?

randrewy commented 2 years ago

Currently it is stored as string like .ToString was called on guid object: "db331560-56c6-4a35-84cc-1846d0d5cc7e"

SebastianStehle commented 2 years ago

This library does not use mongo serializer. It uses Newtonsoft.JSON and then converts it to BSON. So you can write your own serializer if you need another representation. But binary data is not supported.

randrewy commented 2 years ago

Actually I can write custom converter to store Guid as binary data, but that means I have to

I've looked through the code and found this converter you are talking about and there is this special case for Guid type https://github.com/OrleansContrib/Orleans.Providers.MongoDB/blob/22bc56b54b9407453116d9f2294700e40992bdb4/Orleans.Providers.MongoDB/StorageProviders/JsonBsonConverter.cs#L79-L80 I can confirm that Newtonsoft.Json library recognize guids and serialize them with proper JTokenType.Guid MongoDb Driver has it's own converter in form of Implicit Conversion (Guid to BsonValue) and it should tolerate at least global BsonDefaults.GuidRepresentation settings (I've never checked that). Edit: implicit conversion is deprecated and BsonBinaryData(Guid guid, GuidRepresentation representation) should be used.

So it looks like there is everything needed to implement this feature. The only remaining questions are: is is it planned for implementation? PR are welcomed?

SebastianStehle commented 2 years ago

I do not see that much benefit in a byte representation. It is so much harder to debug.

But you are welcome to provide a PR, but it must be backwards compatible. BsonDefaults.GuidRepresentation is therefore not an option.

I see 2 options:

  1. Just provide a flag for the line you mentioned. But you have to think about how to do the deserialization.
  2. Provide a flag to use Mongo Serialization instead of the conversion.
SebastianStehle commented 1 year ago

Is this something you want to work on or can we close the issue?