NextId 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package NextId --version 1.0.1                
NuGet\Install-Package NextId -Version 1.0.1                
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="NextId" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NextId --version 1.0.1                
#r "nuget: NextId, 1.0.1"                
#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 NextId as a Cake Addin
#addin nuget:?package=NextId&version=1.0.1

// Install NextId as a Cake Tool
#tool nuget:?package=NextId&version=1.0.1                

NextId

Strongly-typed, K-Sortable globally unique identifier with checksum for .NET 7+.


Example

Example value:

user-4B4XH7BnCp68CCY8mzVbNT5X
  • Left from - is type (max 11 characters)
  • Right from - is id value

id is componsed of:

  • First 8 chars, Unix timestamp in milliseconds
  • Next 2 chars, microseconds of the timestamp
  • Next 11 chars, random value
  • Final 3 chars, checksum value

Checksum can be used to validate id before going to database to detect malicious or unexpected activities by clients.


How to use it

Install nuget package

dotnet add package NextId

Inherit NextId.Identifier class:

public class UserId : Identifier<UserId>
{
    private const string PrefixConst = "user";
    private const string SaltConst = "99AAB45utg";
    protected override string Prefix => PrefixConst;
    protected override string Salt => SaltConst;

    public UserId() { }
    
    // constructor with time component
    public UserId(DateTimeOffset dt) : base(dt) { }

    // constructor for parsing
    private UserId(string value) : base(value) { }

    public static UserId NewId() => new();

    public static UserId Parse(string value) => new(value);

    public static bool IsValid(string value) => IsValid(value, PrefixConst, SaltConst);
    
    // you can add TryParse if needed
}

You can call new UserId() or UserId.NewId() for new values, IsValid for validation of existing values and Parse for parsing existing values.

UserId id1 = UserId.NewId();
UserId id2 = UserId.Parse(id1.Value);

(id1 == id2).Should().BeTrue();

You can get string value of identifier by calling Value property or ToString() method.

Prefix will be set as identifier prefix (user in this case). Value must have less than 12 characters and can contain only ASCII letters and digits.

Salt must be less than 33 characters and should be set to random value to each type. Once set, it must not be changed.

Serialization

System.Text.Json serializer options are available.

First add package reference:

dotnet add package NextId.Serialization.Json
var options = new JsonSerializerOptions();
options.AddIdentifierConverters();

User user1 = User.NewRandomUser();
string json = JsonSerializer.Serialize(user1, options);
User user2 = JsonSerializer.Deserialize<User>(json, options)!;

Performance

  • ~866,551 generated ids per second per core
  • ~369,822 parsed ids per second per core
BenchmarkDotNet=v0.13.5, OS=Windows 10 (10.0.19045.3086/22H2/2022Update)
AMD Ryzen 7 2700X, 1 CPU, 16 logical and 8 physical cores
.NET SDK=7.0.304
  [Host]     : .NET 7.0.7 (7.0.723.27404), X64 RyuJIT AVX2
  DefaultJob : .NET 7.0.7 (7.0.723.27404), X64 RyuJIT AVX2


|     Method |     Mean |     Error |    StdDev |
|----------- |---------:|----------:|----------:|
| NewId_1000 | 1.154 ms | 0.0054 ms | 0.0050 ms |
| Parse_1000 | 2.704 ms | 0.0309 ms | 0.0258 ms |

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NextId:

Package Downloads
NextId.Serialization.Json

System.Text.Json serializer support for NextId identifiers

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.1 401 12/23/2023
1.1.0 137 12/23/2023
1.0.1 285 8/19/2023
1.0.0 274 7/2/2023