1. 为了避免线程冲突而采用了单件模式来生成主键,并将上一个主键存放起来,以免生成重复的主键。
2. 字段类型采用定长字符型,如char(20), 生成的字符串前面还可以加上不同的前缀,比如表前缀或者其它一些有意义的标识符.
3. 转化成字母的十六进制类型是为了让最后的长度更小一些.
请大家参考:
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace GB.Core
6 {
7 /// <summary>
8 /// Powered By 沙加, matin0728@gmail.com, QQ5364241
9 /// </summary>
10 public class IdentityGenerator
11 {
12 static long lastIdentity = 0;
13 static IdentityGenerator o = new IdentityGenerator();
14 public static IdentityGenerator Instance
15 {
16 get
17 {
18 if (o != null)
19 return o;
20 else
21 return new IdentityGenerator();
22 }
23 }
24 public string NextIdentity()
25 {
26 long idint = DateTime.Now.Ticks - new DateTime(2000, 11, 1).Ticks;
27
28 while (lastIdentity >= idint)
29 {
30 idint++;
31 }
32 lastIdentity = idint;
33
34 return Convert.ToString(idint, 16);
35 }
36 }
37 }
38
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace GB.Core
6 {
7 /// <summary>
8 /// Powered By 沙加, matin0728@gmail.com, QQ5364241
9 /// </summary>
10 public class IdentityGenerator
11 {
12 static long lastIdentity = 0;
13 static IdentityGenerator o = new IdentityGenerator();
14 public static IdentityGenerator Instance
15 {
16 get
17 {
18 if (o != null)
19 return o;
20 else
21 return new IdentityGenerator();
22 }
23 }
24 public string NextIdentity()
25 {
26 long idint = DateTime.Now.Ticks - new DateTime(2000, 11, 1).Ticks;
27
28 while (lastIdentity >= idint)
29 {
30 idint++;
31 }
32 lastIdentity = idint;
33
34 return Convert.ToString(idint, 16);
35 }
36 }
37 }
38