Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(idgenerator): Add IIdGenerator And Add MasaApp #188

Merged
merged 22 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b0bddb9
refactor(IdGenerator): Refactored id generator
zhenlei520 Aug 8, 2022
ae6dfd6
refactor(Mapping): Refactoring Mapper
zhenlei520 Aug 8, 2022
6af4057
refactor(Caller): Delete IsDefault
zhenlei520 Aug 8, 2022
3f227f1
chore: delete Masa.BuildingBlocks.Data.Contracts.DataFiltering and Ma…
zhenlei520 Aug 9, 2022
0750f94
feat: Initialize RootServices
zhenlei520 Aug 9, 2022
4de06bb
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Aug 9, 2022
e57607c
chore: use global using
zhenlei520 Aug 9, 2022
fe42140
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Aug 9, 2022
45330e5
style: format code
zhenlei520 Aug 9, 2022
9908e26
fix: Fix Caller bug
zhenlei520 Aug 9, 2022
6e8def6
chore: Add IdGeneratorCore
zhenlei520 Aug 9, 2022
fe2fdc9
refactor: Modify MasaApp
zhenlei520 Aug 9, 2022
4c9bbce
test: Fix UnitTest Error
zhenlei520 Aug 9, 2022
652174d
Update pr_run_test_ci.yml
doddgu Aug 9, 2022
d3c93dd
Merge branch 'main' into feature/idgenerator
doddgu Aug 9, 2022
43b71c6
chore: format code
zhenlei520 Aug 9, 2022
b20ac0c
Merge branch 'feature/idgenerator' of https://github.com/masastack/MA…
zhenlei520 Aug 9, 2022
74094a8
test: Supplemental unit tests
zhenlei520 Aug 9, 2022
6b41b60
test: Improve unit tests
zhenlei520 Aug 10, 2022
459312d
ci: Change the Elasticsearch startup order
zhenlei520 Aug 10, 2022
8c21f54
test: Improve unit tests
zhenlei520 Aug 10, 2022
4e884b4
Merge branch 'feature/idgenerator' of https://github.com/masastack/MA…
zhenlei520 Aug 10, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions .github/workflows/pr_run_test_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ jobs:
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144

- name: Start Elasticsearch
uses: everpcpc/elasticsearch-action@v2
with:
version: 7.6.1
plugins: |
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch-analysis-ik-7.6.1.zip
https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.6.1/elasticsearch-analysis-pinyin-7.6.1.zip
analysis-icu
analysis-smartcn
analysis-kuromoji

- name: Set up JDK 11
uses: actions/setup-java@v1
with:
Expand Down Expand Up @@ -71,8 +59,16 @@ jobs:
dotnet-sonarscanner begin /k:"masastack_MASA.Framework" /o:"masastack" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
dotnet build /p:ContinuousIntegrationBuild=true
dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

- name: Start Elasticsearch
uses: everpcpc/elasticsearch-action@v2
with:
version: 7.6.1
plugins: |
https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch-analysis-ik-7.6.1.zip
https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.6.1/elasticsearch-analysis-pinyin-7.6.1.zip
analysis-icu
analysis-smartcn
analysis-kuromoji
- name: test
run: dotnet test --filter FullyQualifiedName!~Masa.Utils --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[*.Tests]*"


11 changes: 11 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.IdGenerat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Framework.IntegrationTests.EventBus", "test\Masa.Framework.IntegrationTests.EventBus\Masa.Framework.IntegrationTests.EventBus.csproj", "{A9EED08B-6C1E-4409-9874-AA7D34102519}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Data.IdGenerator.NormalGuid.Tests", "src\Contrib\Data\IdGenerator\NormalGuid\Masa.Contrib.Data.IdGenerator.NormalGuid.Tests\Masa.Contrib.Data.IdGenerator.NormalGuid.Tests.csproj", "{C488CAC3-6F06-4D29-B449-93CEF482C4D7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1843,6 +1845,14 @@ Global
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|Any CPU.Build.0 = Release|Any CPU
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|x64.ActiveCfg = Release|Any CPU
{A9EED08B-6C1E-4409-9874-AA7D34102519}.Release|x64.Build.0 = Release|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Debug|x64.ActiveCfg = Debug|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Debug|x64.Build.0 = Debug|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Release|Any CPU.Build.0 = Release|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Release|x64.ActiveCfg = Release|Any CPU
{C488CAC3-6F06-4D29-B449-93CEF482C4D7}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2100,6 +2110,7 @@ Global
{B37640C5-3F96-4363-915C-0C2848004A05} = {55DCA34D-48CE-4D32-8515-B6583FA548AE}
{9EA05A47-B28A-4CD1-8058-00A2098ED0C2} = {55DCA34D-48CE-4D32-8515-B6583FA548AE}
{A9EED08B-6C1E-4409-9874-AA7D34102519} = {E747043D-81E2-4A89-8B5B-1258ED45F941}
{C488CAC3-6F06-4D29-B449-93CEF482C4D7} = {4515CCA0-E8FC-4FCF-93E1-B812F5949371}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
<ProjectReference Include="..\Masa.BuildingBlocks.Data.Mapping\Masa.BuildingBlocks.Data.Mapping.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ namespace System;

public static class ObjectExtensions
{
private static Mapper? _mapper;
private static Mapper GetMapper() => _mapper ??= Mapper.Instance ?? throw new Exception("Please use MapperFactory to initialize Mapper");
private static IMapper? _mapper;

private static IMapper GetMapper()
=> _mapper ??= MasaApp.GetService<IMapper>() ?? throw new Exception("Please use MapperFactory to initialize Mapper");

public static TDestination Map<TSource, TDestination>(this TSource source, MapOptions? options = null)
=> GetMapper().Map<TSource, TDestination>(source, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Data.Mapping;
global using Masa.BuildingBlocks.Data.Mapping.Options;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data.Contracts.DataFiltering;
namespace Masa.BuildingBlocks.Data.Contracts;

public interface IDataFilter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data.Contracts.DataFiltering;
namespace Masa.BuildingBlocks.Data.Contracts;

public interface ISoftDelete
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data.Contracts;

public class MasaFactoryOptions<TRelationOptions> where TRelationOptions : MasaRelationOptions
{
public List<TRelationOptions> Options { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data.Contracts;

public class MasaRelationOptions
{
public string Name { get; protected set; }
}

public class MasaRelationOptions<TService> : MasaRelationOptions
where TService : class
{
public Func<IServiceProvider, TService> Func { get; set; }

public MasaRelationOptions(string name)
{
Name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data.Contracts.Paginated;
namespace Masa.BuildingBlocks.Data.Contracts;

public class BasePaginatedList<TEntity>
where TEntity : class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddIdGeneratorCore(this IServiceCollection services)
{
services.AddSingleton<IIdGeneratorFactory, DefaultIdGeneratorFactory>();
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data;

public abstract class BaseIdGenerator<TOut> : IIdGenerator<TOut>
where TOut : notnull
{
public abstract TOut NewId();

public virtual string NewStringId() => NewId().ToString()!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data;

public class DefaultIdGeneratorFactory : IIdGeneratorFactory
{
private readonly IServiceProvider _serviceProvider;
private readonly IOptions<IdGeneratorFactoryOptions> _idGeneratorFactoryOptions;
private readonly IdGeneratorRelationOptions? _defaultIdGeneratorOptions;
private IGuidGenerator? _guidGenerator;
private ISequentialGuidGenerator? _sequentialGuidGenerator;
private ISnowflakeGenerator? _snowflakeGenerator;

public IGuidGenerator GuidGenerator => _guidGenerator ??=
_serviceProvider.GetService<IGuidGenerator>() ?? throw new Exception($"Unsupported {nameof(GuidGenerator)}");

public ISequentialGuidGenerator SequentialGuidGenerator => _sequentialGuidGenerator ??=
_serviceProvider.GetService<ISequentialGuidGenerator>() ?? throw new Exception($"Unsupported {nameof(SequentialGuidGenerator)}");

public ISnowflakeGenerator SnowflakeGenerator => _snowflakeGenerator ??=
_serviceProvider.GetService<ISnowflakeGenerator>() ?? throw new Exception($"Unsupported {nameof(SnowflakeGenerator)}");

public DefaultIdGeneratorFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_idGeneratorFactoryOptions = serviceProvider.GetRequiredService<IOptions<IdGeneratorFactoryOptions>>();
_defaultIdGeneratorOptions =
_idGeneratorFactoryOptions.Value.Options.FirstOrDefault(generator
=> generator.Name == Microsoft.Extensions.Options.Options.DefaultName) ??
_idGeneratorFactoryOptions.Value.Options.FirstOrDefault();
}

public IIdGenerator<TOut> Create<TOut>() where TOut : notnull
{
var idGenerator = Create();
return idGenerator as IIdGenerator<TOut> ?? throw new Exception($"Unsupported {nameof(IIdGenerator<TOut>)}");
}

public IIdGenerator<TOut> Create<TOut>(string name) where TOut : notnull
{
var idGenerator = Create(name);
return idGenerator as IIdGenerator<TOut> ?? throw new Exception($"Unsupported {nameof(IIdGenerator<TOut>)}");
}

public IIdGenerator Create()
{
if (_defaultIdGeneratorOptions == null)
throw new NotImplementedException("No default IdGenerator found, you may need service.AddSimpleGuidGenerator()");

return _defaultIdGeneratorOptions.Func.Invoke(_serviceProvider);
}

public IIdGenerator Create(string name)
{
var idGeneratorOptions = _idGeneratorFactoryOptions.Value.Options.FirstOrDefault(generator => generator.Name == name);
if (idGeneratorOptions == null)
throw new NotImplementedException($"No IdGenerator found for name {name}");

return idGeneratorOptions.Func.Invoke(_serviceProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Masa.BuildingBlocks.Data;

public interface IGuidGenerator : IIdGenerator<Guid, Guid>
public interface IGuidGenerator : IIdGenerator<Guid>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

namespace Masa.BuildingBlocks.Data;

public interface IIdGenerator<in T,out TOut>
where T : notnull
public interface IIdGenerator
{
string NewStringId();
}

public interface IIdGenerator<out TOut>: IIdGenerator
where TOut : notnull
{
public TOut NewId();
TOut NewId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data;

public interface IIdGeneratorFactory
{
IGuidGenerator GuidGenerator { get; }

ISequentialGuidGenerator SequentialGuidGenerator { get; }

ISnowflakeGenerator SnowflakeGenerator { get; }

IIdGenerator<TOut> Create<TOut>() where TOut : notnull;

IIdGenerator<TOut> Create<TOut>(string name) where TOut : notnull;

IIdGenerator Create();

IIdGenerator Create(string name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Masa.BuildingBlocks.Data;

public interface ISequentialGuidGenerator : IIdGenerator<SequentialGuid, Guid>
public interface ISequentialGuidGenerator : IIdGenerator<Guid>
{
Guid Create(SequentialGuidType guidType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Masa.BuildingBlocks.Data;

public interface ISnowflakeGenerator : IIdGenerator<Snowflake, long>
public interface ISnowflakeGenerator : IIdGenerator<long>
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,20 @@

namespace Masa.BuildingBlocks.Data;

public class IdGeneratorFactory
public static class IdGeneratorFactory
{
private static IGuidGenerator? _guidGenerator;
public static IGuidGenerator GuidGenerator => _guidGenerator ?? throw new Exception($"Unsupported {nameof(GuidGenerator)}");

private static ISequentialGuidGenerator? _sequentialGuidGenerator;
public static ISequentialGuidGenerator SequentialGuidGenerator
=> _sequentialGuidGenerator ?? throw new Exception($"Unsupported {nameof(SequentialGuidGenerator)}");

private static ISnowflakeGenerator? _snowflakeGenerator;
public static ISnowflakeGenerator SnowflakeGenerator
=> _snowflakeGenerator ?? throw new Exception($"Unsupported {nameof(SnowflakeGenerator)}");

public static void SetGuidGenerator(IGuidGenerator guidGenerator)
{
ArgumentNullException.ThrowIfNull(guidGenerator, nameof(guidGenerator));
_guidGenerator = guidGenerator;
}
public static IGuidGenerator GuidGenerator => _guidGenerator ??= MasaApp.GetRequiredService<IGuidGenerator>() ??
throw new Exception($"Unsupported {nameof(GuidGenerator)}");

public static void SetSequentialGuidGenerator(ISequentialGuidGenerator sequentialGuidGenerator)
{
ArgumentNullException.ThrowIfNull(sequentialGuidGenerator, nameof(sequentialGuidGenerator));
_sequentialGuidGenerator = sequentialGuidGenerator;
}
public static ISequentialGuidGenerator SequentialGuidGenerator
=> _sequentialGuidGenerator ??= MasaApp.GetRequiredService<ISequentialGuidGenerator>() ??
throw new Exception($"Unsupported {nameof(SequentialGuidGenerator)}");

public static void SetSnowflakeGenerator(ISnowflakeGenerator snowflakeGenerator)
{
ArgumentNullException.ThrowIfNull(snowflakeGenerator, nameof(snowflakeGenerator));
_snowflakeGenerator = snowflakeGenerator;
}
public static ISnowflakeGenerator SnowflakeGenerator
=> _snowflakeGenerator ??= MasaApp.GetRequiredService<ISnowflakeGenerator>() ??
throw new Exception($"Unsupported {nameof(SnowflakeGenerator)}");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace System;
namespace Masa.BuildingBlocks.Data;

public readonly struct SequentialGuid
public class IdGeneratorFactoryOptions : MasaFactoryOptions<IdGeneratorRelationOptions>
{

}
Loading