幸福框架:可扩展的、动态的、万能的 编号生成器

背景

之前写过三篇文章介绍如何实现这种编号生成器:

上周整理了一下,把代码合并到了http://happy.codeplex.com/,需要的朋友直接下载最新代码,不要用Download(直接去Source Code下载)。

今天重点介绍一下如何使用。

一些常见的编号示例

像如下这些规则,非常容易支持,如:

  • 【xxxx】年某企业第【x】份劳动合同,规则配置:【<日期:yyyy>】年某企业第【<种子:销售订单:yyyy>】份劳动合同。
  • xxxx年xxxx月xxxx日第x份销售订单,规则配置:<日期:yyyy年MM月dd日>第<种子:销售订单:yyyyMMdd>份销售订单。

测试代码

 1 using System;
 2 using System.Collections.Generic;
 3 using Microsoft.VisualStudio.TestTools.UnitTesting;
 4 using System.IO;
 5 
 6 using Happy.Domain.CodeRule;
 7 using Happy.Domain.CodeRule.Generators;
 8 
 9 namespace Happy.Test.Doamin.CodeRule
10 {
11     [TestClass]
12     public class CodeRuleInterceptorTest
13     {
14         [TestMethod]
15         public void Intercept_Test()
16         {
17             var seedKey = "销售订单";
18             var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Seeds", seedKey + ".txt");
19             if (File.Exists(file))
20             {
21                 File.Delete(file);
22             }
23 
24             var interceptor = new CodeRuleInterceptor();
25 
26             interceptor
27                 .RegisterInterceptor(new DateTimeCodeGeneratorInterceptor())
28                 .RegisterInterceptor(new LiteralCodeGeneratorInterceptor())
29                 .RegisterInterceptor(new SeedCodeGeneratorInterceptor(new FileSeedStore()));
30 
31             var generator = interceptor.Intercept("前缀---<日期:yyyyMMdd>---中缀---<种子:销售订单>---后缀");
32 
33             Assert.IsNotNull(generator);
34 
35 
36             Assert.AreEqual("前缀---20130705---中缀---00001---后缀", generator.Generate(new GenerateContext()));
37             Assert.AreEqual("前缀---20130705---中缀---00002---后缀", generator.Generate(new GenerateContext()));
38             Assert.AreEqual("前缀---20130705---中缀---00003---后缀", generator.Generate(new GenerateContext()));
39         }
40     }
41 }

常见问题

问:种子的生成能保证唯一性吗?答:是的,在并发情况下也能保证唯一。

问:种子的生成能保证连续性吗?答:是的,约束就是必须使用基于数据库的种子仓储(Happy.Infrastructure.PetaPoco.PetaPocoSeedStore),且必须运行在处于”可重复读“隔离级别的事务中,上边的测试用例采用的是基于文件的。

问:为什么一定要配置规则,解释执行?答:这是面向产品级别的项目,如果是一般的项目,直接用种子仓储就行了,代码如下:

 1 using System;
 2 using Microsoft.VisualStudio.TestTools.UnitTesting;
 3 using System.IO;
 4 
 5 using Happy.Domain.CodeRule;
 6 
 7 namespace Happy.Test.Doamin.CodeRule
 8 {
 9     [TestClass]
10     public class FileSeedStoreTest
11     {
12         [TestMethod]
13         public void NextSeed_Test()
14         {
15             var seedKey = "销售订单";
16             var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Seeds", seedKey + ".txt");
17             if (File.Exists(file))
18             {
19                 File.Delete(file);
20             }
21 
22             var seedStore = new FileSeedStore();
23 
24             Assert.AreEqual(1, seedStore.NextSeed(seedKey));
25             Assert.AreEqual(2, seedStore.NextSeed(seedKey));
26             Assert.AreEqual(3, seedStore.NextSeed(seedKey));
27         }
28     }
29 }

备注

这种规则生成器,我在产品和项目中都有用过,新入门的朋友可以直接使用,高手要多提些意见。

原文地址:https://www.cnblogs.com/happyframework/p/3177128.html