-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix build problem * - Use static class instead of singleton pattern - Add SetSeed Method - Remove extra dependencies * Update Test * Updated Readme Adding Comments
- Loading branch information
Showing
9 changed files
with
121 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
|
||
namespace SequentialGuid | ||
{ | ||
/// <summary> | ||
/// Generate Guids in Sequential order | ||
/// First Guid will be 00000000-0000-0000-0000-000000000001 | ||
/// Use Set Seed to Seed First Guid | ||
/// Prefered Way to do : | ||
/// <code> | ||
/// SetSeed(Guid.NewGuid()); | ||
/// </code> | ||
/// </summary> | ||
public static class GuidInstance | ||
{ | ||
private static int[]? _sqlOrderMap; | ||
private static int[] SqlOrderMap | ||
{ | ||
get | ||
{ | ||
_sqlOrderMap ??= new int[16] { | ||
3, 2, 1, 0, 5, 4, 7, 6, 9, 8, 15, 14, 13, 12, 11, 10 | ||
}; | ||
return _sqlOrderMap; | ||
} | ||
} | ||
|
||
private static Guid _currentGuid; | ||
static GuidInstance() | ||
{ | ||
_currentGuid = new Guid("00000000-0000-0000-0000-000000000001"); | ||
} | ||
/// <summary> | ||
/// Set Starting Point for Guid Generation | ||
/// </summary> | ||
/// <param name="guid"></param> | ||
public static void SetSeed(Guid guid) | ||
{ | ||
_currentGuid = new Guid(guid.ToByteArray()); | ||
} | ||
/// <summary> | ||
/// Returns Current Guid (Last Generated) | ||
/// </summary> | ||
/// <returns>Current Guid</returns> | ||
public static Guid GetCurrentGuid() | ||
{ | ||
return _currentGuid; | ||
} | ||
/// <summary> | ||
/// Generate Next Guid | ||
/// </summary> | ||
/// <returns>Next Guid</returns> | ||
public static Guid Next() | ||
{ | ||
byte[] bytes = _currentGuid.ToByteArray(); | ||
for (int mapIndex = 0; mapIndex < 16; mapIndex++) | ||
{ | ||
int bytesIndex = SqlOrderMap[mapIndex]; | ||
bytes[bytesIndex]++; | ||
if (bytes[bytesIndex] != 0) | ||
{ | ||
break; // No need to increment more significant bytes | ||
} | ||
} | ||
_currentGuid = new Guid(bytes); | ||
return _currentGuid; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SequentialGuid\SequentialGuid.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\SequentialGuid\SequentialGuid.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,42 @@ | ||
using SequentialGuid; | ||
|
||
namespace SequentialGuidTests | ||
{ | ||
[TestClass] | ||
public class UnitTest | ||
{ | ||
private readonly ISequentialGuid _sequenctialGuid; | ||
public UnitTest() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddSequentialGuid(); | ||
var serviceProvider = services.BuildServiceProvider(); | ||
_sequenctialGuid = serviceProvider.GetService<ISequentialGuid>()!; | ||
} | ||
[TestMethod] | ||
public void Sequential_Guid_Should_Not_Be_Null() | ||
{ | ||
Assert.IsNotNull(_sequenctialGuid); | ||
} | ||
|
||
[TestMethod] | ||
public void Cureent_Guid_Should_Not_Be_Null() | ||
{ | ||
Assert.IsNotNull(_sequenctialGuid.GetCurrentGuid()); | ||
Assert.IsNotNull(GuidInstance.GetCurrentGuid()); | ||
} | ||
|
||
[TestMethod] | ||
public void Next_Guid_Should_Not_Be_Null() | ||
{ | ||
Assert.IsNotNull(_sequenctialGuid.Next()); | ||
Assert.IsNotNull(GuidInstance.Next()); | ||
} | ||
[TestMethod] | ||
public void Next_Guid_Should_Be_Greater() | ||
{ | ||
var current = _sequenctialGuid.GetCurrentGuid(); | ||
var next = _sequenctialGuid.Next(); | ||
var current = GuidInstance.GetCurrentGuid(); | ||
var next = GuidInstance.Next(); | ||
Assert.IsTrue(next.CompareTo(current) > 0); | ||
} | ||
|
||
[TestMethod] | ||
public void Check_Seed_Method() | ||
{ | ||
Guid current = GuidInstance.GetCurrentGuid(); | ||
Guid next = Guid.NewGuid(); | ||
GuidInstance.SetSeed(next); | ||
Guid NewCurrnet = GuidInstance.GetCurrentGuid(); | ||
Assert.IsNotNull(NewCurrnet); | ||
Assert.IsTrue(next.CompareTo(NewCurrnet) == 0); | ||
Assert.IsTrue(current.CompareTo(NewCurrnet) != 0); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
global using Microsoft.Extensions.DependencyInjection; | ||
global using Microsoft.VisualStudio.TestTools.UnitTesting; |