幸福框架:可擴展的、動態的、萬能的 編號生成器


背景

之前寫過三篇文章介紹如何實現這種編號生成器:

上周整理了一下,把代碼合並到了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 }

備注

這種規則生成器,我在產品和項目中都有用過,新入門的朋友可以直接使用,高手要多提些意見。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM