abpframework / abp

Open-source web application framework for ASP.NET Core! Offers an opinionated architecture to build enterprise software solutions with best practices on top of the .NET. Provides the fundamental infrastructure, cross-cutting-concern implementations, startup templates, application modules, UI themes, tooling and documentation.
https://abp.io
GNU Lesser General Public License v3.0
12.93k stars 3.44k forks source link

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard #5010

Open vd3d opened 4 years ago

vd3d commented 4 years ago

Hi,

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

hikalkan commented 4 years ago

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?

vd3d commented 4 years ago

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.

hikalkan commented 4 years ago

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.

mtozlu commented 4 years ago

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 commented 4 years ago

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
maliming commented 4 years ago

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?

mtozlu commented 4 years ago

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 commented 4 years ago

"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

maliming commented 4 years ago

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

mtozlu commented 4 years ago

There is currently no 3.x package. right?

No. It is not released yet.

shersh commented 1 month ago

Any news?