Sharpflake 1.0.0
dotnet add package Sharpflake --version 1.0.0
NuGet\Install-Package Sharpflake -Version 1.0.0
<PackageReference Include="Sharpflake" Version="1.0.0" />
paket add Sharpflake --version 1.0.0
#r "nuget: Sharpflake, 1.0.0"
// Install Sharpflake as a Cake Addin #addin nuget:?package=Sharpflake&version=1.0.0 // Install Sharpflake as a Cake Tool #tool nuget:?package=Sharpflake&version=1.0.0
Sharpflake
A Simple C# Snowflake ID library
This will allow you to generate unique IDs for your application but also parse the details back.
Installation
To install the package, you can use the following command:
dotnet add package Sharpflake
Usage with Dependency Injection
To use the library together with ASP.net on your Program.cs, while configuring the services, you can add the following:
builder.Services.AddSnowflakeIdGenerator();
Then you can inject the ISnowflakeIdGenerator
interface into your services and use it to generate IDs.
public class MyController(ISnowflakeIdGenerator generator): ControllerBase {
[HttpGet]
public void MyGet() {
var snowflake = generator.GenerateSnowflake();
}
}
Usage without Dependency Injection
If you are not using Dependency Injection, you can create an instance of the SnowflakeGenerator
class and use it to generate IDs.
var generator = new SnowflakeGenerator();
var snowflake = generator.GenerateSnowflake();
Advanced Configuration
You can also configure the generator to use a custom epoch and worker ID, the epoch can be set using a timestamp in milliseconds or using the DateTime
// Using the DateTime class
var configA = new Configuration(
new DateTime(2024, 7, 19, 0, 0, 0, 0),
0
);
// Using a timestamp
var configB = new Configuration(
1577836800000,
1
);
var generatorA = new SnowflakeGenerator(configA);
var generator2 = new SnowflakeGenerator(configB);
When using a dependency injection, this can be done by adding the configuration to the AddSnowflakeIdGenerator
method.
// Using the DateTime class
builder.Services.AddSnowflakeIdGenerator(new SnowflakeConfig(
new DateTime(2024, 7, 19, 0, 0, 0, 0),
0
));
// Using the timestamp
builder.Services.AddSnowflakeIdGenerator(new SnowflakeConfig(
1577836800000,
1
));
// Using the configuration class
var config = new Configuration(
new DateTime(2024, 7, 19, 0, 0, 0, 0),
0
);
builder.Services.AddSnowflakeIdGenerator(config);
Parsing a Snowflake
Sharpflake includes methods to parse a snowflake into its components.
The parsing can be done on string
and long
types.
'62937765418893312'.ToSnowflake();
In case you have a custom configuration, you should pass the configuration to the parser
var config = new Configuration(
new DateTime(2024, 7, 19, 0, 0, 0, 0),
0
);
'62937765418893312'.ToSnowflake(config);
Or you can pass the generator to the parser
var config = new Configuration(
new DateTime(2024, 7, 19, 0, 0, 0, 0),
0
);
var generator = new SnowflakeGenerator(config);
'62937765418893312'.ToSnowflake(generator);
It's also possible to type cast a string
or long
to a Snowflake
object,
this will automatically parse the snowflake, but it doesn't take a custom configuration.
var snowflake = (Snowflake)'62937765418893312';
Related Projects
- SwiftySnowflake - A Swift Snowflake ID library
- avalanche - A Simple TypeScript Snowflake ID library
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
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 |
---|---|---|
1.0.0 | 71 | 1/18/2025 |