Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoogn authored Nov 24, 2023
1 parent 870e3a9 commit c637d70
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
### SnowflakeId
This is the implementation of twitter's snowflakeId algorithm in C# language
This is the implementation of twitter's snowflakeId algorithm in C# programming language

### Get Started
SnowflakeId is a library that can help you to generate a unique Id, specifically for those who are working in a Distributed Systems.
The currently version is version 2.0.0, and there are break changes in this version with version 1.0.0 so be careful when you upgrade from version 1.0.0 to version 2.0.0
The currently version is version 3.0.0, and there are break changes in this version when you upgrade from an older versions so be careful when you upgrade to version 3.0.0

For any .NET application higher than .Net 6 install the library by using NuGet package command
```
dotnet add package Hussien.SnowflakeId --version 2.0.0
dotnet add package Hussien.SnowflakeId --version 3.0.0
```
---

### With Asp.Net Core Application

First you have to imports these namespaces
```
using Microsoft.AspNetCore.Builder;
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeIdResult = SnowflakeId.Provider.SnowflakeId;
using SnowflakeId.DependencyInjection;
```

Second register the Snowflake service by adding these lines:
```C#
builder.Services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1;
options.DataCenterId = 1; // Its best if you read the value from the appsettings.json file
});
```

Expand All @@ -44,7 +43,6 @@ app.MapGet("/", (ISnowflakeService snowflakeService, ISnowflakeIdProvider snowfl
long generatingId = snowflakeService.GenerateSnowflakeId();
SnowflakeIdResult sn = snowflakeIdProvider.GetDateTimeBySnowflakeId(generatingId);


return $"The genrated Id is: { generatingId } - and is genrated at { sn.GeneratedDateTime }";
});

Expand All @@ -57,27 +55,33 @@ And here is the result if you run the app:
---
### With Console Application
```C#
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeId.DependencyInjection;
using System;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSnowflakeUniqueId(options =>
{
options.DataCenterId = 1;
options.DataCenterId = 7;
});
}).Build();

var idServive = host.Services.GetRequiredService<ISnowflakeService>();
var idProvider = host.Services.GetRequiredService<ISnowflakeIdProvider>();

var uniqueId = idServive.GenerateSnowflakeId();
Console.WriteLine("The unique Id is: {0}", uniqueId);
Console.WriteLine("*******************************");

var generatedAt = idProvider.GetDateTimeBySnowflakeId(uniqueId);
var generatedAt = idServive.GetGeneratedDateTimeBySnowflakeId(uniqueId);
Console.WriteLine("The Id is: {0} and is generated At: {1}", uniqueId, generatedAt);

var dataCenterId = idServive.GetDataCenterIdBySnowflakId(uniqueId);
Console.WriteLine("The id is generated at data center has id: {0}", dataCenterId);

Console.WriteLine("The Id is: {0} and is generated At: {1}", generatedAt.Id, generatedAt.GeneratedDateTime);
Console.ReadLine();
```

As you can see from the previous code, you can generate a new id, then you can query at which time the id is generated, and lastly at which data center id is saved.

0 comments on commit c637d70

Please sign in to comment.