Tenon.DistributedId.Abstractions 0.0.1-alpha-202502101554

This is a prerelease version of Tenon.DistributedId.Abstractions.
dotnet add package Tenon.DistributedId.Abstractions --version 0.0.1-alpha-202502101554                
NuGet\Install-Package Tenon.DistributedId.Abstractions -Version 0.0.1-alpha-202502101554                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tenon.DistributedId.Abstractions" Version="0.0.1-alpha-202502101554" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tenon.DistributedId.Abstractions --version 0.0.1-alpha-202502101554                
#r "nuget: Tenon.DistributedId.Abstractions, 0.0.1-alpha-202502101554"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tenon.DistributedId.Abstractions as a Cake Addin
#addin nuget:?package=Tenon.DistributedId.Abstractions&version=0.0.1-alpha-202502101554&prerelease

// Install Tenon.DistributedId.Abstractions as a Cake Tool
#tool nuget:?package=Tenon.DistributedId.Abstractions&version=0.0.1-alpha-202502101554&prerelease                

Tenon.DistributedId.Abstractions

NuGet version License: MIT

Tenon.DistributedId.Abstractions 提供了统一的分布式 ID 生成器抽象接口定义,是 Tenon 框架分布式 ID 生成功能的核心基础。通过抽象接口设计,实现了 ID 生成器的可插拔性和一致性。

✨ 设计优势

  • 🎯 统一抽象:提供统一的 IDGenerator 接口,确保不同 ID 生成实现的一致性
  • 🔌 可插拔性:支持多种 ID 生成算法无缝切换,无需修改业务代码
  • 💡 简洁接口:精心设计的 API 接口,易于使用和扩展
  • 🛡️ 类型安全:严格的类型设计,避免运行时类型错误
  • 🔄 工作节点管理:完善的工作节点管理机制,确保分布式环境下的唯一性
  • 📦 扩展机制:灵活的选项扩展机制,支持自定义实现
  • 高性能:优化的接口设计,最小化性能开销

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.DistributedId.Abstractions

🚀 核心接口

IDGenerator

提供统一的 ID 生成器接口:

public interface IDGenerator
{
    // 设置工作节点 ID
    void SetWorkerId(ushort workerId);

    // 重置工作节点 ID
    void ResetWorkerId();

    // 获取下一个唯一 ID
    long GetNextId();

    // 当前工作节点 ID
    short? WorkerId { get; }

    // 最大工作节点 ID
    short MaxWorkerId { get; }
}

IDistributedIdOptionsExtension

提供统一的选项扩展接口:

public interface IDistributedIdOptionsExtension
{
    // 添加服务到依赖注入容器
    void AddServices(IServiceCollection services);
}

📖 使用方式

1. 注册服务

services.AddDistributedId(options =>
{
    // 使用雪花算法实现
    options.UseSnowflake(snowflakeOptions => 
    {
        snowflakeOptions.ServiceName = "OrderService";
        snowflakeOptions.WorkerNode = new WorkerNodeOptions 
        {
            Prefix = "distributedId:workerIds:",
            ExpireTimeInSeconds = 60
        };
    });
});

2. 在服务中使用

public class OrderService
{
    private readonly IDGenerator _idGenerator;

    public OrderService(IDGenerator idGenerator)
    {
        _idGenerator = idGenerator;
    }

    public long CreateOrderId()
    {
        return _idGenerator.GetNextId();
    }
}

💡 实现参考

框架提供了多个开箱即用的实现:

  1. Tenon.DistributedId.Snowflake

    • 基于雪花算法的分布式 ID 生成器实现
    • 支持 Redis 工作节点管理
    • 提供完整的配置选项
  2. 自定义实现

    • 实现 IDGenerator 接口
    • 实现 IDistributedIdOptionsExtension 接口
    • 参考现有实现的最佳实践

⚙️ 配置选项

DistributedIdOptions

分布式 ID 生成器的基础配置选项:

public class DistributedIdOptions
{
    // 扩展集合
    public IList<IDistributedIdOptionsExtension> Extensions { get; }
    
    // 添加扩展
    public void RegisterExtension(IDistributedIdOptionsExtension extension);
}

🔨 项目依赖

  • Microsoft.Extensions.DependencyInjection.Abstractions

📝 最佳实践

1. 接口实现

  • 确保线程安全
  • 实现完整的异常处理
  • 添加适当的日志记录

2. 扩展开发

  • 遵循依赖注入原则
  • 实现优雅的配置机制
  • 提供合理的默认值

3. 性能优化

  • 避免不必要的对象分配
  • 使用高效的算法
  • 实现适当的缓存策略

🌰 实现示例

1. 基础实现

public class CustomIdGenerator : IDGenerator
{
    private short? _workerId;
    
    public void SetWorkerId(ushort workerId)
    {
        _workerId = (short)workerId;
    }

    public void ResetWorkerId()
    {
        _workerId = null;
    }

    public long GetNextId()
    {
        if (!_workerId.HasValue)
            throw new InvalidOperationException("WorkerId not set");
            
        // 实现自定义的 ID 生成逻辑
        return GenerateId();
    }

    public short? WorkerId => _workerId;

    public short MaxWorkerId => 1024;
}

2. 扩展实现

public class CustomOptionsExtension : IDistributedIdOptionsExtension
{
    private readonly CustomOptions _options;

    public CustomOptionsExtension(CustomOptions options)
    {
        _options = options;
    }

    public void AddServices(IServiceCollection services)
    {
        services.Configure<CustomOptions>(_options);
        services.AddSingleton<IDGenerator, CustomIdGenerator>();
    }
}

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Tenon.DistributedId.Abstractions:

Package Downloads
Tenon.MessageTracker.EfCore

Package Description

Tenon.DistributedId.Snowflake

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.1-alpha-202502101554 38 2/10/2025
0.0.1-alpha-202502101448 36 2/10/2025
0.0.1-alpha-202502101434 33 2/10/2025
0.0.1-alpha-202501130258 38 1/13/2025
0.0.1-alpha-202412311524 78 12/31/2024
0.0.1-alpha-202412061617 56 12/6/2024
0.0.1-alpha-202412051527 56 12/5/2024
0.0.1-alpha-202412051432 50 12/5/2024
0.0.1-alpha-202412041445 52 12/4/2024
0.0.1-alpha-202412021409 49 12/2/2024
0.0.1-alpha-202411301019 56 11/30/2024
0.0.1-alpha-202411170525 54 11/17/2024
0.0.1-alpha-202411161308 55 11/16/2024
0.0.1-alpha-202411131604 53 11/13/2024
0.0.1-alpha-202411111439 68 11/11/2024
0.0.1-alpha-202411051434 56 11/5/2024
0.0.1-alpha-202410281339 57 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 62 7/26/2024
0.0.1-alpha-202407261325 60 7/26/2024
0.0.1-alpha-202406271301 65 6/27/2024
0.0.1-alpha-202406251508 59 6/25/2024
0.0.1-alpha-202406251310 59 6/25/2024
0.0.1-alpha-202406141611 59 6/14/2024
0.0.1-alpha-202406141550 58 6/14/2024
0.0.1-alpha-202406121515 62 6/12/2024
0.0.1-alpha-202406061553 67 6/6/2024
0.0.1-alpha-202406041519 58 6/4/2024
0.0.1-alpha-202406011613 64 6/1/2024
0.0.1-alpha-202406011238 59 6/1/2024
0.0.1-alpha-202405311458 58 5/31/2024
0.0.1-alpha-202405291213 70 5/29/2024
0.0.1-alpha-202405190457 64 5/19/2024
0.0.1-alpha-202405161229 56 5/16/2024
0.0.1-alpha-202405141510 60 5/14/2024
0.0.1-alpha-202405101323 66 5/10/2024
0.0.1-alpha-202405081356 72 5/8/2024
0.0.1-alpha-202405021337 28 5/2/2024
0.0.1-alpha-202405021336 32 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 62 4/29/2024
0.0.1-alpha-202404281218 71 4/28/2024