Browse Source

Added a new `Create` method to `IGuidGenerator` that accepts `SequentialGuidType`.

IGuidGenerator
maliming 1 year ago
parent
commit
11cac1dcc5
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      docs/en/framework/infrastructure/guid-generation.md
  2. 6
      framework/src/Volo.Abp.Guids/Volo/Abp/Guids/IGuidGenerator.cs
  3. 6
      framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SequentialGuidGenerator.cs
  4. 5
      framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SimpleGuidGenerator.cs

2
docs/en/framework/infrastructure/guid-generation.md

@ -24,6 +24,8 @@ So, **never use `Guid.NewGuid()` to create Ids** for your entities!
One good solution to this problem is to generate **sequential GUIDs**, which is provided by the ABP out of the box. `IGuidGenerator` service creates sequential GUIDs (implemented by the `SequentialGuidGenerator` by default). Use `IGuidGenerator.Create()` when you need to manually set Id of an [entity](../architecture/domain-driven-design/entities.md).
Additionally, the `IGuidGenerator` interface includes a `Create(SequentialGuidType guidType)` method that allows specifying the type of sequential GUID to generate. See the [AbpSequentialGuidGeneratorOptions](#AbpSequentialGuidGeneratorOptions) section for more information about the `SequentialGuidType`.
**Example: An entity with GUID primary key and creating the entity**
Assume that you've a `Product` [entity](../architecture/domain-driven-design/entities.md) that has a `Guid` key:

6
framework/src/Volo.Abp.Guids/Volo/Abp/Guids/IGuidGenerator.cs

@ -11,4 +11,10 @@ public interface IGuidGenerator
/// Creates a new <see cref="Guid"/>.
/// </summary>
Guid Create();
/// <summary>
/// Creates a new <see cref="Guid"/>.
/// </summary>
/// <param name="guidType">Sequential Guid type.</param>
Guid Create(SequentialGuidType guidType);
}

6
framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SequentialGuidGenerator.cs

@ -15,19 +15,19 @@ public class SequentialGuidGenerator : IGuidGenerator, ITransientDependency
{
public AbpSequentialGuidGeneratorOptions Options { get; }
private static readonly RandomNumberGenerator RandomNumberGenerator = RandomNumberGenerator.Create();
private readonly static RandomNumberGenerator RandomNumberGenerator = RandomNumberGenerator.Create();
public SequentialGuidGenerator(IOptions<AbpSequentialGuidGeneratorOptions> options)
{
Options = options.Value;
}
public Guid Create()
public virtual Guid Create()
{
return Create(Options.GetDefaultSequentialGuidType());
}
public Guid Create(SequentialGuidType guidType)
public virtual Guid Create(SequentialGuidType guidType)
{
// We start with 16 bytes of cryptographically strong random data.
var randomBytes = new byte[10];

5
framework/src/Volo.Abp.Guids/Volo/Abp/Guids/SimpleGuidGenerator.cs

@ -13,4 +13,9 @@ public class SimpleGuidGenerator : IGuidGenerator
{
return Guid.NewGuid();
}
public virtual Guid Create(SequentialGuidType guidType)
{
return Guid.NewGuid();
}
}

Loading…
Cancel
Save