NextId 1.0.0

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

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

NextId

Strongly-typed, K-Sortable globally unique identifier with checksum


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 for 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.

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 403 12/23/2023
1.1.0 139 12/23/2023
1.0.1 287 8/19/2023
1.0.0 276 7/2/2023