Files
xDataService/Providers/XSequentialGuid.cs
T
2026-05-04 00:37:06 +03:30

71 lines
1.9 KiB
C#

using System;
using xDataService.Interfaces;
namespace xDataService.Helpers
{
/// <summary>
/// this service provide Sequential GUID mechanism for Entity Ids ...
/// </summary>
public class XSequentialGuid : IXSequentialGuid
{
private static int[] sqlOrderMap = null;
private static int[] SQLORDERMAP
{
get
{
if (sqlOrderMap == null)
{
sqlOrderMap = new int[16] {
3,
2,
1,
0,
5,
4,
7,
6,
9,
8,
15,
14,
13,
12,
11,
10
};
// 3 - the least significant byte in Guid ByteArray [for SQL Server ORDER BY clause]
// 10 - the most significant byte in Guid ByteArray [for SQL Server ORDERY BY clause]
}
return sqlOrderMap;
}
}
private Guid currentGuid;
public XSequentialGuid()
{
currentGuid = Guid.NewGuid();
}
public Guid GetCurrentGuid()
{
return currentGuid;
}
public 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;
}
}
}