From c637d70feb5831eaa73da7ad7e20e22af9e3d53d Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Fri, 24 Nov 2023 10:11:32 +0300 Subject: [PATCH] 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.