TwoRivers.IdGeneratrion 1.1.6

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package TwoRivers.IdGeneratrion --version 1.1.6
                    
NuGet\Install-Package TwoRivers.IdGeneratrion -Version 1.1.6
                    
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="TwoRivers.IdGeneratrion" Version="1.1.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TwoRivers.IdGeneratrion" Version="1.1.6" />
                    
Directory.Packages.props
<PackageReference Include="TwoRivers.IdGeneratrion" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TwoRivers.IdGeneratrion --version 1.1.6
                    
#r "nuget: TwoRivers.IdGeneratrion, 1.1.6"
                    
#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.
#:package TwoRivers.IdGeneratrion@1.1.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TwoRivers.IdGeneratrion&version=1.1.6
                    
Install as a Cake Addin
#tool nuget:?package=TwoRivers.IdGeneratrion&version=1.1.6
                    
Install as a Cake Tool

TwoRivers.IdGeneration

The TwoRivers.IdGeneration library provides tools for generating unique identifiers tailored to various scenarios, including time-based IDs, cryptographic keys, and database-optimized sequential GUIDs. These generators ensure uniqueness, security, and optimal database performance across distributed systems.

Table of Contents

Installation

You can install the TwoRivers.IdGeneration package via NuGet Package Manager:

Install-Package TwoRivers.IdGeneration

Or via the .NET CLI:

dotnet add package TwoRivers.IdGeneration

Components

TimeBasedIdGenerator

Generates unique identifiers based on the current timestamp, encoded in Base36 format. This generator is thread-safe and ensures that IDs generated within the same millisecond are incremented sequentially.

Key Features:

  • Time-based uniqueness ensuring chronological ordering
  • Base36 encoding for compact, alphanumeric strings
  • Thread-safe with locking mechanisms
  • Sequential increment for concurrent generation

Usage:

using TwoRivers.IdGeneration;

// Register as singleton in DI container
services.AddSingleton<IIdGenerator, TimeBasedIdGenerator>(sp => 
	new TimeBasedIdGenerator(TimeProvider.System));

// Generate unique IDs
var id = idGenerator.NextId();  // Returns: "ABC123XYZ"

// Decode back to timestamp
var timestamp = TimeBasedIdGenerator.Decode(id);
Console.WriteLine($"Created at: {timestamp}");

KeyGenerator

Generates cryptographically secure random keys using uppercase letters and numbers (A-Z, 0-9). Ideal for API keys, tokens, and other security-sensitive identifiers.

Key Features:

  • Cryptographically secure using RandomNumberGenerator
  • Configurable length
  • Uppercase alphanumeric characters only (excludes lowercase for clarity)
  • Implements IDisposable for proper resource cleanup

Usage:

using TwoRivers.IdGeneration;

using var keyGen = new KeyGenerator();

// Generate a 32-character key
var apiKey = keyGen.Generate(32);  // Returns: "K7M9X2QWERTY4P8N3VCXZ1ASJKLF6H"

CombGuid

Factory for generating database-optimized sequential GUIDs. Uses DatabaseType enum to select the appropriate implementation for your database engine.

Database-Specific Optimization: Different databases compare GUID bytes in different orders, so sequential GUIDs MUST be optimized for your specific database to avoid fragmentation.

Usage:

using TwoRivers.IdGeneration;

// For SQL Server
var sqlGuid = CombGuid.NewGuid(DatabaseType.SqlServer);

// For PostgreSQL, MySQL, Oracle
var pgGuid = CombGuid.NewGuid(DatabaseType.PostgreSql);

SqlServerCombGuid

Sequential GUID generator optimized for SQL Server's non-standard byte comparison order. Places timestamp in the last 6 bytes (10-15) which SQL Server compares first.

When to Use: SQL Server databases using UNIQUEIDENTIFIER primary keys or clustered indexes.

Benefits:

  • Reduces index fragmentation
  • Improves INSERT performance
  • Maintains chronological ordering in SQL Server

Usage:

using TwoRivers.IdGeneration;

var guid = SqlServerCombGuid.NewGuid();
// Example: "a1b2c3d4-e5f6-4789-abcd-123456789abc"
// Last 6 bytes contain timestamp for SQL Server ordering

PostgreSqlCombGuid

Sequential GUID generator optimized for PostgreSQL, MySQL, Oracle, and other databases using RFC 4122 lexicographic ordering. Places timestamp in the first 6 bytes (0-5).

When to Use: PostgreSQL, MySQL, Oracle, or any database using standard left-to-right GUID comparison.

Benefits:

  • Reduces index fragmentation
  • Improves INSERT performance
  • Maintains chronological ordering in standard-compliant databases

Usage:

using TwoRivers.IdGeneration;

var guid = PostgreSqlCombGuid.NewGuid();
// Example: "123456789abc-e5f6-4789-abcd-a1b2c3d4"
// First 6 bytes contain timestamp for lexicographic ordering

Base36 Encoding

Internal utility for encoding/decoding Int64 values to/from Base36 format. Used by TimeBasedIdGenerator for compact ID representation.

Features:

  • Encodes Int64 to alphanumeric strings (0-9, A-Z)
  • Decodes Base36 strings back to Int64
  • Supports negative values
  • Overflow detection

Usage

Complete Example: Entity with Database-Specific GUIDs

using Microsoft.EntityFrameworkCore;
using TwoRivers.IdGeneration;

public class Order
{
	// Use database-specific GUID generation
	public Guid Id { get; private set; }
	public String OrderNumber { get; private set; }

	public Order(DatabaseType dbType)
	{
		Id = CombGuid.NewGuid(dbType);
		OrderNumber = GenerateOrderNumber();
	}

	private String GenerateOrderNumber()
	{
		using var keyGen = new KeyGenerator();
		return keyGen.Generate(12);
	}
}

// In your DbContext configuration
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	modelBuilder.Entity<Order>(entity =>
	{
		// SQL Server
		entity.Property(e => e.Id)
			  .HasDefaultValueSql("NEWSEQUENTIALID()");

		// Or in code (preferred for testing)
		entity.HasData(new Order(DatabaseType.SqlServer));
	});
}

Dependency Injection Setup

using Microsoft.Extensions.DependencyInjection;
using TwoRivers.IdGeneration;

services.AddSingleton<IIdGenerator, TimeBasedIdGenerator>(sp => 
	new TimeBasedIdGenerator(TimeProvider.System));

services.AddScoped<IKeyGenerator, KeyGenerator>();

// Store database type in configuration
services.Configure<DatabaseOptions>(options => 
	options.Type = DatabaseType.PostgreSql);

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
2.0.0-beta.8 69 3/5/2026
2.0.0-beta.7 63 3/5/2026
2.0.0-beta.6 59 3/5/2026
2.0.0-beta.5 60 3/4/2026
2.0.0-beta.4 61 3/4/2026
2.0.0-beta.3 64 3/3/2026
2.0.0-beta.2 66 3/3/2026
2.0.0-beta.1 58 3/3/2026
2.0.0-beta.0 65 3/3/2026
1.1.6 113 2/26/2026
1.1.5 109 2/25/2026
1.1.4 115 2/22/2026
1.1.3 117 2/22/2026
1.1.2 118 2/22/2026
1.1.1 116 2/22/2026
1.1.0 118 2/22/2026
1.0.0 144 2/17/2026
Loading failed