添加项目文件。
This commit is contained in:
47
Atomx.Data/Services/IdCreatorService.cs
Normal file
47
Atomx.Data/Services/IdCreatorService.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using IdGen;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Atomx.Data.Services
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IIdCreatorService
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
long NewId();
|
||||
}
|
||||
|
||||
/// <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 NewId()
|
||||
{
|
||||
return IdCreator.CreateId();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user