Siticone IdGen
The SiticoneIdGen component is a high-performance, thread-safe non-visual component used for generating infinite unique identifiers.
It supports multiple generation strategies—including Twitter Snowflake (Distributed), Cryptographic Randomness, UUIDv1, and ULID—making it suitable for database primary keys, session tokens, license keys, and distributed system IDs.
Configuration Properties
Properties that define the format and structure of the generated IDs.
| Property | Type | Description & Usage Example |
|---|---|---|
Strategy |
IdGenerationStrategy |
idGen.Strategy = IdGenerationStrategy.Distributed;
The algorithm used to generate IDs. Options: Distributed (Snowflake), CryptoRandom, UuidV1, Ulid.
|
Length |
int | idGen.Length = 32; The total number of characters in the generated ID (excluding prefixes/suffixes). Must be at least 16. |
Prefix |
string | idGen.Prefix = "ORD-"; A string prepended to the generated ID. Useful for identifying ID types (e.g., 'USR-', 'INV-'). |
Suffix |
string | idGen.Suffix = "-X"; A string appended to the end of the generated ID. |
BatchSize |
int | idGen.BatchSize = 100; Configures the default size for batch generation operations. |
Formatting & Character Set
Control the visual presentation and allowed characters for the IDs.
| Property | Type | Description |
|---|---|---|
UseUpperCase |
bool | Converts the final output to uppercase (default: true). |
IncludeDashes |
bool | Inserts hyphens at regular intervals to improve readability. |
DashInterval |
int | Defines the number of characters between dashes (default: 4). |
IncludeNumbers |
bool | Includes digits (0-9) in the allowed character set. |
IncludeSpecialChars |
bool | Includes special characters (!@#$%) in the set. Use with caution for URLs. |
CustomCharacterSet |
string | Overrides all other settings to use strictly this set of characters (e.g., "01" for binary). |
ProhibitedCharacters |
string | Comma-separated list of characters to exclude (e.g., "I,O,1,0" to avoid confusion). |
Distributed (Snowflake) Settings
These properties are specifically used when Strategy is set to Distributed. They ensure uniqueness across different machines/nodes.
| Property | Type | Description |
|---|---|---|
NodeId |
int | Unique ID (0-1023) representing the machine generating the ID. |
DatacenterId |
int | Unique ID (0-1023) representing the physical datacenter or region. |
Epoch |
DateTime | The start date for timestamp calculation. Defaults to Jan 1, 2024. |
Namespace |
string | A string hashed into the ID to prevent collisions between different entity types. |
ShardId |
int | Optional logical shard identifier mixed into the Node bits. |
Public Methods
// Generates a single unique identifier based on current configuration.
// Thread-safe and blocking.
string newId = siticoneIdGen1.GenerateId();
// Generates a unique identifier asynchronously.
// Recommended for batch operations to prevent UI freezing.
string newId = await siticoneIdGen1.GenerateIdAsync();
// Checks if a string conforms to the current format configuration
// (Length, Prefix, Suffix, Allowed Characters).
bool isValid = siticoneIdGen1.ValidateId("USR-1234-ABCD");
// Extracts metadata (Timestamp, NodeId) from a generated ID.
// Only works for time-based strategies (Distributed, UuidV1, Ulid).
IdMetadata meta = siticoneIdGen1.ParseId(someId);
DateTime createdTime = meta.Timestamp;
Events
// Occurs immediately after a new ID is successfully generated.
siticoneIdGen1.IdGenerated += (sender, e) =>
{
Console.WriteLine($"New ID Created: {e.GeneratedId}");
};
Enumerations
public enum IdGenerationStrategy
{
// Twitter Snowflake algorithm. Time-ordered, distributed, high performance.
Distributed,
// Cryptographically secure random numbers. Best for security tokens.
CryptoRandom,
// Standard UUID Version 1 (Time-based).
UuidV1,
// Universally Unique Lexicographically Sortable Identifier.
Ulid
}
Usage Examples
Example 1: Distributed (Snowflake) ID Generation
Best for database primary keys in distributed systems. IDs are roughly time-ordered and contain information about which machine generated them.
private void SetupDistributedGen()
{
// Configure for Snowflake algorithm
siticoneIdGen1.Strategy = IdGenerationStrategy.Distributed;
// Identify this machine/process
siticoneIdGen1.NodeId = 1; // Machine 1
siticoneIdGen1.DatacenterId = 1; // Region 1
// Optional: Set a custom epoch (e.g., app launch date)
siticoneIdGen1.Epoch = new DateTime(2023, 1, 1, 0, 0, 0, DateTimeKind.Utc);
// Generate
string dbKey = siticoneIdGen1.GenerateId();
// Extract info later
IdMetadata meta = siticoneIdGen1.ParseId(dbKey);
Console.WriteLine($"Created at: {meta.Timestamp} by Node {meta.NodeId}");
}
Example 2: Secure License Key Generation
Generates a readable, dashed, alphanumeric license key using cryptographic randomness.
private string GenerateLicenseKey()
{
siticoneIdGen1.Strategy = IdGenerationStrategy.CryptoRandom;
siticoneIdGen1.Length = 20;
// Format: XXXX-XXXX-XXXX-XXXX-XXXX
siticoneIdGen1.IncludeDashes = true;
siticoneIdGen1.DashInterval = 4;
siticoneIdGen1.UseUpperCase = true;
// Ensure readable characters (exclude 0, O, 1, I)
siticoneIdGen1.CustomCharacterSet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
return siticoneIdGen1.GenerateId();
}
Example 3: Async Batch Generation
Efficiently generates a large batch of IDs without blocking the UI thread.
private async void BtnGenerateBatch_Click(object sender, EventArgs e)
{
siticoneIdGen1.Strategy = IdGenerationStrategy.Ulid;
List<string> ids = new List<string>();
btnGenerate.Enabled = false;
await Task.Run(() =>
{
for (int i = 0; i < 1000; i++)
{
// Thread-safe call
ids.Add(siticoneIdGen1.GenerateId());
}
});
listBoxResults.DataSource = ids;
btnGenerate.Enabled = true;
}
Example 4: Custom Prefix/Suffix for Invoices
Using simple prefixes to categorize IDs.
public string CreateInvoiceId()
{
// Result: INV-2024-X9Z1-A2B3
siticoneIdGen1.Strategy = IdGenerationStrategy.CryptoRandom;
siticoneIdGen1.Length = 12;
siticoneIdGen1.Prefix = "INV-" + DateTime.Now.Year + "-";
siticoneIdGen1.IncludeDashes = true;
return siticoneIdGen1.GenerateId();
}