Skip to content

Commit

Permalink
Support .NET8.0 and Add new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoogn committed Nov 24, 2023
1 parent 654388e commit 459b27e
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 122 deletions.
10 changes: 4 additions & 6 deletions SnowflakeId.AspNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.AspNetCore.Builder;
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeIdResult = SnowflakeId.Provider.SnowflakeId;
using SnowflakeId.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSnowflakeUniqueId(options =>
Expand All @@ -12,13 +10,13 @@

var app = builder.Build();

app.MapGet("/", (ISnowflakeService snowflakeService, ISnowflakeIdProvider snowflakeIdProvider) =>
app.MapGet("/", (ISnowflakeService snowflakeService) =>
{
long generatingId = snowflakeService.GenerateSnowflakeId();
SnowflakeIdResult sn = snowflakeIdProvider.GetDateTimeBySnowflakeId(generatingId);
var generatedAt = snowflakeService.GetGeneratedDateTimeBySnowflakeId(generatingId);
return $"The genrated Id is: { generatingId } - and is genrated at { sn.GeneratedDateTime }";
return $"The genrated Id is: { generatingId } - and is genrated at { generatedAt }";
});

app.Run();
12 changes: 6 additions & 6 deletions example/SnowflakeId.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Build and implemented with love by Mohammed Ahmed Hussien Babiker
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SnowflakeId.Core;
using SnowflakeId.Core.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeId.DependencyInjection;
using System;


Expand All @@ -18,20 +17,21 @@ Build and implemented with love by Mohammed Ahmed Hussien Babiker
{
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}", generatedAt.Id, generatedAt.GeneratedDateTime);
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.ReadLine();
11 changes: 5 additions & 6 deletions src/core/DependencyInjection/SnowflakeIdServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Everyone is permitted to copy and distribute verbatim copies
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.DependencyInjection;
using SnowflakeId.Provider;
using SnowflakeId.Core.Options;
using SnowflakeId.Core;


namespace SnowflakeId.Core.DependencyInjection
namespace SnowflakeId.DependencyInjection
{
public static class SnowflakeIdServiceExtensions
{
public static IServiceCollection AddSnowflakeUniqueId(this IServiceCollection services, Action<SnowflakOptions> setupAction)
public static IServiceCollection AddSnowflakeUniqueId(this IServiceCollection services,
Action<SnowflakOptions> setupAction)
{
if (services == null)
{
Expand All @@ -29,8 +29,7 @@ public static IServiceCollection AddSnowflakeUniqueId(this IServiceCollection se
throw new ArgumentNullException(nameof(setupAction));
}

services.TryAddSingleton<ISnowflakeService, SnowflakeIdService>();
services.TryAddScoped<ISnowflakeIdProvider, SnowflakeIdProvider>();
services.TryAddScoped<ISnowflakeService, SnowflakeIdService>();
services.Configure(setupAction);
return services;
}
Expand Down
19 changes: 0 additions & 19 deletions src/core/ISnowflakeIdProvider.cs

This file was deleted.

44 changes: 44 additions & 0 deletions src/core/ISnowflakeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,54 @@ Everyone is permitted to copy and distribute verbatim copies
*/


using System;

namespace SnowflakeId.Core
{
/// <summary>
/// An abstraction define methods to generate a new unique ids.
/// </summary>
public interface ISnowflakeService
{
/// <summary>
/// Generate new unique number, it's length is 19 digits.
/// </summary>
/// <returns>A new unique number that has a long type.</returns>
long GenerateSnowflakeId();

/// <summary>
/// A method caculated the generate date time for a given generated snowflake id.
/// </summary>
/// <param name="snowflakeId">snowflakeId.</param>
/// <returns>A SnowflakeId object that hold the id and the generated date time.</returns>
SnowflakeId GetSnowflakeById(long snowflakeId);

/// <summary>
/// Methos to return at which time the id is generated.
/// </summary>
/// <param name="snowflakeId">snowflakeId.</param>
/// <returns>The generated date time as a <see cref="DateTime"/> struct.</returns>
DateTime GetGeneratedDateTimeBySnowflakeId(long snowflakeId);

/// <summary>
/// Methos to return at which time the id is generated py passing the seconds Since UnixEpoch.
/// </summary>
/// <param name="secondsSinceUnixEpoch">seconds Since UnixEpoch.</param>
/// <returns>A <see cref="DateTime"/> struct.</returns>
DateTime GetDateTimeBySecondsSinceUnixEpoch(long secondsSinceUnixEpoch);

/// <summary>
/// Method return seconds since the UnixEpoch from a given generated snowflake id.
/// </summary>
/// <param name="snowflakeId">snowflakeId.</param>
/// <returns>The seconds as a long data type.</returns>
long GetSecondsSinceUnixEpochFromId(long snowflakeId);

/// <summary>
/// A method return at which data cennter id the snowflake id is generated.
/// </summary>
/// <param name="snowflakeId">snowflakeId.</param>
/// <returns>Data center id which has int data type.</returns>
int GetDataCenterIdBySnowflakId(long snowflakeId);
}
}
2 changes: 1 addition & 1 deletion src/core/SnowflakOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
*/

namespace SnowflakeId.Core.Options
namespace SnowflakeId.Core
{
public class SnowflakOptions
{
Expand Down
18 changes: 13 additions & 5 deletions src/core/SnowflakeId.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<PackageId>Hussien.SnowflakeId</PackageId>
Expand All @@ -19,15 +19,23 @@
</Description>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
</ItemGroup>


</Project>
2 changes: 1 addition & 1 deletion src/core/SnowflakeId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Everyone is permitted to copy and distribute verbatim copies

using System;

namespace SnowflakeId.Provider
namespace SnowflakeId.Core
{
public class SnowflakeId
{
Expand Down
66 changes: 0 additions & 66 deletions src/core/SnowflakeIdProvider.cs

This file was deleted.

Loading

0 comments on commit 459b27e

Please sign in to comment.