"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[9931],{6406:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>l,contentTitle:()=>t,default:()=>h,frontMatter:()=>s,metadata:()=>c,toc:()=>o});var i=r(5893),d=r(1151);const s={id:"idgenerator",title:"27. \u5206\u5e03\u5f0f ID \u751f\u6210",sidebar_label:"27. \u5206\u5e03\u5f0f ID \u751f\u6210"},t=void 0,c={id:"idgenerator",title:"27. \u5206\u5e03\u5f0f ID \u751f\u6210",description:"27.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f ID",source:"@site/docs/idgenerator.mdx",sourceDirName:".",slug:"/idgenerator",permalink:"/docs/idgenerator",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/idgenerator.mdx",tags:[],version:"current",lastUpdatedBy:"\u767e\u5c0f\u50e7",lastUpdatedAt:1698699579,formattedLastUpdatedAt:"Oct 30, 2023",frontMatter:{id:"idgenerator",title:"27. \u5206\u5e03\u5f0f ID \u751f\u6210",sidebar_label:"27. \u5206\u5e03\u5f0f ID \u751f\u6210"},sidebar:"docs",previous:{title:"26.3 \u4efb\u52a1\u961f\u5217",permalink:"/docs/task-queue"},next:{title:"28. \u6a21\u5757\u5316\u5f00\u53d1",permalink:"/docs/module-dev"}},l={},o=[{value:"27.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f ID",id:"271-\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f-id",level:2},{value:"27.2 \u5206\u5e03\u5f0f ID \u6709\u54ea\u4e9b",id:"272-\u5206\u5e03\u5f0f-id-\u6709\u54ea\u4e9b",level:2},{value:"27.3 \u5982\u4f55\u4f7f\u7528",id:"273-\u5982\u4f55\u4f7f\u7528",level:2},{value:"27.3.1 \u8fde\u7eed GUID
\u65b9\u5f0f",id:"2731-\u8fde\u7eed-guid-\u65b9\u5f0f",level:3},{value:"27.3.2 \u77ed ID
",id:"2732-\u77ed-id",level:3},{value:"27.3.3 \u96ea\u82b1\u7b97\u6cd5 ID
",id:"2733-\u96ea\u82b1\u7b97\u6cd5-id",level:3},{value:"27.4 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"274-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function a(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,d.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.h2,{id:"271-\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f-id",children:"27.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e03\u5f0f ID"}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"\u5168\u5c40\u552f\u4e00\u6027"}),": \u4e0d\u80fd\u51fa\u73b0\u91cd\u590d\u7684 ID \u53f7, \u65e2\u7136\u662f\u552f\u4e00\u6807\u8bc6, \u8fd9\u662f\u6700\u57fa\u672c\u7684\u8981\u6c42\u3002"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"\u8d8b\u52bf\u9012\u589e"}),": \u5728 MySQL InnoDB \u5f15\u64ce\u4e2d\u4f7f\u7528\u7684\u662f\u805a\u96c6\u7d22\u5f15, \u7531\u4e8e\u591a\u6570 RDBMS \u4f7f\u7528 B-tree \u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u7d22\u5f15\u6570\u636e, \u5728\u4e3b\u952e\u7684\u9009\u62e9\u4e0a\u9762\u6211\u4eec\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528\u6709\u5e8f\u7684\u4e3b\u952e\u4fdd\u8bc1\u5199\u5165\u6027\u80fd\u3002"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"\u5355\u8c03\u9012\u589e"}),": \u4fdd\u8bc1\u4e0b\u4e00\u4e2a ID \u4e00\u5b9a\u5927\u4e8e\u4e0a\u4e00\u4e2a ID, \u4f8b\u5982\u4e8b\u52a1\u7248\u672c\u53f7, IM \u589e\u91cf\u6d88\u606f, \u6392\u5e8f\u7b49\u7279\u6b8a\u9700\u6c42\u3002"]}),"\n"]}),"\n",(0,i.jsxs)(n.li,{children:["\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"\u4fe1\u606f\u5b89\u5168"}),": \u5982\u679c ID \u662f\u8fde\u7eed\u7684, \u6076\u610f\u7528\u6237\u7684\u6252\u53d6\u5de5\u4f5c\u5c31\u975e\u5e38\u5bb9\u6613\u505a\u4e86, \u76f4\u63a5\u6309\u7167\u987a\u5e8f\u4e0b\u8f7d\u6307\u5b9a URL \u5373\u53ef; \u5982\u679c\u662f\u8ba2\u5355\u53f7\u5c31\u66f4\u5371\u9669\u4e86, \u7ade\u5bf9\u53ef\u4ee5\u76f4\u63a5\u77e5\u9053\u6211\u4eec\u4e00\u5929\u7684\u5355\u91cf\u3002 \u6240\u4ee5\u5728\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u4e0b, \u4f1a\u9700\u8981 ID \u65e0\u89c4\u5219, \u4e0d\u89c4\u5219\u3002"]}),"\n"]}),"\n"]}),"\n",(0,i.jsx)(n.h2,{id:"272-\u5206\u5e03\u5f0f-id-\u6709\u54ea\u4e9b",children:"27.2 \u5206\u5e03\u5f0f ID \u6709\u54ea\u4e9b"}),"\n",(0,i.jsxs)(n.p,{children:["\u5e38\u89c1\u7684\u5206\u5e03\u5f0f ID \u6709 ",(0,i.jsx)(n.code,{children:"\u8fde\u7eed GUID"}),"\u3001",(0,i.jsx)(n.code,{children:"\u77ed ID"}),"\u3001",(0,i.jsx)(n.code,{children:"\u96ea\u82b1\u7b97\u6cd5 ID"}),"\u3002"]}),"\n",(0,i.jsx)(n.h2,{id:"273-\u5982\u4f55\u4f7f\u7528",children:"27.3 \u5982\u4f55\u4f7f\u7528"}),"\n",(0,i.jsxs)(n.h3,{id:"2731-\u8fde\u7eed-guid-\u65b9\u5f0f",children:["27.3.1 ",(0,i.jsx)(n.code,{children:"\u8fde\u7eed GUID"})," \u65b9\u5f0f"]}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:["\u9759\u6001 ",(0,i.jsx)(n.code,{children:"IDGen"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4}",children:"var guid = IDGen.NextID();\r\n\r\n// \u8fd8\u53ef\u4ee5\u914d\u7f6e\u66f4\u591a\u53c2\u6570\r\nvar guid2 = IDGen.NextID(new SequentialGuidSettings { LittleEndianBinary16Format = true })); // SequentialGuidSettings \u53c2\u6570\u53d6\u51b3\u4e8e\u4f60\u7684\u5206\u5e03\u5f0fID\u7684\u5b9e\u73b0\n"})}),"\n",(0,i.jsx)(n.admonition,{title:"\u7279\u522b\u63d0\u9192",type:"important",children:(0,i.jsxs)(n.p,{children:["\u5982\u679c\u5728\u5faa\u73af\u4e2d\u4f7f\u7528 ",(0,i.jsx)(n.code,{children:"IDGen"})," \u9759\u6001\u7c7b\u65b9\u5f0f\uff0c\u6027\u80fd\u6700\u5dee\uff0c\u539f\u56e0\u662f\u5e95\u5c42\u4e0d\u65ad\u89e3\u6790\u670d\u52a1\u3002\u5982\u679c\u975e\u5faa\u73af\u4e2d\uff0c\u6027\u80fd\u7b49\u4e8e\u4e0b\u9762\u4e24\u79cd\u7528\u6cd5\u3002"]})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"IDistributedIDGenerator"})," \u6ce8\u5165\u65b9\u5f0f ",(0,i.jsx)(n.strong,{children:"\u63a8\u8350"})]}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {2,6}",children:"private readonly IDistributedIDGenerator _idGenerator;\r\npublic AppServices(IDistributedIDGenerator idGenerator)\r\n{\r\n _idGenerator = idGenerator;\r\n\r\n var guidObject = _idGenerator.Create();\r\n}\n"})}),"\n",(0,i.jsxs)(n.ul,{children:["\n",(0,i.jsxs)(n.li,{children:[(0,i.jsx)(n.code,{children:"SequentialGuidIDGenerator"})," \u65b9\u5f0f"]}),"\n"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers ",children:"var idGen = new SequentialGuidIDGenerator();\r\nvar guid = idGen.Create();\r\n\r\n// \u66f4\u591a\u53c2\u6570\r\nvar idGen2 = new SequentialGuidIDGenerator();\r\nvar guid2 = idGen2.Create(new SequentialGuidSettings { LittleEndianBinary16Format = true });\n"})}),"\n",(0,i.jsxs)(n.h3,{id:"2732-\u77ed-id",children:["27.3.2 ",(0,i.jsx)(n.code,{children:"\u77ed ID"})]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"\u77ed ID"})," \u6309\u9053\u7406\u4e0d\u5e94\u8be5\u653e\u5728\u5206\u5e03\u5f0f ID \u751f\u6210\u8fd9\u4e2a\u7ae0\u8282\uff0c\u5b83\u7684\u4f5c\u7528\u7528\u9014\u5e38\u7528\u4e8e\u5e76\u53d1\u4e0d\u5f3a\u7684\u5185\u90e8\u7cfb\u7edf\u4e2d\uff0c\u6bd4\u5982 ",(0,i.jsx)(n.code,{children:"\u4efb\u52a1ID"}),"\uff0c",(0,i.jsx)(n.code,{children:"Issue \u7f16\u53f7"})," \u7b49\u7b49\u3002"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,4}",children:'var shortid = ShortIDGen.NextID(); // \u751f\u6210\u4e00\u4e2a\u5305\u542b\u6570\u5b57\uff0c\u5b57\u6bcd\uff0c\u4e0d\u5305\u542b\u7279\u6b8a\u7b26\u53f7\u7684 8 \u4f4d\u77edid\r\n\r\n// \u6dfb\u52a0\u66f4\u591a\u914d\u7f6e\r\nvar shortid = ShortIDGen.NextID(new GenerationOptions {\r\n UseNumbers = false, // \u4e0d\u5305\u542b\u6570\u5b57\r\n UseSpecialCharacters = true, // \u5305\u542b\u7279\u6b8a\u7b26\u53f7\r\n Length = 8// \u8bbe\u7f6e\u957f\u5ea6\uff0c\u6ce8\u610f\uff1a\u4e0d\u8bbe\u7f6e\u6b21\u957f\u5ea6\u662f\u968f\u673a\u957f\u5ea6\uff01\uff01\uff01\uff01\uff01\uff01\uff01\r\n});\r\n\r\n// \u81ea\u5b9a\u4e49\u751f\u6210\u77ed ID \u53c2\u4e0e\u8fd0\u7b97\u5b57\u7b26\r\nstring characters = "\u24b6\u24b7\u24b8\u24b9\u24ba\u24bb\u24bc\u24bd\u24be\u24bf\u24c0\u24c1\u24c2\u24c3\u24c4\u24c5\u24c6\u24c7\u24c8\u24c9\u24ca\u24cb\u24cc\u24cd\u24ce\u24cf\u24d0\u24d1\u24d2\u24d3\u24d4\u24d5\u24d6\u24d7\u24d8\u24d9\u24da\u24db\u24dc\u24dd\u24de\u24df\u24e0\u24e1\u24e2\u24e3\u24e4\u24e5\u24e6\u24e7\u24e8\u24e9\u2460\u2461\u2462\u2463\u2464\u2465\u2466\u2467\u2468\u2469\u246a\u246b"; //whatever you want;\r\nShortIDGen.SetCharacters(characters);\r\n\r\n// \u81ea\u5b9a\u4e49\u968f\u673a\u6570\uff08for\uff09\u6b65\u957f\r\nint seed = 1939048828;\r\nShortIDGen.SetSeed(seed);\r\n\r\n// \u91cd\u8f7d\u6240\u6709\u81ea\u5b9a\u4e49\u914d\u7f6e\r\nShortIDGen.Reset();\n'})}),"\n",(0,i.jsxs)(n.h3,{id:"2733-\u96ea\u82b1\u7b97\u6cd5-id",children:["27.3.3 ",(0,i.jsx)(n.code,{children:"\u96ea\u82b1\u7b97\u6cd5 ID"})]}),"\n",(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.code,{children:"Furion"})," \u5728\u6700\u65b0\u7684 ",(0,i.jsx)(n.code,{children:"2.1 +"})," \u7248\u672c\u79fb\u9664\u4e86",(0,i.jsx)(n.code,{children:"\u96ea\u82b1\u7b97\u6cd5 ID"})," \u529f\u80fd\uff0c\u539f\u56e0\u662f\uff1a"]}),"\n",(0,i.jsx)(n.p,{children:(0,i.jsxs)(n.strong,{children:["\u76ee\u524d\uff0c",(0,i.jsx)(n.code,{children:"\u96ea\u82b1\u7b97\u6cd5 ID"})," \u4f7f\u7528\u9891\u7387\u4e0d\u9ad8\uff0c\u800c\u4e14\u5b9e\u73b0 ",(0,i.jsx)(n.code,{children:"\u96ea\u82b1\u7b97\u6cd5 ID"})," \u7684\u65b9\u5f0f\u4e5f\u662f\u5343\u5dee\u4e07\u522b\uff0c\u6240\u4ee5\u6846\u67b6\u79fb\u9664\u8be5\u529f\u80fd\uff0c\u91c7\u7528\u62d3\u5c55\u6216\u81ea\u96c6\u6210\u65b9\u5f0f\u3002"]})}),"\n",(0,i.jsx)(n.h2,{id:"274-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"27.4 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,i.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,i.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,i.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,i.jsx)(n.hr,{})]})}function h(e={}){const{wrapper:n}={...(0,d.a)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(a,{...e})}):a(e)}},1151:(e,n,r)=>{r.d(n,{Z:()=>c,a:()=>t});var i=r(7294);const d={},s=i.createContext(d);function t(e){const n=i.useContext(s);return i.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function c(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(d):e.components||d:t(e.components),i.createElement(s.Provider,{value:n},e.children)}}}]);