From c637d70feb5831eaa73da7ad7e20e22af9e3d53d Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Fri, 24 Nov 2023 10:11:32 +0300 Subject: [PATCH 1/2] Update README.md --- README.md | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4ac0cde..1d500e2 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ ### 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 ``` --- @@ -15,17 +15,16 @@ dotnet add package Hussien.SnowflakeId --version 2.0.0 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 }); ``` @@ -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 }"; }); @@ -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(); -var idProvider = host.Services.GetRequiredService(); 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. From 13a4c399eb24b07f861c7f00834dece5e68c41ae Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Sat, 2 Dec 2023 21:11:45 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1d500e2..614f951 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,11 @@ dotnet add package Hussien.SnowflakeId --version 3.0.0 First you have to imports these namespaces ``` -using Microsoft.AspNetCore.Builder; using SnowflakeId.Core; using SnowflakeId.DependencyInjection; ``` -Second register the Snowflake service by adding these lines: +Second register the Hussine.SnowflakeId library service by adding next two lines of code: ```C# builder.Services.AddSnowflakeUniqueId(options => { @@ -28,8 +27,12 @@ builder.Services.AddSnowflakeUniqueId(options => }); ``` -### The Complete Example: +### The Complete Asp.NET Core Example: ```C# +using Microsoft.AspNetCore.Builder; +using SnowflakeId.Core; +using SnowflakeId.DependencyInjection; + var builder = WebApplication.CreateBuilder(args); builder.Services.AddSnowflakeUniqueId(options => { @@ -38,22 +41,24 @@ builder.Services.AddSnowflakeUniqueId(options => var app = builder.Build(); -app.MapGet("/", (ISnowflakeService snowflakeService, ISnowflakeIdProvider snowflakeIdProvider) => +app.MapGet("/", (ISnowflakeService snowflakeService) => { long generatingId = snowflakeService.GenerateSnowflakeId(); - SnowflakeIdResult sn = snowflakeIdProvider.GetDateTimeBySnowflakeId(generatingId); + DateTime generatedAt = snowflakeService.GetGeneratedDateTimeBySnowflakeId(generatingId); + int dataCenterId = snowflakeService.GetDataCenterIdBySnowflakId(generatingId); - return $"The genrated Id is: { generatingId } - and is genrated at { sn.GeneratedDateTime }"; + return $"The genrated Id is: { generatingId } - and is genrated at: { generatedAt } - at Data Center Id: {dataCenterId}"; }); app.Run(); ``` + And here is the result if you run the app: -![aspsnow](https://user-images.githubusercontent.com/18530495/210797780-f69c14c8-7158-4daa-bba0-36b313852026.JPG) +![image](https://github.com/Shoogn/SnowflakeId/assets/18530495/6d05dcd7-4a87-4fb9-86a7-bfd79aca7c80) --- -### With Console Application +### With Console Application Or any other .NET application: ```C# using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;