🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

SequentialGuid

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

SequentialGuid

Will generate Sequential Guids based on MongoDB's ObjectId specification. Date & time are encoded into the value so you do not need to store them separately in your database. Supports standard Guids as well as SQL Server guids.

4.0.6
Source
NuGet
Version published
Maintainers
1
Created
Source

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();

Keywords

Sequential

FAQs

Package last updated on 19 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts