Skip to content

Commit

Permalink
Develop (#3)
Browse files Browse the repository at this point in the history
* 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
knourian authored Jan 24, 2023
1 parent f289f26 commit f30518a
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 113 deletions.
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,36 @@ NuGet Package to generate SQL Server Friendly Sequential Guid

## Usage

Add as Dependency to service collection:
This will register this Package as Singelton
1. Get Next Guid

```csharp
using SequentialGuid;

services.AddSequentialGuid();
public void ExampleMethod()
{
var guid = GuidInstance.Next();
}
```

Add With Dependency Injection to where its needed:
2. Get Current Guid

```csharp
using SequentialGuid;
private readonly ISequentialGuid _sequentialGuid;
public class Example(ISequentialGuid sequentialGuid)

public void ExampleMethod()
{
_sequentialGuid = sequentialGuid;
var guid = GuidInstance.GetCurrentGuid();
}
```


3. Seed First Guid

```csharp
using SequentialGuid;

public void ExampleMethod()
{
var guid = _sequentialGuid.Next();
var guid = GuidInstance.SetSeed(Guid.NewGuid());
}
```
69 changes: 69 additions & 0 deletions SequentialGuid/GuidInstance.cs
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;
}
}
}
10 changes: 0 additions & 10 deletions SequentialGuid/ISequentialGuid.cs

This file was deleted.

44 changes: 0 additions & 44 deletions SequentialGuid/SequentialGuid.cs

This file was deleted.

6 changes: 1 addition & 5 deletions SequentialGuid/SequentialGuid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<RepositoryUrl>https://github.com/knourian/Sequential-Guid</RepositoryUrl>
<PackageId>Knourian.$(AssemblyName)</PackageId>
<Version>1.0.1</Version>
<Version>2.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,8 +23,4 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.1.22" />
</ItemGroup>

</Project>
13 changes: 0 additions & 13 deletions SequentialGuid/ServiceProvider.cs

This file was deleted.

31 changes: 15 additions & 16 deletions SequentialGuidTests/SequentialGuidTests.csproj
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>
34 changes: 18 additions & 16 deletions SequentialGuidTests/UnitTest.cs
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);
}
}
}
}
1 change: 0 additions & 1 deletion SequentialGuidTests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.VisualStudio.TestTools.UnitTesting;

0 comments on commit f30518a

Please sign in to comment.