SequentialGuid 4.0.5

dotnet add package SequentialGuid --version 4.0.5                
NuGet\Install-Package SequentialGuid -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" Version="4.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SequentialGuid --version 4.0.5                
#r "nuget: SequentialGuid, 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 as a Cake Addin
#addin nuget:?package=SequentialGuid&version=4.0.5

// Install SequentialGuid as a Cake Tool
#tool nuget:?package=SequentialGuid&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.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (13)

Showing the top 5 NuGet packages that depend on SequentialGuid:

Package Downloads
BR.MicroServices.Utilities

Package Description

SequentialGuid.NodaTime

Provide extension methods to round trip between NodaTime Instant and sequential guid

Loyo.EntityFramework

EntityFrameworkCore UnitOfWork Repositories

Atomiv.Infrastructure.SequentialGuid

Package Description

Optivem.Atomiv.Infrastructure.SequentialGuid

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.5 44,663 3/20/2024
4.0.4 404,261 2/27/2023
4.0.3 49,277 10/25/2022
4.0.2 8,710 10/20/2022
4.0.1 5,296 10/10/2022
4.0.0 3,455 10/5/2022
3.0.1 100,596 2/22/2022
3.0.0 34,662 9/15/2021
2.5.0 257,312 11/18/2020
2.4.2 21,779 7/31/2020
2.4.1 36,483 7/8/2020
2.4.0 183,772 12/13/2017
2.2.1 2,120 10/17/2017
2.2.0 2,102 9/24/2017
1.0.0 5,360 2/17/2014