Skip to content

Commit 577791d

Browse files
authored
refactor: Refactoring IdGenerator (#574)
* refactor: Refactoring IdGenerator * chore: Dealing with Code Smells * rename: TestGetDistibutedLockFaieldAsync -> TestGetDistributedLockFailedAsync * test: Update UnitTest
1 parent c5a108d commit 577791d

File tree

21 files changed

+201
-407
lines changed

21 files changed

+201
-407
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,25 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
// ReSharper disable once CheckNamespace
5+
46
namespace Masa.BuildingBlocks.Data;
57

6-
public class DefaultIdGeneratorFactory : MasaFactoryBase<IIdGenerator, IdGeneratorRelationOptions>,
7-
IIdGeneratorFactory
8+
public class DefaultIdGeneratorFactory : IIdGeneratorFactory
89
{
9-
private IGuidGenerator? _guidGenerator;
10-
private ISequentialGuidGenerator? _sequentialGuidGenerator;
11-
private ISnowflakeGenerator? _snowflakeGenerator;
12-
13-
public IGuidGenerator GuidGenerator => _guidGenerator ??=
14-
ServiceProvider.GetService<IGuidGenerator>() ?? throw new Exception($"Unsupported {nameof(GuidGenerator)}");
15-
16-
public ISequentialGuidGenerator SequentialGuidGenerator => _sequentialGuidGenerator ??=
17-
ServiceProvider.GetService<ISequentialGuidGenerator>() ?? throw new Exception($"Unsupported {nameof(SequentialGuidGenerator)}");
18-
19-
public ISnowflakeGenerator SnowflakeGenerator => _snowflakeGenerator ??=
20-
ServiceProvider.GetService<ISnowflakeGenerator>() ?? throw new Exception($"Unsupported {nameof(SnowflakeGenerator)}");
21-
22-
protected override string DefaultServiceNotFoundMessage { get; } =
23-
"No default IdGenerator found, you may need service.AddSimpleGuidGenerator()";
24-
25-
protected override string SpecifyServiceNotFoundMessage { get; } =
26-
"Please make sure you have used [{0}] IdGenerator, it was not found";
10+
public IGuidGenerator? GuidGenerator { get; }
2711

28-
protected override MasaFactoryOptions<IdGeneratorRelationOptions> FactoryOptions => _options.CurrentValue;
12+
public ISequentialGuidGenerator? SequentialGuidGenerator { get; }
2913

30-
private readonly IOptionsMonitor<IdGeneratorFactoryOptions> _options;
31-
32-
public DefaultIdGeneratorFactory(IServiceProvider serviceProvider) : base(serviceProvider)
33-
{
34-
_options = serviceProvider.GetRequiredService<IOptionsMonitor<IdGeneratorFactoryOptions>>();
35-
}
36-
37-
public IIdGenerator<TOut> Create<TOut>() where TOut : notnull
38-
{
39-
var idGenerator = Create();
40-
return idGenerator as IIdGenerator<TOut> ?? throw new Exception($"Unsupported {nameof(IIdGenerator<TOut>)}");
41-
}
14+
public ISnowflakeGenerator? SnowflakeGenerator { get; }
4215

43-
public IIdGenerator<TOut> Create<TOut>(string name) where TOut : notnull
16+
public DefaultIdGeneratorFactory(
17+
IGuidGenerator? guidGenerator = null,
18+
ISequentialGuidGenerator? sequentialGuidGenerator = null,
19+
ISnowflakeGenerator? snowflakeGenerator = null)
4420
{
45-
var idGenerator = Create(name);
46-
return idGenerator as IIdGenerator<TOut> ?? throw new Exception($"Unsupported {nameof(IIdGenerator<TOut>)}");
21+
GuidGenerator = guidGenerator;
22+
SequentialGuidGenerator = sequentialGuidGenerator;
23+
SnowflakeGenerator = snowflakeGenerator;
4724
}
4825
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

4+
[assembly: InternalsVisibleTo("Masa.Contrib.Data.IdGenerator.NormalGuid")]
5+
[assembly: InternalsVisibleTo("Masa.Contrib.Data.IdGenerator.SequentialGuid")]
6+
[assembly: InternalsVisibleTo("Masa.Contrib.Data.IdGenerator.Snowflake")]
7+
48
// ReSharper disable once CheckNamespace
9+
510
namespace Microsoft.Extensions.DependencyInjection;
611

712
public static partial class ServiceCollectionExtensions
813
{
9-
public static IServiceCollection AddIdGeneratorCore(this IServiceCollection services)
14+
public static IServiceCollection AddIdGenerator(this IServiceCollection services, Action<IdGeneratorOptions> configure)
1015
{
11-
services.AddSingleton<IIdGeneratorFactory, DefaultIdGeneratorFactory>();
16+
services.TryAddSingleton<IIdGeneratorFactory, DefaultIdGeneratorFactory>();
17+
18+
var idGeneratorOptions = new IdGeneratorOptions(services);
19+
configure.Invoke(idGeneratorOptions);
20+
MasaApp.TrySetServiceCollection(services);
1221
return services;
1322
}
1423
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IdGenerator/IIdGeneratorFactory.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33

44
namespace Masa.BuildingBlocks.Data;
55

6-
public interface IIdGeneratorFactory : IMasaFactory<IIdGenerator>
6+
public interface IIdGeneratorFactory
77
{
8-
IGuidGenerator GuidGenerator { get; }
8+
IGuidGenerator? GuidGenerator { get; }
99

10-
ISequentialGuidGenerator SequentialGuidGenerator { get; }
10+
ISequentialGuidGenerator? SequentialGuidGenerator { get; }
1111

12-
ISnowflakeGenerator SnowflakeGenerator { get; }
13-
14-
IIdGenerator<TOut> Create<TOut>() where TOut : notnull;
15-
16-
IIdGenerator<TOut> Create<TOut>(string name) where TOut : notnull;
12+
ISnowflakeGenerator? SnowflakeGenerator { get; }
1713
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IdGenerator/Options/IdGeneratorRelationOptions.cs src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IdGenerator/IdGeneratorOptions.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
namespace Masa.BuildingBlocks.Data;
55

6-
public class IdGeneratorRelationOptions : MasaRelationOptions<IIdGenerator>
6+
public class IdGeneratorOptions
77
{
8-
public IdGeneratorRelationOptions(string name)
9-
: base(name)
8+
public IServiceCollection Services { get; }
9+
10+
public IdGeneratorOptions(IServiceCollection services)
1011
{
12+
Services = services;
1113
}
1214
}

src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IdGenerator/Options/IdGeneratorFactoryOptions.cs

-8
This file was deleted.

src/Contrib/Data/IdGenerator/NormalGuid/Masa.Contrib.Data.IdGenerator.NormalGuid.Tests/NormalGuidGeneratorTest.cs

+8-40
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Masa.Contrib.Data.IdGenerator.NormalGuid.Tests;
77
public class NormalGuidGeneratorTest
88
{
99
[TestMethod]
10-
public void TestNormarlGuidReturnIdGeneratorIsNotNull()
10+
public void TestNormalGuidReturnIdGeneratorIsNotNull()
1111
{
1212
var services = new ServiceCollection();
1313
services.AddSimpleGuidGenerator();
@@ -17,55 +17,23 @@ public void TestNormarlGuidReturnIdGeneratorIsNotNull()
1717
Assert.IsTrue(idGenerator.GetType() == typeof(NormalGuidGenerator));
1818

1919
Assert.IsNotNull(serviceProvider.GetService<IIdGenerator>());
20-
}
21-
22-
[TestMethod]
23-
public void TestNormarlGuidByMasaAppReturnIdGeneratorIsNotNull()
24-
{
25-
var services = new ServiceCollection();
26-
services.TestAddSimpleGuidGenerator();
27-
var idGenerator = MasaApp.GetService<IIdGenerator<Guid>>();
28-
Assert.IsNotNull(idGenerator);
29-
Assert.IsTrue(idGenerator.GetType() == typeof(NormalGuidGenerator));
3020

31-
Assert.IsNotNull(MasaApp.GetService<IIdGenerator>());
21+
Assert.IsTrue(IdGeneratorFactory.GuidGenerator.GetType() == typeof(NormalGuidGenerator));
3222
}
3323

3424
[TestMethod]
35-
public void TestNormarlGuidByCustomNameReturnIdGeneratorIsNotNull()
25+
public void TestNormalGuidByCustomNameReturnIdGeneratorIsNotNull()
3626
{
3727
var services = new ServiceCollection();
38-
services.TestAddSimpleGuidGenerator("normal");
39-
var idGeneratorFactory = MasaApp.GetService<IIdGeneratorFactory>();
28+
services.AddSimpleGuidGenerator();
29+
var serviceProvider = services.BuildServiceProvider();
30+
var idGeneratorFactory = serviceProvider.GetService<IIdGeneratorFactory>();
4031
Assert.IsNotNull(idGeneratorFactory);
4132

42-
var idGenerator = idGeneratorFactory.Create("normal");
33+
var idGenerator = idGeneratorFactory.GuidGenerator;
4334
Assert.IsNotNull(idGenerator);
4435
Assert.IsTrue(idGenerator.GetType() == typeof(NormalGuidGenerator));
45-
}
46-
47-
[TestMethod]
48-
public void TestNormarlGuidByNameIsNullReturnArgumentNullException()
49-
{
50-
var services = new ServiceCollection();
51-
Assert.ThrowsException<ArgumentNullException>(() => services.TestAddSimpleGuidGenerator(null!));
52-
}
53-
54-
[TestMethod]
55-
public void TestAddMultiSequentialGuidReturnIdGeneratorCountIs1()
56-
{
57-
var services = new ServiceCollection();
58-
services.TestAddSimpleGuidGenerator().TestAddSimpleGuidGenerator();
59-
60-
Assert.IsTrue(services.Count(d => d.ServiceType == typeof(IIdGenerator<Guid>)) == 1);
61-
}
62-
63-
[TestMethod]
64-
public void TestNewId()
65-
{
66-
var services = new ServiceCollection();
67-
services.TestAddSimpleGuidGenerator();
6836

69-
Assert.AreNotEqual(Guid.Empty, MasaApp.GetRequiredService<IIdGenerator<Guid>>().NewId());
37+
Assert.IsTrue(IdGeneratorFactory.GuidGenerator.GetType() == typeof(NormalGuidGenerator));
7038
}
7139
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Masa.BuildingBlocks.Data;
7+
8+
public static class IdGeneratorOptionsExtensions
9+
{
10+
public static void UseSimpleGuidGenerator(this IdGeneratorOptions options)
11+
{
12+
if (options.Services.Any(service => service.ImplementationType == typeof(SimpleGuidGeneratorProvider))) return;
13+
14+
options.Services.AddSingleton<SimpleGuidGeneratorProvider>();
15+
16+
options.Services.AddSingleton<IGuidGenerator, NormalGuidGenerator>();
17+
options.Services.TryAddSingleton<IIdGenerator<Guid>>(serviceProvider => serviceProvider.GetRequiredService<IGuidGenerator>());
18+
options.Services.TryAddSingleton<IIdGenerator>(serviceProvider => serviceProvider.GetRequiredService<IGuidGenerator>());
19+
}
20+
21+
#pragma warning disable S2094
22+
private sealed class SimpleGuidGeneratorProvider
23+
{
24+
}
25+
#pragma warning restore S2094
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Microsoft.Extensions.DependencyInjection;
7+
8+
public static class ServiceCollectionExtensions
9+
{
10+
public static IServiceCollection AddSimpleGuidGenerator(this IServiceCollection services)
11+
=> services.AddIdGenerator(options => options.UseSimpleGuidGenerator());
12+
}

src/Contrib/Data/IdGenerator/NormalGuid/Masa.Contrib.Data.IdGenerator.NormalGuid/ServiceCollectionExtensions.cs

-57
This file was deleted.

src/Contrib/Data/IdGenerator/NormalGuid/Masa.Contrib.Data.IdGenerator.NormalGuid/_Imports.cs

+2
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33

44
global using Masa.BuildingBlocks.Data;
55
global using Masa.Contrib.Data.IdGenerator.NormalGuid;
6+
global using Microsoft.Extensions.DependencyInjection;
7+
global using Microsoft.Extensions.DependencyInjection.Extensions;
68
global using System.Runtime.CompilerServices;

0 commit comments

Comments
 (0)