45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using xCommons.Extensions;
|
|
using xDataService.Extensions;
|
|
using xDataService.Interfaces;
|
|
using xModels.Base;
|
|
|
|
namespace xDataService.Providers
|
|
{
|
|
/// <summary>
|
|
/// Default Long Key Generator ...
|
|
/// </summary>
|
|
/// <typeparam name="TEntity"></typeparam>
|
|
public class XLongKeyGenerator<TEntity> : XKeyGenerator<TEntity, long>, IXKeyGenerator<TEntity, long>
|
|
where TEntity : XBaseEntity<long>
|
|
{
|
|
public override async Task<long> GenerateKey(
|
|
IXBaseRepository<TEntity, long> repository,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var items = (await repository.GetAllAsync())
|
|
.OrderByDescending(nameof(XBaseEntity<long>.Id));
|
|
var last = items
|
|
.FirstOrDefault();
|
|
|
|
//
|
|
var result = last.IsNull() ? 1 : last.Id + 1;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
public override bool IsEmpty(long id)
|
|
{
|
|
//
|
|
var result = id <= 0;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |