diff --git a/src/Yitter.IdGenerator/YidHelper.cs b/src/Yitter.IdGenerator/YidHelper.cs new file mode 100644 index 0000000..8f7a018 --- /dev/null +++ b/src/Yitter.IdGenerator/YidHelper.cs @@ -0,0 +1,53 @@ +/* + * 版权属于:yitter(yitter@126.com) + * 开源地址:https://gitee.com/yitter/idgenerator + * 版权协议:MIT + * 版权说明:只要保留本版权,你可以免费使用、修改、分发本代码。 + * 免责条款:任何因为本代码产生的系统、法律、政治、宗教问题,均与版权所有者无关。 + * + */ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Yitter.IdGenerator +{ + /// + /// 这是一个调用的例子,默认情况下,集成者可以直接使用 NewId()。 + /// + public class YidHelper + { + private static IIdGenerator _IdGenInstance = null; + + public static IIdGenerator IdGenInstance + { + get + { + return _IdGenInstance; + } + } + + public static void SetIdGenerator(IdGeneratorOptions options) + { + _IdGenInstance = new YitIdGenerator(options); + } + + /// + /// 生成新的Id + /// 调用本方法前,请确保调用了 SetIdGenerator 方法做初始化。 + /// 否则将会初始化一个WorkerId为1的对象。 + /// + /// + public static long NewId() + { + if (_IdGenInstance == null) + { + _IdGenInstance = new YitIdGenerator(new IdGeneratorOptions() { WorkerId = 1 }); + } + + return _IdGenInstance.NewLong(); + } + + } +} diff --git a/src/Yitter.IdGenerator/YitIdGenerator.cs b/src/Yitter.IdGenerator/YitIdGenerator.cs index 63613b7..ea3952c 100644 --- a/src/Yitter.IdGenerator/YitIdGenerator.cs +++ b/src/Yitter.IdGenerator/YitIdGenerator.cs @@ -24,6 +24,7 @@ namespace Yitter.IdGenerator set => _SnowWorker.GenAction = value; } + public YitIdGenerator(IdGeneratorOptions options) { if (options == null)