alexandre-spieser / mongodb-generic-repository

An example of generic repository implementation using the MongoDB C# Sharp 2.0 driver (async)
MIT License
311 stars 84 forks source link

Random is a false random generate,why dou you use it yet #31

Closed liuyl1992 closed 4 years ago

liuyl1992 commented 4 years ago

https://github.com/alexandre-spieser/mongodb-generic-repository/blob/f0af32adcad2b58a435cdad72d4e041331030b8e/MongoDbGenericRepository/Utils/IdGenerator.cs#L11

alexandre-spieser commented 4 years ago

What do you mean by false random generator? It's not crypto security grade, but it's good enough for most purposes. To be honest, I'd advise against any of those:

                case "Int16":
                    return (TKey)(object)Random.Next(1, short.MaxValue);
                case "Int32":
                    return (TKey)(object)Random.Next(1, int.MaxValue);
                case "Int64":
                    return (TKey)(object)(Random.NextLong(1, long.MaxValue));

For generating primary keys in MongoDB, it's just for retrocompatibility for the EF stuff that is relying of Identity columns and incremental Ids. If you still want to use integers for Id's you can always rely on the heavy duty RNG Crypto generator for "real randomness".