52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System;
|
|
using System.Threading;
|
|
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,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
return await Task.Run(() =>
|
|
{
|
|
//
|
|
// Check if provided SequentialGuid ...
|
|
if (sequentialGuid.IsNull())
|
|
{
|
|
return Guid.NewGuid();
|
|
}
|
|
else
|
|
{
|
|
return sequentialGuid.Next();
|
|
}
|
|
}, cancellationToken: cancellationToken);
|
|
}
|
|
|
|
public bool IsEmpty(Guid id)
|
|
{
|
|
//
|
|
var result = id.IsNull() || id.IsDefaultGuid();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |