From 85c04592daee6de136e550c4fb70b196d94ef5c5 Mon Sep 17 00:00:00 2001 From: Nice <2017875139@qq.com> Date: Thu, 21 Apr 2022 13:50:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A3=98=E9=80=B8=E9=9B=AA?= =?UTF-8?q?=E8=8A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easy.Snowflakes/Easy.Snowflakes.csproj | 20 ++++++++++++++++ .../ServiceCollectionServiceExtensions.cs | 16 +++++++++++++ .../DriftingSnowflakeIdGenerator.cs | 24 +++++++++++++++++++ .../Easy/Snowflakes/ISnowflakeIdGenerator.cs | 5 ++++ Easy.sln | 7 ++++++ src/Easy.Uow/Easy.UnitOfWork.csproj | 2 +- .../ServiceCollectionServiceExtensions.cs | 3 +-- 7 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Easy.Snowflakes/Easy.Snowflakes.csproj create mode 100644 Easy.Snowflakes/Easy/Extensions/ServiceCollectionServiceExtensions.cs create mode 100644 Easy.Snowflakes/Easy/Realization/DriftingSnowflakeIdGenerator.cs create mode 100644 Easy.Snowflakes/Easy/Snowflakes/ISnowflakeIdGenerator.cs diff --git a/Easy.Snowflakes/Easy.Snowflakes.csproj b/Easy.Snowflakes/Easy.Snowflakes.csproj new file mode 100644 index 0000000..4af1623 --- /dev/null +++ b/Easy.Snowflakes/Easy.Snowflakes.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + True + 1.1.0 + + + + + + + + + + + + + diff --git a/Easy.Snowflakes/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/Easy.Snowflakes/Easy/Extensions/ServiceCollectionServiceExtensions.cs new file mode 100644 index 0000000..5a01a41 --- /dev/null +++ b/Easy.Snowflakes/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -0,0 +1,16 @@ +using Easy.Snowflakes; +using Microsoft.Extensions.DependencyInjection; + +namespace Easy.Extensions; +public static class ServiceCollectionServiceExtensions +{ + public static IServiceCollection AddDriftingSnowflakes(this IServiceCollection services, ushort workId) + { + services.AddSingleton(typeof(ISnowflakeIdGenerator), o => + { + return new DriftingSnowflakeIdGenerator(workId); + }); + + return services; + } +} \ No newline at end of file diff --git a/Easy.Snowflakes/Easy/Realization/DriftingSnowflakeIdGenerator.cs b/Easy.Snowflakes/Easy/Realization/DriftingSnowflakeIdGenerator.cs new file mode 100644 index 0000000..f1d509a --- /dev/null +++ b/Easy.Snowflakes/Easy/Realization/DriftingSnowflakeIdGenerator.cs @@ -0,0 +1,24 @@ +using Yitter.IdGenerator; + +namespace Easy.Snowflakes; +public class DriftingSnowflakeIdGenerator : ISnowflakeIdGenerator +{ + private readonly IIdGenerator Generator; + public DriftingSnowflakeIdGenerator(ushort workerId) + { + Generator = new DefaultIdGenerator(new IdGeneratorOptions() + { + WorkerId = workerId, + BaseTime = DateTime.Parse("2022-01-01 00:00:00") + }); + } + + /// + /// 生成新的Id + /// + /// + public long Create() + { + return Generator.NewLong(); + } +} \ No newline at end of file diff --git a/Easy.Snowflakes/Easy/Snowflakes/ISnowflakeIdGenerator.cs b/Easy.Snowflakes/Easy/Snowflakes/ISnowflakeIdGenerator.cs new file mode 100644 index 0000000..ecdbd3d --- /dev/null +++ b/Easy.Snowflakes/Easy/Snowflakes/ISnowflakeIdGenerator.cs @@ -0,0 +1,5 @@ +namespace Easy.Snowflakes; +public interface ISnowflakeIdGenerator +{ + long Create(); +} diff --git a/Easy.sln b/Easy.sln index 43b2468..4783aae 100644 --- a/Easy.sln +++ b/Easy.sln @@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthorityManagementCenter", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Easy.DDD.Application", "src\Easy.DDD.Application\Easy.DDD.Application.csproj", "{8D281035-BB53-4377-9386-0CB664A5C6B3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Easy.Snowflakes", "Easy.Snowflakes\Easy.Snowflakes.csproj", "{9D31086B-1333-4745-AEC2-7966141E503C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -105,6 +107,10 @@ Global {8D281035-BB53-4377-9386-0CB664A5C6B3}.Debug|Any CPU.Build.0 = Debug|Any CPU {8D281035-BB53-4377-9386-0CB664A5C6B3}.Release|Any CPU.ActiveCfg = Release|Any CPU {8D281035-BB53-4377-9386-0CB664A5C6B3}.Release|Any CPU.Build.0 = Release|Any CPU + {9D31086B-1333-4745-AEC2-7966141E503C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9D31086B-1333-4745-AEC2-7966141E503C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9D31086B-1333-4745-AEC2-7966141E503C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9D31086B-1333-4745-AEC2-7966141E503C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -128,6 +134,7 @@ Global {3509F9B3-6B41-4E99-B8DE-2A3E3753AFF3} = {3CD21B92-C376-437C-AE85-7D1669AC1A37} {CEE286C3-D107-497B-8D4A-1F1199A40A10} = {F59B534D-D72A-462B-B8B9-CD3A11B21C49} {8D281035-BB53-4377-9386-0CB664A5C6B3} = {A263D7E3-0153-48D0-822D-E3E0B51A4890} + {9D31086B-1333-4745-AEC2-7966141E503C} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7260112B-0232-4725-AB84-EF4DCCABFD77} diff --git a/src/Easy.Uow/Easy.UnitOfWork.csproj b/src/Easy.Uow/Easy.UnitOfWork.csproj index c519647..550010e 100644 --- a/src/Easy.Uow/Easy.UnitOfWork.csproj +++ b/src/Easy.Uow/Easy.UnitOfWork.csproj @@ -6,7 +6,7 @@ True - 1.1.0 + 1.1.1 diff --git a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs index 03a6f87..6dd700a 100644 --- a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -8,8 +8,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Easy.Extensions; public static class ServiceCollectionServiceExtensions { - public static IServiceCollection AddUnitOfWork(this IServiceCollection services, Type unitOfWork) - where TDbContext : DbContext + public static IServiceCollection AddUnitOfWork(this IServiceCollection services, Type unitOfWork) { services.AddScoped(typeof(IUnitOfWork), unitOfWork); services.Configure(options =>