幸福框架:如何扩展编号生成器
来源:互联网 发布:淘宝代购蔻驰包真假 编辑:程序博客网 时间:2024/06/16 20:03
背景
昨天发布了一个编号生成器(http://www.cnblogs.com/happyframework/p/3177128.html),有朋友留言问支不支持某些规则,因为是可以扩展的,所以很容易支持各种规则,今天我就写了另外两种规则,也是对留言的朋友做一个回复。
留言A
留言
这个规则其实就是访问实体的属性就行了,属性里做个判断返回X或Y。
扩展类型
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.Generators10 {11 /// <summary>12 /// 属性编号生成器。13 /// </summary>14 public sealed class PropertyCodeGenerator : ICodeGenerator15 {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 context32 .Target33 .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 : ICodeRuleInterceptor14 {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 GenerateContext15 {16 Target = new17 {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 }
运行结果
留言B
留言
这位朋友实现了字母流水,我基本没有遇到过这种需求,实现一个也不麻烦,思路就是将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.Generators10 {11 /// <summary>12 /// 字母种子编号生成器。13 /// </summary>14 public sealed class LetterSeedCodeGenerator : ICodeGenerator15 {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 do47 {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 }
备注
扩展规则就不说了,应该很容易看懂!
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 幸福框架:如何扩展编号生成器
- 幸福框架:可扩展的、动态的、万能的 编号生成器
- 编号生成器Ejb3.0
- 通用编号生成器
- 题目编号生成器
- 配置yii框架环境和bootstrap扩展,使用代码生成器。
- 框架生成器
- 使用yii框架开发第一步:配置yii框架环境和bootstrap扩展,使用代码生成器。
- PHP扩展生成器
- java(优化一) 编号规则生成器(一)
- java(优化二) 编号规则生成器(二)
- java(优化三) 编号规则生成器(三)
- 如何自建appender扩展Log4j框架
- 如何追求幸福
- 扩展TypeList:Typelist生成器、MaxSizeOf
- kohana框架查询生成器
- 代码框架生成器
- 如何实现自编号
- C# ORM—Entity Framework 之Code first(代码优先)(二)
- 解决IP地址冲突的问题
- ASIHTTPRequest 取消
- loner_li C#中的反射
- 定制Octopress
- 幸福框架:如何扩展编号生成器
- 基于SPI的数据报过滤原理与实现
- socket(套节字)基本概念---linux进程间通信
- 利用ucarp实现虚拟IP故障转移
- CDN(内容分发网络)知识篇
- 360随身Wifi怎么使用
- 利用javascript 和 html5做的流体力学小件
- 创建守护进程
- Spring Batch Framework– introduction chapter(上)