zoukankan      html  css  js  c++  java
  • 幸福框架:如何扩展编号生成器

    背景

    昨天发布了一个编号生成器(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.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         }

    运行结果

    留言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.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 }

    备注

    扩展规则就不说了,应该很容易看懂!

  • 相关阅读:
    codeforces 814B An express train to reveries
    codeforces 814A An abandoned sentiment from past
    codeforces 785D D. Anton and School
    codeforces 785C Anton and Fairy Tale
    codeforces 791C Bear and Different Names
    AOP详解
    Spring集成JUnit测试
    Spring整合web开发
    IOC装配Bean(注解方式)
    IOC装配Bean(XML方式)
  • 原文地址:https://www.cnblogs.com/happyframework/p/3179208.html
Copyright © 2011-2022 走看看