ArticleDirectory
- Seed Table Design
- Running result
Background
In my previous article ". Net: extensible document number generator + simple interpreter", I introduced a simple document numbering framework. A friend left a message asking how to implement"Sequence number, and cannot be repeated", This article uses the framework introduced in the previous article to address this issue.
Train of Thought sequence number = last sequence number + step size
According to the formula above, the problem can be resolved as follows: how to obtain the sequence number of the last time? You can obtain the sequence number in either of the following ways:
-
- Scan the document table to find the latest record.
- The seed table is introduced. The seed table records the latest sequence number.
Because the generated sequence numbers cannot be repeated, there is a concurrency requirement here. To maximize the concurrency, I choose 2 (Introduce a seed table).
Concurrent processing can be selected: Pessimistic lock or optimistic lock. For simplicity, I choosePessimistic lock.
Implementation Code Download: http://yunpan.cn/q5kmuta3qgpct. Seed Table Design
1 Create Table [ DBO ] . [ Codeseeds ] ( 2 [ ID ] Uniqueidentifier Not Null , 3 [ Key ] Nvarchar ( 500 ) Not Null , 4 [ Value ] Int Not Null , 5 Primary Key Clustered ( [ ID ] ASC ) 6 );
Seedcoderuleprovider. CS
1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 Using System. Threading. tasks; 6 7 Using System. Transactions; 8 Using System. Text. regularexpressions; 9 Using System. Data. entity. Infrastructure; 10 11 Namespace Entitycoderuledemo 12 { 13 Public Class Seedcoderuleprovider: icoderuleprovider 14 { 15 Private Readonly Int _ Width; 16 17 Public Seedcoderuleprovider ( Int Width) 18 { 19 _ Width = Width; 20 } 21 22 Public String Generate ( Object Entity) 23 { 24 Return Getseedvalue (Entity). tostring (). padleft (_ width, ' 0 ' ); 25 } 26 27 Protected Virtual String Getkey ( Object Entity) 28 { 29 Return Entity. GetType (). fullname; 30 } 31 32 Private Int Getseedvalue ( Object Entity) 33 { 34 Try 35 { 36 VaR Value = 0 ; 37 VaR Key = This . Getkey (entity ); 38 39 Using ( VaR TS = New Transactionscope (transactionscospontion. requiresnew, 40 New Transactionoptions {isolationlevel = Isolationlevel. repeatableread })) 41 { 42 Using ( VaR Context = New Testcontext ()) 43 { 44 VaR Seed = context. codeseeds. Where (x => X. Key = Key). firstordefault (); 45 If (Seed = Null ) 46 { 47 Seed = New Codeseed {id = guid. newguid (), Key = key, value =-1 }; 48 Context. codeseeds. Add (SEED ); 49 } 50 51 Seed. Value ++ ; 52 Value = Seed. value; 53 Context. savechanges (); 54 } 55 56 TS. Complete (); 57 } 58 59 Return Value; 60 } 61 Catch (Dbupdateexception) 62 { 63 Return This . Getseedvalue (entity ); 64 } 65 } 66 67 Public Static Seedcoderuleprovider seedcoderuleproviderfactory ( String Literal) 68 { 69 VaR Match = New RegEx ( " ^ <Seed (:(? <Width>. *?)?> $ " ). Match (literal ); 70 71 VaR Width = match. Groups [ " Width " ]. Value; 72 73 Return New Seedcoderuleprovider ( String . Isnullorempty (width )? 5 : Int . Parse (width )); 74 } 75 } 76 }
Program. CS
1 Using System; 2 Using System. Collections. Generic; 3 Using System. LINQ; 4 Using System. text; 5 Using System. Threading. tasks; 6 7 Using System. Text. regularexpressions; 8 9 Namespace Entitycoderuledemo 10 { 11 Class Program 12 { 13 Static Void Main ( String [] ARGs) 14 { 15 Coderuleinterpreter. registproviderfactory ( New RegEx ( " ^ <Seed (:(? <Width>. *?)?> $ " ), Seedcoderuleprovider. seedcoderuleproviderfactory ); 16 17 VaR Employeecode = Coderuleinterpreter 18 . Interpret ( " Prefix _ <date: yyyy_mm_dd >_< attribute: namepinyin >_< seed: 6> " ) 19 . Generate ( New Employee {namepinyin = " Duangw " }); 20 21 Console. writeline (employeecode ); 22 } 23 } 24 }
Running result
Remarks
There are write business requirements that will force the continuity of numbers or random numbers. For these two requirements, you also need to develop a separate provider and have the opportunity to write articles again.