abp icon indicating copy to clipboard operation
abp copied to clipboard

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard

Open vd3d opened this issue 5 years ago • 11 comments

Hi,

I suspect that you should use BsonDefaults.GuidRepresentation = GuidRepresentation.Standard !

vd3d avatar Aug 09 '20 19:08 vd3d

I don't have experience on that before. Can you explain more please? We havent't got any problem related to that. Why do you think we should change it?

hikalkan avatar Aug 09 '20 21:08 hikalkan

Because you use the "C# legacy" UID format. Today there is a new "standard" one.

Also, if I want to create jobs in any language, it is preferrable to not have something related to .NET

Also, I suspect (to verify) that the keys range can be bigger with the standard one, ie. you can scale more.

vd3d avatar Aug 10 '20 09:08 vd3d

Thanks for the information. I will investigate that. Anyone can do it today for their applications. We will decide to make it default on the startup template or in the framework.

hikalkan avatar Aug 10 '20 14:08 hikalkan

Currently, all Guid properties are stored as LUUID (Legacy UUID) in mongodb, which has problems with converting due to different byte orders being used. More info can be found here: https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/guidserialization/background/

To use the latest standard, one must set the GuidRepresentation to Standard. However, it is stated here that BsonDefaults.GuidRepresentation is obsolete: https://mongodb.github.io/mongo-csharp-driver/2.11/apidocs/html/P_MongoDB_Bson_BsonDefaults_GuidRepresentation.htm

The new way of setting this is by using serializers. Currently i am able to set this in OnApplicationInitialization of MongoDbModule. Now all my Guids are of UUID type. Here is my code;

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
    BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
    BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
}

I would like to create a PR but, current dev branch is using .net5 which requires installing a separate VS preview version to use latest MSBuild. If that's okay, i can create a new branch from a previous branch which uses .net3.1.

mtozlu avatar Oct 16 '20 08:10 mtozlu

Another note; If you are using MongoDB with GuidRepresentationMode.V3 and configuring Guid as ExtraProperty of an object (Object Extension), mongodb driver throws error;

"This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2".

Example:

ObjectExtensionManager.Instance.Modules().ConfigureIdentity(identity => {
  identity.ConfigureUser(user => {
    user.AddOrUpdateProperty<System.Guid>("WarehouseId");
  });
});

user.SetProperty("WarehouseId", id); //id is of type System.Guid

mtozlu avatar Oct 29 '20 14:10 mtozlu

hi @mtozlu

We should change this option in the template project and not in the mongodb module. Otherwise it will be a breaking change.

Since this is configurable, maybe we shouldn't change it, because currently it does not affect our application.

"This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2".

Can you provide an example project?

maliming avatar Nov 04 '20 07:11 maliming

In 3.x releases of mongo driver, V3 will be the default with no ability to change.

In the v2.x versions of the driver V2 is the default mode but V3 mode is supported. In future v3.x versions of the driver V3 will be the default mode (and support for V2 mode will be removed).

https://mongodb.github.io/mongo-csharp-driver/2.11/reference/bson/guidserialization/guidrepresentationmode/guidrepresentationmode/

In that case if abp mongo package is upgraded to use mongo 3.x driver, all applications using 2.x version will have a breaking change since all UUID's in their databases are of legacy type. Clearly, some upgrading strategy needs to be discussed before using 3.x packages in abp.

Since this is configurable, maybe we shouldn't change it, because currently it does not affect our application.

Current template applications do not have any problem but they use Legacy UUID type. Legacy UUID's have problems as described in previous comments. Maybe it can be changed in template application or in mongodb project using MongoClientSettings (see #6036) which has a GuidRepresentation property.

mtozlu avatar Nov 04 '20 07:11 mtozlu

"This constructor can only be used when BsonDefaults.GuidRepresentationMode is V2".

Can you provide an example project?

Regarding this; it is about the scenario if anyone wants to use new GuidRepresentation with the way i described in this comment and after that, if they want to use System.Guid type in an extended object (using abp object extending system) as described in this comment, the driver throws an error. So it was just a note to consider if abp wants to use new GuidRepresentationMode.

Here is an example project, just run the DbMigrator to observe the error. The changes i made are specified in commits. https://github.com/mtozlu/abp-example-guid-representation

mtozlu avatar Nov 04 '20 08:11 mtozlu

In that case if abp mongo package is upgraded to use mongo 3.x driver, all applications using 2.x version will have a breaking change since all UUID's in their databases are of legacy type.

There is currently no 3.x package. right?

https://github.com/mongodb/mongo-csharp-driver/releases

maliming avatar Nov 11 '20 06:11 maliming

There is currently no 3.x package. right?

No. It is not released yet.

mtozlu avatar Nov 11 '20 06:11 mtozlu

Any news?

shersh avatar Sep 13 '24 16:09 shersh