From cf2af34a57db3d766fad971e0de435f1947b0fef Mon Sep 17 00:00:00 2001 From: Mohammed Ahmed Hussien Date: Fri, 12 Dec 2025 08:41:45 +0300 Subject: [PATCH] Support net10.0 and add a new API registration to register the snowflakeId and read the options from the configuration file --- .../SnowflakeId.AspNetCoreSample/Program.cs | 5 +-- .../SnowflakeId.AspNetCoreSample.csproj | 2 +- .../appsettings.json | 5 +++ .../SnowflakeId.Example.csproj | 6 +-- .../SnowflakeIdServiceExtensions.cs | 21 +++++++++ src/core/SnowflakeId.Core.csproj | 7 ++- tests/SnowflakeId.Tests.csproj | 8 +++- tests/SnowflakeIdServiceExtensionsTest.cs | 44 +++++++++++++++++-- 8 files changed, 83 insertions(+), 15 deletions(-) diff --git a/example/SnowflakeId.AspNetCoreSample/Program.cs b/example/SnowflakeId.AspNetCoreSample/Program.cs index 609fee0..22c12fe 100644 --- a/example/SnowflakeId.AspNetCoreSample/Program.cs +++ b/example/SnowflakeId.AspNetCoreSample/Program.cs @@ -4,10 +4,7 @@ using System; var builder = WebApplication.CreateBuilder(args); -builder.Services.AddSnowflakeUniqueId(options => -{ - options.DataCenterId = 1; -}); +builder.Services.AddSnowflakeUniqueId(builder.Configuration); var app = builder.Build(); diff --git a/example/SnowflakeId.AspNetCoreSample/SnowflakeId.AspNetCoreSample.csproj b/example/SnowflakeId.AspNetCoreSample/SnowflakeId.AspNetCoreSample.csproj index 60f9e15..529f1d7 100644 --- a/example/SnowflakeId.AspNetCoreSample/SnowflakeId.AspNetCoreSample.csproj +++ b/example/SnowflakeId.AspNetCoreSample/SnowflakeId.AspNetCoreSample.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 disable disable diff --git a/example/SnowflakeId.AspNetCoreSample/appsettings.json b/example/SnowflakeId.AspNetCoreSample/appsettings.json index 10f68b8..dacab7a 100644 --- a/example/SnowflakeId.AspNetCoreSample/appsettings.json +++ b/example/SnowflakeId.AspNetCoreSample/appsettings.json @@ -5,5 +5,10 @@ "Microsoft.AspNetCore": "Warning" } }, + "SnowflakeId": { + "DataCenterId": 1, + "UseConsoleLog": false, + "CustomEpoch": "2025-01-01T00:00:00Z" + }, "AllowedHosts": "*" } diff --git a/example/SnowflakeId.Example/SnowflakeId.Example.csproj b/example/SnowflakeId.Example/SnowflakeId.Example.csproj index 5db1e24..f916594 100644 --- a/example/SnowflakeId.Example/SnowflakeId.Example.csproj +++ b/example/SnowflakeId.Example/SnowflakeId.Example.csproj @@ -2,14 +2,14 @@ Exe - net9.0 + net10.0 disable disable - - + + diff --git a/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs b/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs index 22f6863..31f9d03 100644 --- a/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs +++ b/src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection; using SnowflakeId.Core; +using Microsoft.Extensions.Configuration; namespace SnowflakeId.DependencyInjection { @@ -27,5 +28,25 @@ public static IServiceCollection AddSnowflakeUniqueId(this IServiceCollection se services.Configure(setupAction); return services; } + + + public static IServiceCollection AddSnowflakeUniqueId(this IServiceCollection services, + IConfiguration configuration) + { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + services.AddOptions(); + var snowflakeIdSection = configuration.GetSection("SnowflakeId"); + if (!snowflakeIdSection.Exists()) + { + throw new ArgumentException("The SnowflakeId section is not found in the configuration."); + } + services.Configure(snowflakeIdSection); + services.TryAddScoped(); + return services; + } + } } diff --git a/src/core/SnowflakeId.Core.csproj b/src/core/SnowflakeId.Core.csproj index 8d477d2..1fa7c7a 100644 --- a/src/core/SnowflakeId.Core.csproj +++ b/src/core/SnowflakeId.Core.csproj @@ -1,6 +1,6 @@  - net6.0;net7.0;net8.0;net9.0 + net6.0;net7.0;net8.0;net9.0;net10.0 disable disable Hussien.SnowflakeId @@ -8,7 +8,7 @@ Mohammed Ahmed Hussien https://github.com/Shoogn/SnowflakeId https://github.com/Shoogn/SnowflakeId - 3.2.0 + 3.3.0 git SnowflakeId, UniqueSnowflakeId, UniqueId, Primary Keys en-SD @@ -23,6 +23,7 @@ 7.0.0 8.0.0 9.0.0 + 10.0.0 @@ -30,6 +31,7 @@ $(Net7Version) $(Net8Version) $(Net9Version) + $(Net10Version) @@ -37,6 +39,7 @@ + diff --git a/tests/SnowflakeId.Tests.csproj b/tests/SnowflakeId.Tests.csproj index 7a76a18..6f0658e 100644 --- a/tests/SnowflakeId.Tests.csproj +++ b/tests/SnowflakeId.Tests.csproj @@ -1,7 +1,7 @@  - net6.0;net7.0;net8.0;net9.0 + net6.0;net7.0;net8.0;net9.0;net10.0 enable false @@ -13,6 +13,7 @@ 7.0.0 8.0.0 9.0.0 + 10.0.0 @@ -20,13 +21,13 @@ $(Net7Version) $(Net8Version) $(Net9Version) + $(Net10Version) - @@ -37,6 +38,9 @@ + + + diff --git a/tests/SnowflakeIdServiceExtensionsTest.cs b/tests/SnowflakeIdServiceExtensionsTest.cs index 2d0f21f..7d2d054 100644 --- a/tests/SnowflakeIdServiceExtensionsTest.cs +++ b/tests/SnowflakeIdServiceExtensionsTest.cs @@ -6,6 +6,7 @@ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. */ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Moq; @@ -17,7 +18,7 @@ namespace SnowflakeId.Tests public class SnowflakeIdServiceExtensionsTest { [Fact] - public void Can_Add_SnowflakId_To_Service_Collection_Without_SnowflakeId_Option() + public void AddSnowflakId_Without_SnowflakeIdOption_And_RegistersService() { var services = new ServiceCollection(); services.AddLogging(); @@ -35,7 +36,7 @@ public void Can_Add_SnowflakId_To_Service_Collection_Without_SnowflakeId_Option( } [Fact] - public void Can_Add_SnowflakeId_To_Service_Collections_With_SnowflakOptions() + public void AddSnowflakId_With_SnowflakeIdOption_And_RegistersService() { var services = new ServiceCollection(); services.AddLogging(); @@ -51,7 +52,44 @@ public void Can_Add_SnowflakeId_To_Service_Collections_With_SnowflakOptions() } [Fact] - public void Can_Replace_SnowflakeId_Default_Registration_By_Creating_Object_That_Implement_ISnowflakeService() + public void AddSnowflakId_And_Binding_Configuration_And_RegistersService() + { + var inMemorySettings = new Dictionary + { + {"SnowflakeId:DataCenterId", "3"}, + {"SnowflakeId:UseConsoleLog", "true"}, + {"SnowflakeId:CustomEpoch", "2025-01-01T00:00:00Z"}, + }; + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(inMemorySettings) + .Build(); + var services = new ServiceCollection(); + services.AddLogging(); + services.AddSnowflakeUniqueId(configuration); + var serviceProvider = services.BuildServiceProvider(); + var snowflakeService = serviceProvider.GetRequiredService(); + var snowflakeIdOptionGetter = serviceProvider.GetRequiredService>(); + var snowflakeIdOption = snowflakeIdOptionGetter.Value; + Assert.NotNull(snowflakeService); + Assert.Equal(3, snowflakeIdOption.DataCenterId); + Assert.True(snowflakeIdOption.UseConsoleLog); + Assert.Equal(DateTime.Parse("2025-01-01T00:00:00Z").ToUniversalTime(), snowflakeIdOption.CustomEpoch?.ToUniversalTime()); + + } + + [Fact] + public void AddSnowflakId_Throe_When_Missing_Binding_Configuration() + { + // empty config intentionally + IConfiguration config = new ConfigurationBuilder().Build(); + var services = new ServiceCollection(); + + var ex = Assert.Throws(() => services.AddSnowflakeUniqueId(config)); + Assert.Contains("SnowflakeId", ex.Message, StringComparison.OrdinalIgnoreCase); + } + + [Fact] + public void Replace_SnowflakeId_Default_Registration_By_Creating_Object_That_Implement_ISnowflakeService() { var services = new ServiceCollection(); services.AddLogging();