"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[984],{3066:(e,r,n)=>{n.r(r),n.d(r,{assets:()=>a,contentTitle:()=>t,default:()=>l,frontMatter:()=>o,metadata:()=>d,toc:()=>c});var s=n(5893),i=n(1151);const o={id:"dapper",title:"10.2 Dapper \u96c6\u6210",sidebar_label:"10.2 Dapper \u96c6\u6210"},t=void 0,d={id:"dapper",title:"10.2 Dapper \u96c6\u6210",description:"\u5728 Furion \u5305\u4e2d\u9ed8\u8ba4\u96c6\u6210\u4e86 EFCore\uff0c\u5982\u679c\u4e0d\u4f7f\u7528 EFCore\uff0c\u53ef\u5b89\u88c5\u7eaf\u51c0\u7248 Furion.Pure \u4ee3\u66ff Furion\u3002",source:"@site/docs/dapper.mdx",sourceDirName:".",slug:"/dapper",permalink:"/docs/dapper",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/dapper.mdx",tags:[],version:"current",lastUpdatedBy:"MonkSoul",lastUpdatedAt:1660021283,formattedLastUpdatedAt:"Aug 9, 2022",frontMatter:{id:"dapper",title:"10.2 Dapper \u96c6\u6210",sidebar_label:"10.2 Dapper \u96c6\u6210"},sidebar:"docs",previous:{title:"10.1 SqlSugar \u96c6\u6210",permalink:"/docs/sqlsugar"},next:{title:"10.3 MongoDB \u64cd\u4f5c",permalink:"/docs/mongodb"}},a={},c=[{value:"10.2.1 \u5173\u4e8e Dapper",id:"1021-\u5173\u4e8e-dapper",level:2},{value:"10.2.2 \u5982\u4f55\u96c6\u6210",id:"1022-\u5982\u4f55\u96c6\u6210",level:2},{value:"10.2.2.1 \u6ce8\u518c Dapper \u670d\u52a1",id:"10221-\u6ce8\u518c-dapper-\u670d\u52a1",level:3},{value:"10.2.2.2 \u5b89\u88c5\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u63d0\u4f9b\u5668",id:"10222-\u5b89\u88c5\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u63d0\u4f9b\u5668",level:3},{value:"10.2.3 \u57fa\u672c\u4f7f\u7528",id:"1023-\u57fa\u672c\u4f7f\u7528",level:2},{value:"10.2.3.1 sql \u64cd\u4f5c",id:"10231-sql-\u64cd\u4f5c",level:3},{value:"10.2.3.2 <TEntity> \u64cd\u4f5c",id:"10232-tentity-\u64cd\u4f5c",level:3},{value:"10.2.4 \u9ad8\u7ea7\u4f7f\u7528",id:"1024-\u9ad8\u7ea7\u4f7f\u7528",level:2},{value:"10.2.4.1 \u67e5\u8be2\u4e00\u5bf9\u4e00",id:"10241-\u67e5\u8be2\u4e00\u5bf9\u4e00",level:3},{value:"10.2.4.2 \u67e5\u8be2\u591a\u4e2a\u7ed3\u679c",id:"10242-\u67e5\u8be2\u591a\u4e2a\u7ed3\u679c",level:3},{value:"10.2.4.3 \u66f4\u591a\u64cd\u4f5c",id:"10243-\u66f4\u591a\u64cd\u4f5c",level:3},{value:"10.2.5 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"1025-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function p(e){const r={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,i.a)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(r.admonition,{title:"\u6e29\u99a8\u63d0\u9192",type:"warning",children:(0,s.jsxs)(r.p,{children:["\u5728 ",(0,s.jsx)(r.code,{children:"Furion"})," \u5305\u4e2d\u9ed8\u8ba4\u96c6\u6210\u4e86 ",(0,s.jsx)(r.code,{children:"EFCore"}),"\uff0c",(0,s.jsxs)(r.strong,{children:["\u5982\u679c\u4e0d\u4f7f\u7528 ",(0,s.jsx)(r.code,{children:"EFCore"}),"\uff0c\u53ef\u5b89\u88c5\u7eaf\u51c0\u7248 ",(0,s.jsx)(r.code,{children:"Furion.Pure"})," \u4ee3\u66ff ",(0,s.jsx)(r.code,{children:"Furion"})]}),"\u3002"]})}),"\n",(0,s.jsx)(r.h2,{id:"1021-\u5173\u4e8e-dapper",children:"10.2.1 \u5173\u4e8e Dapper"}),"\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.code,{children:"Dapper"})," \u662f .NET/C# \u5e73\u53f0\u975e\u5e38\u4f18\u79c0\u7684 ",(0,s.jsx)(r.code,{children:"\u5fae\u578b ORM"})," \u6846\u67b6\uff0c\u4e3b\u8981\u662f\u4e3a ",(0,s.jsx)(r.code,{children:"ADO.NET"})," \u64cd\u4f5c\u5bf9\u8c61\u63d0\u4f9b\u62d3\u5c55\u80fd\u529b\uff0c\u63a8\u5d07\u539f\u751f ",(0,s.jsx)(r.code,{children:"sql"})," \u64cd\u4f5c\u6cd5\u3002"]}),"\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.code,{children:"Dapper"})," \u5b98\u65b9\u4ed3\u5e93\u5730\u5740\uff1a",(0,s.jsx)(r.a,{href:"https://github.com/StackExchange/Dapper",children:"https://github.com/StackExchange/Dapper"})]}),"\n",(0,s.jsx)(r.h2,{id:"1022-\u5982\u4f55\u96c6\u6210",children:"10.2.2 \u5982\u4f55\u96c6\u6210"}),"\n",(0,s.jsxs)(r.p,{children:["\u5728 ",(0,s.jsx)(r.code,{children:"Furion"})," \u6846\u67b6\u4e2d\uff0c\u5df2\u7ecf\u63a8\u51fa ",(0,s.jsx)(r.code,{children:"Dapper"})," \u62d3\u5c55\u5305 ",(0,s.jsx)(r.a,{href:"https://www.nuget.org/packages/Furion.Extras.DatabaseAccessor.Dapper",children:"Furion.Extras.DatabaseAccessor.Dapper"}),"\u3002"]}),"\n",(0,s.jsxs)(r.h3,{id:"10221-\u6ce8\u518c-dapper-\u670d\u52a1",children:["10.2.2.1 \u6ce8\u518c ",(0,s.jsx)(r.code,{children:"Dapper"})," \u670d\u52a1"]}),"\n",(0,s.jsxs)(r.p,{children:["\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728 ",(0,s.jsx)(r.code,{children:"Startup.cs"})," \u4e2d\u6dfb\u52a0 ",(0,s.jsx)(r.code,{children:"services.AddDapper(connStr, SqlProvider)"})," \u5373\u53ef\u3002\u5982\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'services.AddDapper("Data Source=./Furion.db", SqlProvider.Sqlite);\r\n\r\n// \u66f4\u591a\u914d\u7f6e\uff0c\u4ec5 v3.4.3+ \u7248\u672c\u6709\u6548\r\nservers.AddDapper("Data Source=./Furion.db", SqlProvider.Sqlite, () => {\r\n DefaultTypeMap.MatchNamesWithUnderscores = true;\r\n})\n'})}),"\n",(0,s.jsx)(r.h3,{id:"10222-\u5b89\u88c5\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u63d0\u4f9b\u5668",children:"10.2.2.2 \u5b89\u88c5\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u63d0\u4f9b\u5668"}),"\n",(0,s.jsxs)(r.ul,{children:["\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"SqlServer"}),"\uff1a",(0,s.jsx)(r.code,{children:"Microsoft.Data.SqlClient"})]}),"\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"Sqlite"}),"\uff1a",(0,s.jsx)(r.code,{children:"Microsoft.Data.Sqlite"})]}),"\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"MySql"}),"\uff1a",(0,s.jsx)(r.code,{children:"MySql.Data"})]}),"\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"Npgsql"}),"\uff1a",(0,s.jsx)(r.code,{children:"Npgsql"})]}),"\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"Oracle"}),"\uff1a",(0,s.jsx)(r.code,{children:"Oracle.ManagedDataAccess.Core"})]}),"\n",(0,s.jsxs)(r.li,{children:[(0,s.jsx)(r.code,{children:"Firebird"}),"\uff1a",(0,s.jsx)(r.code,{children:"FirebirdSql.Data.FirebirdClient"})]}),"\n"]}),"\n",(0,s.jsx)(r.admonition,{title:"\u5b89\u88c5\u62d3\u5c55\u5305\u4f4d\u7f6e",type:"important",children:(0,s.jsxs)(r.p,{children:["\u5728 ",(0,s.jsx)(r.code,{children:"Furion"})," \u6846\u67b6\u4e2d\uff0c\u63a8\u8350\u5c06\u62d3\u5c55\u5305 ",(0,s.jsx)(r.code,{children:"Furion.Extras.DatabaseAccessor.Dapper"})," \u5b89\u88c5\u5230 ",(0,s.jsx)(r.code,{children:"Furion.Core"})," \u5c42\u4e2d\u3002"]})}),"\n",(0,s.jsx)(r.h2,{id:"1023-\u57fa\u672c\u4f7f\u7528",children:"10.2.3 \u57fa\u672c\u4f7f\u7528"}),"\n",(0,s.jsxs)(r.p,{children:["\u5728\u4f7f\u7528\u4e4b\u524d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u51fd\u6570\u6ce8\u5165 ",(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u6216 ",(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u63a5\u53e3\uff0c\u5982\uff1a"]}),"\n",(0,s.jsxs)(r.ul,{children:["\n",(0,s.jsx)(r.li,{children:"\u975e\u6cdb\u578b\u7248\u672c"}),"\n"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:"private readonly IDapperRepository _dapperRepository;\r\npublic PersonService(IDapperRepository dapperRepository)\r\n{\r\n _dapperRepository = dapperRepository;\r\n}\n"})}),"\n",(0,s.jsxs)(r.ul,{children:["\n",(0,s.jsx)(r.li,{children:"\u6cdb\u578b\u7248\u672c"}),"\n"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:"private readonly IDapperRepository _personRepository;\r\npublic PersonService(IDapperRepository personRepository)\r\n{\r\n _personRepository = personRepository;\r\n}\n"})}),"\n",(0,s.jsxs)(r.h3,{id:"10231-sql-\u64cd\u4f5c",children:["10.2.3.1 ",(0,s.jsx)(r.code,{children:"sql"})," \u64cd\u4f5c"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'var data = _dapperRepository.Query("select * from person");\r\nvar data = await _dapperRepository.QueryAsync("select * from person");\r\n\r\nvar data = _dapperRepository.Query("select * from person");\r\n\r\nvar guid = Guid.NewGuid();\r\nvar dog = _dapperRepository.Query("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });\n'})}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'var count = _dapperRepository.Execute(@"insert MyTable(colA, colB) values (@a, @b)",\r\n new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }\r\n );\r\n\r\nvar user = _dapperRepository.Query("spGetUser", new {Id = 1},\r\n commandType: CommandType.StoredProcedure).SingleOrDefault();\n'})}),"\n",(0,s.jsx)(r.p,{children:"\u7528\u6cd5\u548c\u5b98\u65b9\u4e00\u81f4\uff0c\u6b64\u5904\u4e0d\u518d\u4e3e\u66f4\u591a\u4f8b\u5b50\u3002"}),"\n",(0,s.jsxs)(r.h3,{id:"10232-tentity-\u64cd\u4f5c",children:["10.2.3.2 ",(0,s.jsx)(r.code,{children:""})," \u64cd\u4f5c"]}),"\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.code,{children:"Furion"})," \u6846\u67b6\u63d0\u4f9b\u4e86 ",(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u548c ",(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u4e24\u4e2a\u64cd\u4f5c\u4ed3\u50a8\uff0c\u540e\u8005\u7ee7\u627f\u524d\u8005\u3002\u4f7f\u7528\u5982\u4e0b\uff1a"]}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:"var person = personRepository.Get(1);\r\nvar persons = personRepository.GetAll();\r\n\r\nvar effects = personRepository.Insert(person);\r\nvar effects = personRepository.Update(person);\r\nvar effects = personRepository.Delete(person);\r\n\r\nvar effects = personRepository.Insert(persons); // \u63d2\u5165\u591a\u4e2a\r\nvar effects = personRepository.Update(persons); // \u66f4\u65b0\u591a\u4e2a\r\nvar effects = personRepository.Delete(persons); // \u5220\u9664\u591a\u4e2a\r\n\r\nvar effects = await personRepository.InsertAsync(person);\n"})}),"\n",(0,s.jsx)(r.h2,{id:"1024-\u9ad8\u7ea7\u4f7f\u7528",children:"10.2.4 \u9ad8\u7ea7\u4f7f\u7528"}),"\n",(0,s.jsxs)(r.p,{children:[(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u548c ",(0,s.jsx)(r.code,{children:"IDapperRepository"})," \u4ed3\u50a8\u63d0\u4f9b\u4e86 ",(0,s.jsx)(r.code,{children:"Context"})," \u548c ",(0,s.jsx)(r.code,{children:"DynamicContext"})," \u5c5e\u6027\uff0c\u8be5\u5c5e\u6027\u8fd4\u56de ",(0,s.jsx)(r.code,{children:"IDbConnection"})," \u5bf9\u8c61\u3002"]}),"\n",(0,s.jsxs)(r.p,{children:["\u62ff\u5230\u8be5\u5bf9\u8c61\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u64cd\u4f5c ",(0,s.jsx)(r.code,{children:"Dapper"})," \u63d0\u4f9b\u7684\u6240\u6709\u64cd\u4f5c\u4e86\uff0c\u5982\uff1a"]}),"\n",(0,s.jsx)(r.h3,{id:"10241-\u67e5\u8be2\u4e00\u5bf9\u4e00",children:"10.2.4.1 \u67e5\u8be2\u4e00\u5bf9\u4e00"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'var sql =\r\n@"select * from #Posts p\r\nleft join #Users u on u.Id = p.OwnerId\r\nOrder by p.Id";\r\n\r\nvar data = dapperRepository.Context.Query(sql, (post, user) => { post.Owner = user; return post;});\r\nvar post = data.First();\n'})}),"\n",(0,s.jsx)(r.h3,{id:"10242-\u67e5\u8be2\u591a\u4e2a\u7ed3\u679c",children:"10.2.4.2 \u67e5\u8be2\u591a\u4e2a\u7ed3\u679c"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'var sql =\r\n@"\r\nselect * from Customers where CustomerId = @id\r\nselect * from Orders where CustomerId = @id\r\nselect * from Returns where CustomerId = @id";\r\n\r\nusing (var multi = dapperRepository.Context.QueryMultiple(sql, new {id=selectedId}))\r\n{\r\n var customer = multi.Read().Single();\r\n var orders = multi.Read().ToList();\r\n var returns = multi.Read().ToList();\r\n // ...\r\n}\n'})}),"\n",(0,s.jsx)(r.h3,{id:"10243-\u66f4\u591a\u64cd\u4f5c",children:"10.2.4.3 \u66f4\u591a\u64cd\u4f5c"}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-cs",metastring:"showLineNumbers",children:'var shapes = new List();\r\nusing (var reader = dapperRepository.Context.ExecuteReader("select * from Shapes"))\r\n{\r\n var circleParser = reader.GetRowParser(typeof(Circle));\r\n var squareParser = reader.GetRowParser(typeof(Square));\r\n var triangleParser = reader.GetRowParser(typeof(Triangle));\r\n\r\n var typeColumnIndex = reader.GetOrdinal("Type");\r\n\r\n while (reader.Read())\r\n {\r\n IShape shape;\r\n var type = (ShapeType)reader.GetInt32(typeColumnIndex);\r\n switch (type)\r\n {\r\n case ShapeType.Circle:\r\n \tshape = circleParser(reader);\r\n \tbreak;\r\n case ShapeType.Square:\r\n \tshape = squareParser(reader);\r\n \tbreak;\r\n case ShapeType.Triangle:\r\n \tshape = triangleParser(reader);\r\n \tbreak;\r\n default:\r\n \tthrow new NotImplementedException();\r\n }\r\n\r\n \tshapes.Add(shape);\r\n }\r\n}\n'})}),"\n",(0,s.jsx)(r.h2,{id:"1025-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"10.2.5 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,s.jsx)(r.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,s.jsxs)(r.p,{children:["\u7ed9 Furion \u63d0 ",(0,s.jsx)(r.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,s.jsx)(r.hr,{}),"\n",(0,s.jsx)(r.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,s.jsxs)(r.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,s.jsx)(r.code,{children:"Dapper"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,s.jsx)(r.a,{href:"https://github.com/StackExchange/Dapper",children:"Dapper \u5b98\u7f51"}),"\u3002"]})})]})}function l(e={}){const{wrapper:r}={...(0,i.a)(),...e.components};return r?(0,s.jsx)(r,{...e,children:(0,s.jsx)(p,{...e})}):p(e)}},1151:(e,r,n)=>{n.d(r,{Z:()=>d,a:()=>t});var s=n(7294);const i={},o=s.createContext(i);function t(e){const r=s.useContext(o);return s.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function d(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:t(e.components),s.createElement(o.Provider,{value:r},e.children)}}}]);