SequentialGuid.MongoDB 4.0.5

dotnet add package SequentialGuid.MongoDB --version 4.0.5                
NuGet\Install-Package SequentialGuid.MongoDB -Version 4.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SequentialGuid.MongoDB" Version="4.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SequentialGuid.MongoDB --version 4.0.5                
#r "nuget: SequentialGuid.MongoDB, 4.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SequentialGuid.MongoDB as a Cake Addin
#addin nuget:?package=SequentialGuid.MongoDB&version=4.0.5

// Install SequentialGuid.MongoDB as a Cake Tool
#tool nuget:?package=SequentialGuid.MongoDB&version=4.0.5                

SequentialGuid

Continuous IntegrationNuGetLicense: MIT

Will generate Sequential Guids based on MongoDB's ObjectId specification sorting algorithm. Date & time are encoded into the value so you do not need to store them separately in your database

Author's Note: The entire purpose of this library is for you to have a dependency free way of generating unique uuid/Guid values that contain the time of creation which will typically result in lower clustered index fragmentation on the back end once they get persisted but it will allow you to generate the keys all the way up in WebAssembly or MAUI and pass it through your API and store it in the database this can help you with itempotency not requiring a trip to the database to generate the ID. Please DO NOT open an issue telling me it doesn't use the Unix timestamp or it isn't an ObjectId those are both true I had to find 32 additional bits to fill a Guid vs ObjectId and so I opted to use the Ticks count to do so while retaining the remainder of the Mongo algorithm. An added bonus to this is this library is not subject to break on 19 January 2038, at 03:14:07 UTC when the Unix timestamp overflows a 32 bit integer.

Returns a new Guid or SqlGuid. The 16-byte Guid or SqlGuid consists of:

  • A 8-byte timestamp, representing the Guid's creation, measured in system ticks.
  • A 5-byte random value generated once per process. This random value is unique to the machine and process.
  • A 3-byte incrementing counter, initialized to a random value.

If you use SQL Server then I highly recommend reading the following two articles to get a basic understanding of how SQL Server sorts uniqueidentifier values

Define an interface to the signature you like

public interface IIdGenerator
{
    Guid NewId();
}

Define your implementing class which can be transient since the singleton is implemented by the framework

public class SequentialIdGenerator : IIdGenerator
{
    public Guid NewId() => SequentialGuidGenerator.Instance.NewGuid();
}

Wire it up to .NET Core dependency injection in the ConfigureServices method during application startup

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IIdGenerator, SequentialIdGenerator>();
}

Finally define a base entity for your application which will contain an id and a timestamp as soon as you initialize it. Note I do not advocate setting a default Id getter this way just illustrating it can be done

public abstract class BaseEntity
{
    public Guid Id { get; set; } = SequentialGuidGenerator.Instance.NewGuid();
    public DateTime? Timestamp => Id.ToDateTime();
    // If you really must have non-UTC time
    public DateTime? LocalTime => Id.ToDateTime()?.ToLocalTime();
}

You can convert between a standard Guid and a SqlGuid using the available helper functions

var guid = SequentialGuidGenerator.Instance.NewGuid();
var sqlGuid = guid.ToSqlGuid();

OR

var sqlGuid = SequentialSqlGuidGenerator.Instance.NewSqlGuid();
var guid = sqlGuid.ToGuid();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.5 126 3/20/2024
4.0.4 282 2/27/2023
4.0.3 409 10/25/2022
4.0.2 431 10/20/2022
4.0.1 405 10/10/2022
4.0.0 373 10/5/2022
3.0.1 485 2/22/2022
3.0.0 401 9/15/2021
2.5.0 479 11/18/2020