Files
Atomx/Atomx.Data/Services/IdCreatorService.cs
2025-12-06 13:30:17 +08:00

48 lines
1.2 KiB
C#

using IdGen;
using Microsoft.Extensions.Configuration;
namespace Atomx.Data.Services
{
/// <summary>
///
/// </summary>
public interface IIdCreatorService
{
/// <summary>
///
/// </summary>
/// <returns></returns>
long CreateId();
}
/// <summary>
///
/// </summary>
public class IdCreatorService : IIdCreatorService
{
private IdGenerator IdCreator { get; set; }
/// <summary>
///
/// </summary>
/// <param name="configuration"></param>
public IdCreatorService(IConfiguration configuration)
{
DateTime epoch = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var structure = new IdStructure(45, 2, 16);
var options = new IdGeneratorOptions(structure, new DefaultTimeSource(epoch));
var workId = int.Parse(configuration.GetSection("IdWorkId").Value ?? "0");
IdCreator = new IdGenerator(workId, options);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public long CreateId()
{
return IdCreator.CreateId();
}
}
}