昨天發佈了一個編號生成器(http://www.cnblogs.com/happyframework/p/3177128.html),有朋友留言問支不支持某些規則,由於是能夠擴展的,因此很容易支持各類規則,今天我就寫了另外兩種規則,也是對留言的朋友作一個回覆。html
這個規則其實就是訪問實體的屬性就好了,屬性裏作個判斷返回X或Y。app
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using Happy.Infrastructure.ExtentionMethods; 8 9 namespace Happy.Domain.CodeRule.Generators 10 { 11 /// <summary> 12 /// 屬性編號生成器。 13 /// </summary> 14 public sealed class PropertyCodeGenerator : ICodeGenerator 15 { 16 private readonly string _property; 17 18 /// <summary> 19 /// 構造方法。 20 /// </summary> 21 public PropertyCodeGenerator(string property) 22 { 23 property.MustNotNullAndNotWhiteSpace("property"); 24 25 _property = property; 26 } 27 28 /// <inheritdoc /> 29 public string Generate(GenerateContext context) 30 { 31 return context 32 .Target 33 .GetType() 34 .GetProperty(_property) 35 .GetValue(context.Target) 36 .ToString(); 37 } 38 } 39 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Text.RegularExpressions; 7 8 namespace Happy.Domain.CodeRule.Generators 9 { 10 /// <summary> 11 /// 屬性編號生成器解釋器。 12 /// </summary> 13 public sealed class PropertyCodeGeneratorInterceptor : ICodeRuleInterceptor 14 { 15 private static readonly Regex _Regex = new Regex(@"^<屬性:(?<名字>.*?)>$"); 16 17 /// <inheritdoc /> 18 public bool IsMatch(string codeRule) 19 { 20 return _Regex.IsMatch(codeRule); 21 } 22 23 /// <inheritdoc /> 24 public ICodeGenerator Intercept(string codeRule) 25 { 26 var match = _Regex.Match(codeRule); 27 28 var property = match.Groups["名字"].Value; 29 30 return new PropertyCodeGenerator(property); 31 } 32 } 33 }
1 static void Main(string[] args) 2 { 3 var interceptor = new CodeRuleInterceptor(); 4 5 interceptor 6 .RegisterInterceptor(new DateTimeCodeGeneratorInterceptor()) 7 .RegisterInterceptor(new LiteralCodeGeneratorInterceptor()) 8 .RegisterInterceptor(new PropertyCodeGeneratorInterceptor()) 9 .RegisterInterceptor(new SeedCodeGeneratorInterceptor(new FileSeedStore("Seeds"))) 10 .RegisterInterceptor(new LetterSeedCodeGeneratorInterceptor(new FileSeedStore("Seeds"))); 11 12 var generator = interceptor.Intercept("PM<日期:yyyyMMdd><屬性:Code><種子:銷售訂單:4>"); 13 14 var context = new GenerateContext 15 { 16 Target = new 17 { 18 Code = "X" 19 } 20 }; 21 22 Console.WriteLine(generator.Generate(context)); 23 Console.WriteLine(generator.Generate(context)); 24 Console.WriteLine(generator.Generate(context)); 25 }
這位朋友實現了字母流水,我基本沒有遇到過這種需求,實現一個也不麻煩,思路就是將10進制的種子數轉換爲26六進制的字母,這裏只介紹核心實現,不貼測試代碼了。測試
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 using Happy.Infrastructure.ExtentionMethods; 8 9 namespace Happy.Domain.CodeRule.Generators 10 { 11 /// <summary> 12 /// 字母種子編號生成器。 13 /// </summary> 14 public sealed class LetterSeedCodeGenerator : ICodeGenerator 15 { 16 private readonly ISeedStore _seedStore; 17 private readonly string _seedKey; 18 private readonly int _seedWidth; 19 20 /// <summary> 21 /// 構造方法。 22 /// </summary> 23 public LetterSeedCodeGenerator(ISeedStore seedStore, string seedKey, int seedWidth) 24 { 25 seedStore.MustNotNull("seedStore"); 26 seedKey.MustNotNullAndNotWhiteSpace("seedKey"); 27 seedWidth.MustGreaterThan("seedWidth", 0); 28 29 _seedStore = seedStore; 30 _seedKey = seedKey; 31 _seedWidth = seedWidth; 32 } 33 34 /// <inheritdoc /> 35 public string Generate(GenerateContext context) 36 { 37 var seed = _seedStore.NextSeed(_seedKey); 38 39 return this.ToLetter(seed).PadLeft(_seedWidth, 'A'); 40 } 41 42 private string ToLetter(int seed) 43 { 44 var letter = ""; 45 46 do 47 { 48 var quotient = seed % 26; 49 seed = seed / 26; 50 letter = (char)(quotient + 65) + letter; 51 } while (seed != 0); 52 53 return letter; 54 } 55 } 56 }
擴展規則就不說了,應該很容易看懂!this