Rafy 框架又添新成员:流水号插件。本文将解释 Rafy 框架中的流水插件的场景、使用方法。
场景
在开发各类数据库应用系统时,往往需要生成从一开始的流水号,有时还需要按月或者按日进行独立生成,如下面的格式:2016031800000001、2016031800000002……。
设计本插件用于生成上述相应格式的编号。
使用方法
添加插件
1.通过 Nuget Package Manager 搜索并安装 Rafy.SerialNumber 插件。
2.在 DomainApp 中添加该插件;同时,设置该插件所对应的数据库配置名:
class JXCApp : DomainApp { protected override void InitEnvironment() { //配置插件所对应的数据库配置名。 Rafy.SerialNumber.SerialNumberPlugin.DbSettingName = "TestDb"; //添加流水号插件到 Rafy 应用程序集中。 RafyEnvironment.DomainPlugins.Add(new Rafy.SerialNumber.SerialNumberPlugin()); base.InitEnvironment(); } }
使用插件
1.生成数据库。
该插件中自带两个实体:SerialNumberInfo 、SerialNumberValue ,所以 Rafy 会为其在数据库中添加相应的两张表。
2.添加流水号生成规则。
SerialNumberInfo 表示定义的流水号生成规则信息。而 SerialNumberValue 则表示生成的流水号的具体值。所以要生成流水号,必须先为其定义生成规则。可以使用 SerialNumberController 进行简单的每日规则创建,示例如下:
var controller = DomainControllerFactory.Create<SerialNumberController>(); var sni = controller.CreateDailySerialNumberInfo("流水号规则-1");
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
CreateDailySerialNumberInfo 方法内部其实非常简单,开发者可以参考以下代码创建新的生成规则,如下:
/// <summary> /// 创建一个以日期进行分组生成编号的规则,存储到仓库中,并返回。 /// 性能-仓库访问次数:1。 /// </summary> /// <param name="name"></param> /// <returns></returns> public SerialNumberInfo CreateDailySerialNumberInfo(string name, string format = "yyyyMMdd********") { var sni = new SerialNumberInfo { Name = name, TimeGroupFormat = "yyyyMMdd", Format = format, RollValueStart = 1, RollValueStep = 1, }; var infoRepo = RF.Concrete<SerialNumberInfoRepository>(); infoRepo.Save(sni); return sni; });
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
3.生成流水号。
使用以下代码生成流水号即可:
var next = controller.GenerateNext("流水号规则-1"); Assert.AreEqual("2016031800000001", next); next = controller.GenerateNext("流水号规则-1"); Assert.AreEqual("2016031800000002", next); next = controller.GenerateNext(sni); Assert.AreEqual("2016031800000002", next);
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
PS:该文已经纳入《 Rafy 用户手册》中。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }