Initial Commit ...
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Interfaces;
|
||||
using xModels.Base;
|
||||
|
||||
namespace xDataService.Providers {
|
||||
public class XGuidKeyGenerator<TEntity> : IXKeyGenerator<TEntity, Guid>
|
||||
where TEntity : XBaseEntity<Guid> {
|
||||
private readonly IXSequentialGuid sequentialGuid;
|
||||
|
||||
public XGuidKeyGenerator (
|
||||
IXSequentialGuid sequentialGuid = null
|
||||
) {
|
||||
this.sequentialGuid = sequentialGuid;
|
||||
}
|
||||
|
||||
public async Task<Guid> GenerateKey (
|
||||
IXBaseRepository<TEntity, Guid> repository
|
||||
) {
|
||||
//
|
||||
await Task.CompletedTask;
|
||||
|
||||
//
|
||||
// Check if provided SequentialGuid ...
|
||||
if (sequentialGuid.IsNull ()) {
|
||||
return Guid.NewGuid ();
|
||||
} else {
|
||||
return sequentialGuid.Next ();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsEmpty (Guid id) {
|
||||
//
|
||||
var result = id.IsNull () || id.IsDefaultGuid ();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Extensions;
|
||||
using xDataService.Interfaces;
|
||||
using xModels.Base;
|
||||
|
||||
namespace xDataService.Providers {
|
||||
public class XIntKeyGenerator<TEntity> : IXKeyGenerator<TEntity, int>
|
||||
where TEntity : XBaseEntity<int> {
|
||||
public async Task<int> GenerateKey (IXBaseRepository<TEntity, int> repository) {
|
||||
//
|
||||
var items = (await repository.GetAllAsync ())
|
||||
.OrderByDescending (nameof (XBaseEntity<int>.Id));
|
||||
var last = items
|
||||
.FirstOrDefault ();
|
||||
|
||||
//
|
||||
var result = last.IsNull () ? 1 : last.Id + 1;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool IsEmpty (int id) {
|
||||
//
|
||||
var result = id <= 0;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using xCommons.Extensions;
|
||||
using xDataService.Interfaces;
|
||||
using xModels.Base;
|
||||
|
||||
namespace xDataService.Providers {
|
||||
public class XStringKeyGenerator : IXKeyGenerator<XBaseEntity<string>, string> {
|
||||
private readonly IXSequentialGuid sequentialGuid;
|
||||
|
||||
public XStringKeyGenerator (IXSequentialGuid sequentialGuid = null) {
|
||||
this.sequentialGuid = sequentialGuid;
|
||||
}
|
||||
|
||||
public async Task<string> GenerateKey (IXBaseRepository<XBaseEntity<string>, string> repository) {
|
||||
//
|
||||
await Task.CompletedTask;
|
||||
|
||||
//
|
||||
if (sequentialGuid.IsNull ()) {
|
||||
return Guid.NewGuid ().ToString ();
|
||||
} else {
|
||||
return sequentialGuid.Next ().ToString ();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsEmpty (string id) {
|
||||
return id.IsNullOrEmpty ();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user