"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[3201],{8231:(e,n,r)=>{r.r(n),r.d(n,{assets:()=>o,contentTitle:()=>d,default:()=>p,frontMatter:()=>i,metadata:()=>c,toc:()=>a});var t=r(5893),s=r(1151);const i={id:"object-mapper",title:"13. \u5bf9\u8c61\u6570\u636e\u6620\u5c04",sidebar_label:"13. \u5bf9\u8c61\u6570\u636e\u6620\u5c04 (Mapper)"},d=void 0,c={id:"object-mapper",title:"13. \u5bf9\u8c61\u6570\u636e\u6620\u5c04",description:"Furion \u6846\u67b6\u63d0\u4f9b\u4e86 Mapster \u7684\u62d3\u5c55 Furion.Extras.ObjectMapper.Mapster\uff0c\u5982\u9700\u4f7f\u7528\u7b2c\u4e09\u65b9\u5982 AutoMapper \u5219\u65e0\u9700\u5b89\u88c5\u6b64\u62d3\u5c55\u3002",source:"@site/docs/object-mapper.mdx",sourceDirName:".",slug:"/object-mapper",permalink:"/docs/object-mapper",draft:!1,unlisted:!1,editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/docs/object-mapper.mdx",tags:[],version:"current",lastUpdatedBy:"\u767e\u5c0f\u50e7",lastUpdatedAt:1695741411,formattedLastUpdatedAt:"Sep 26, 2023",frontMatter:{id:"object-mapper",title:"13. \u5bf9\u8c61\u6570\u636e\u6620\u5c04",sidebar_label:"13. \u5bf9\u8c61\u6570\u636e\u6620\u5c04 (Mapper)"},sidebar:"docs",previous:{title:"12. \u4f9d\u8d56\u6ce8\u5165/\u63a7\u5236\u53cd\u8f6c",permalink:"/docs/dependency-injection"},next:{title:"14. \u5206\u5e03\u5f0f\u7f13\u5b58",permalink:"/docs/cache"}},o={},a=[{value:"13.1 \u5bf9\u8c61\u6620\u5c04",id:"131-\u5bf9\u8c61\u6620\u5c04",level:2},{value:"13.2 \u5148\u770b\u4f8b\u5b50",id:"132-\u5148\u770b\u4f8b\u5b50",level:2},{value:"13.3 Mapster
\u4f7f\u7528",id:"133-mapster-\u4f7f\u7528",level:2},{value:"13.3.1 \u5feb\u901f\u5165\u95e8",id:"1331-\u5feb\u901f\u5165\u95e8",level:3},{value:"13.3.2 \u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219",id:"1332-\u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219",level:3},{value:"13.3.3 \u4f9d\u8d56\u6ce8\u5165\u65b9\u5f0f",id:"1333-\u4f9d\u8d56\u6ce8\u5165\u65b9\u5f0f",level:3},{value:"13.3.4 \u548c EFCore
\u914d\u5408",id:"1334-\u548c-efcore-\u914d\u5408",level:3},{value:"13.4 \u5168\u5c40\u9ed8\u8ba4\u914d\u7f6e",id:"134-\u5168\u5c40\u9ed8\u8ba4\u914d\u7f6e",level:2},{value:"13.5 \u53cd\u9988\u4e0e\u5efa\u8bae",id:"135-\u53cd\u9988\u4e0e\u5efa\u8bae",level:2}];function l(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",hr:"hr",mdxAdmonitionTitle:"mdxAdmonitionTitle",p:"p",pre:"pre",strong:"strong",...(0,s.a)(),...e.components};return(0,t.jsxs)(t.Fragment,{children:[(0,t.jsxs)(n.admonition,{type:"important",children:[(0,t.jsxs)(n.mdxAdmonitionTitle,{children:["\u4f7f\u7528 ",(0,t.jsx)(n.code,{children:"Mapster"})," \u4ee5\u5916\u7684\u62d3\u5c55\u8bf4\u660e"]}),(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u63d0\u4f9b\u4e86 ",(0,t.jsx)(n.code,{children:"Mapster"})," \u7684\u62d3\u5c55 ",(0,t.jsx)(n.code,{children:"Furion.Extras.ObjectMapper.Mapster"}),"\uff0c\u5982\u9700\u4f7f\u7528\u7b2c\u4e09\u65b9\u5982 ",(0,t.jsx)(n.code,{children:"AutoMapper"})," \u5219\u65e0\u9700\u5b89\u88c5\u6b64\u62d3\u5c55\u3002"]})]}),"\n",(0,t.jsx)(n.h2,{id:"131-\u5bf9\u8c61\u6620\u5c04",children:"13.1 \u5bf9\u8c61\u6620\u5c04"}),"\n",(0,t.jsxs)(n.p,{children:["\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7684\u6570\u636e\u6839\u636e\u7279\u5b9a\u89c4\u5219\u6279\u91cf\u6620\u5c04\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u51cf\u5c11\u624b\u5de5\u64cd\u4f5c\u548c\u964d\u4f4e\u4eba\u4e3a\u51fa\u9519\u7387\u3002\u5982\u5c06 ",(0,t.jsx)(n.code,{children:"DTO"})," \u5bf9\u8c61\u6620\u5c04\u5230 ",(0,t.jsx)(n.code,{children:"Entity"})," \u5b9e\u4f53\u4e2d\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002"]}),"\n",(0,t.jsx)(n.h2,{id:"132-\u5148\u770b\u4f8b\u5b50",children:"13.2 \u5148\u770b\u4f8b\u5b50"}),"\n",(0,t.jsx)(n.p,{children:"\u5728\u8fc7\u53bb\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e00\u4e2a\u5bf9\u8c61\u7684\u503c\u8f6c\u6362\u5230\u53e6\u4e00\u4e2a\u5bf9\u8c61\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u8fd9\u6837\u505a\uff0c\u5982\uff1a"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {4-9}",children:'var entity = repository.Find(1);\r\n\r\nvar dto = new Dto();\r\ndto.Id = entity.Id;\r\ndto.Name = entity.Name;\r\ndto.Age = entity.Age;\r\ndto.Address = entity.Address;\r\ndto.FullName = entity.FirstName + entity.LastName;\r\ndto.IdCard = entity.IdCard.Replace("1234", "****");\n'})}),"\n",(0,t.jsx)(n.p,{children:"\u4e0a\u9762\u7684\u4f8b\u5b50\u4f3c\u4e4e\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u4f46\u662f\u5982\u679c\u5f88\u591a\u5730\u65b9\u9700\u8981\u8fd9\u6837\u7684\u8d4b\u503c\u64cd\u4f5c\u3001\u6216\u8005\u76f8\u540c\u7684\u8d4b\u503c\u64cd\u4f5c\u5728\u591a\u4e2a\u5730\u65b9\u4f7f\u7528\uff0c\u53c8\u6216\u8005\u4e00\u4e2a\u7c7b\u4e2d\u542b\u6709\u975e\u5e38\u591a\u7684\u5c5e\u6027\u6216\u81ea\u5b9a\u4e49\u8d4b\u503c\u64cd\u4f5c\u3002\u90a3\u4e48\u8fd9\u6837\u7684\u64cd\u4f5c\u6548\u7387\u6781\u4f4e\uff0c\u5bb9\u6613\u51fa\u9519\uff0c\u4e14\u4ee3\u7801\u975e\u5e38\u81c3\u80bf\u548c\u5197\u4f59\u3002"}),"\n",(0,t.jsxs)(n.p,{children:["\u6240\u4ee5\uff0c\u5b9e\u73b0\u81ea\u52a8\u6620\u5c04\u8d4b\u503c\u548c\u652f\u6301\u7279\u6b8a\u914d\u7f6e\u7684\u9700\u6c42\u5c31\u6709\u4e86\u3002\u76ee\u524d ",(0,t.jsx)(n.code,{children:"C#"})," \u5e73\u53f0\u6709\u4e24\u4e2a\u4f18\u79c0\u7684\u5bf9\u8c61\u6620\u5c04\u5de5\u5177\uff1a",(0,t.jsx)(n.code,{children:"Mapster"})," \u548c ",(0,t.jsx)(n.code,{children:"AutoMapper"}),"\u3002",(0,t.jsxs)(n.strong,{children:["\u5728 ",(0,t.jsx)(n.code,{children:"Furion"})," \u6846\u67b6\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528 ",(0,t.jsx)(n.a,{href:"https://github.com/MapsterMapper/Mapster",children:"Mapster"}),"\uff0c",(0,t.jsx)(n.a,{href:"https://github.com/MapsterMapper/Mapster",children:"Mapster"})," \u662f\u4e00\u6b3e\u6781\u6613\u4f7f\u7528\u4e14\u8d85\u9ad8\u6027\u80fd\u7684\u5bf9\u8c61\u6620\u5c04\u6846\u67b6\u3002"]})]}),"\n",(0,t.jsxs)(n.h2,{id:"133-mapster-\u4f7f\u7528",children:["13.3 ",(0,t.jsx)(n.code,{children:"Mapster"})," \u4f7f\u7528"]}),"\n",(0,t.jsxs)(n.p,{children:["\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 ",(0,t.jsx)(n.code,{children:"Mapster"})," \u63d0\u4f9b\u7684\u5bf9\u8c61\u6620\u5c04\u65b9\u6cd5\uff1a",(0,t.jsx)(n.code,{children:"Adapt"})," \u65b9\u6cd5\u6539\u9020\u4e0a\u9762\u7684\u4f8b\u5b50\uff1a"]}),"\n",(0,t.jsx)(n.admonition,{title:"\u5b89\u88c5\u62d3\u5c55\u5305",type:"important",children:(0,t.jsxs)(n.p,{children:["\u5728 ",(0,t.jsx)(n.code,{children:"Furion.Core"})," \u5c42\u5b89\u88c5 ",(0,t.jsx)(n.code,{children:"Furion.Extras.ObjectMapper.Mapster"})," \u62d3\u5c55\u5305\uff0c\u65e0\u9700\u624b\u52a8\u8c03\u7528\uff0c",(0,t.jsx)(n.code,{children:"Furion"})," \u4f1a\u81ea\u52a8\u52a0\u8f7d\u5e76\u8c03\u7528\u3002"]})}),"\n",(0,t.jsx)(n.h3,{id:"1331-\u5feb\u901f\u5165\u95e8",children:"13.3.1 \u5feb\u901f\u5165\u95e8"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {2}",children:"var entity = repository.Find(1);\r\nvar dto = entity.Adapt();\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u4ec5\u4ec5\u4e00\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u5b9e\u73b0 ",(0,t.jsx)(n.code,{children:"entity -> dto"})," \u7684\u8f6c\u6362\uff0c\u5982\u679c\u6d89\u53ca\u5230\u8d4b\u503c\u7684\u590d\u5236\u64cd\u4f5c\uff0c\u5982 ",(0,t.jsx)(n.code,{children:"dto.FullName"})," \u548c ",(0,t.jsx)(n.code,{children:"dto.IdCard"}),"\uff0c\u6211\u4eec\u53ea\u9700\u8981\u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219\u7c7b\u5373\u53ef\u3002"]}),"\n",(0,t.jsx)(n.h3,{id:"1332-\u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219",children:"13.3.2 \u81ea\u5b9a\u4e49\u6620\u5c04\u89c4\u5219"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1,6,10-12}",children:'using Mapster;\r\nusing System;\r\n\r\nnamespace Furion.Application\r\n{\r\n public class Mapper : IRegister\r\n {\r\n public void Register(TypeAdapterConfig config)\r\n {\r\n config.ForType()\r\n .Map(dest => dest.FullName, src => src.FirstName + src.LastName)\r\n .Map(dest => dest.IdCard, src => src.IdCard.Replace("1234", "****"));\r\n }\r\n }\r\n}\n'})}),"\n",(0,t.jsx)(n.admonition,{title:"\u5c0f\u77e5\u8bc6",type:"tip",children:(0,t.jsxs)(n.p,{children:["\u8be5\u6620\u5c04\u6587\u4ef6 ",(0,t.jsx)(n.code,{children:"Mapper.cs"})," \u53ef\u4ee5\u653e\u5728\u4efb\u4f55\u9879\u76ee\u6216\u6587\u4ef6\u5939\u4e2d\uff0c",(0,t.jsx)(n.code,{children:"Furion"})," \u4f1a\u5728\u7a0b\u5e8f\u542f\u52a8\u7684\u65f6\u5019\u81ea\u52a8\u626b\u63cf\u5e76\u6ce8\u5165\u914d\u7f6e\u3002"]})}),"\n",(0,t.jsx)(n.h3,{id:"1333-\u4f9d\u8d56\u6ce8\u5165\u65b9\u5f0f",children:"13.3.3 \u4f9d\u8d56\u6ce8\u5165\u65b9\u5f0f"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Mapster"})," \u9664\u4e86\u63d0\u4f9b ",(0,t.jsx)(n.code,{children:"Adapt"})," \u62d3\u5c55\u65b9\u6cd5\u4ee5\u5916\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u3002"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"public Person(IMapper mapper)\r\n{\r\n var dto = _mapper.Map(entity);\r\n}\n"})}),"\n",(0,t.jsxs)(n.h3,{id:"1334-\u548c-efcore-\u914d\u5408",children:["13.3.4 \u548c ",(0,t.jsx)(n.code,{children:"EFCore"})," \u914d\u5408"]}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Mapster"})," \u8fd8\u63d0\u4f9b\u4e86 ",(0,t.jsx)(n.code,{children:"ProjectToType"})," Linq \u62d3\u5c55\u65b9\u6cd5\u51cf\u5c11\u6211\u4eec\u624b\u52a8 ",(0,t.jsx)(n.code,{children:"Select"})," \u64cd\u4f5c\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.p,{children:"\u6b63\u5e38\u7684\u64cd\u4f5c\uff1a"}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {2-7}",children:"var destinations = context.Sources\r\n .Select(p => new Destination {\r\n Id = p.Id,\r\n Name = p.Name,\r\n Surname = p.Surname,\r\n ....\r\n })\r\n .ToList();\n"})}),"\n",(0,t.jsxs)(n.p,{children:["\u4f7f\u7528 ",(0,t.jsx)(n.code,{children:"Mapster"})," \u4e4b\u540e\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers {2}",children:" var destinations = context.Sources\r\n .ProjectToType()\r\n .ToList();\n"})}),"\n",(0,t.jsx)(n.h2,{id:"134-\u5168\u5c40\u9ed8\u8ba4\u914d\u7f6e",children:"13.4 \u5168\u5c40\u9ed8\u8ba4\u914d\u7f6e"}),"\n",(0,t.jsxs)(n.p,{children:[(0,t.jsx)(n.code,{children:"Furion"})," \u63d0\u4f9b\u5168\u5c40\u9ed8\u8ba4\u6620\u5c04\u914d\u7f6e\u9009\u9879 ",(0,t.jsx)(n.code,{children:"TypeAdapterConfig.GlobalSettings.Default"}),"\uff0c\u53ef\u5728 ",(0,t.jsx)(n.code,{children:"Startup"})," \u4e2d\u914d\u7f6e\u5373\u53ef\uff0c\u5982\uff1a"]}),"\n",(0,t.jsx)(n.pre,{children:(0,t.jsx)(n.code,{className:"language-cs",metastring:"showLineNumbers",children:"TypeAdapterConfig.GlobalSettings.Default\r\n .PreserveReference(true);\n"})}),"\n",(0,t.jsx)(n.h2,{id:"135-\u53cd\u9988\u4e0e\u5efa\u8bae",children:"13.5 \u53cd\u9988\u4e0e\u5efa\u8bae"}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e0e\u6211\u4eec\u4ea4\u6d41",type:"note",children:(0,t.jsxs)(n.p,{children:["\u7ed9 Furion \u63d0 ",(0,t.jsx)(n.a,{href:"https://gitee.com/dotnetchina/Furion/issues/new?issue",children:"Issue"}),"\u3002"]})}),"\n",(0,t.jsx)(n.hr,{}),"\n",(0,t.jsx)(n.admonition,{title:"\u4e86\u89e3\u66f4\u591a",type:"note",children:(0,t.jsxs)(n.p,{children:["\u60f3\u4e86\u89e3\u66f4\u591a ",(0,t.jsx)(n.code,{children:"Mapster"})," \u77e5\u8bc6\u53ef\u67e5\u9605 ",(0,t.jsx)(n.a,{href:"https://github.com/MapsterMapper/Mapster/wiki",children:"Mapster - Wiki"})," \u6587\u6863\u3002"]})})]})}function p(e={}){const{wrapper:n}={...(0,s.a)(),...e.components};return n?(0,t.jsx)(n,{...e,children:(0,t.jsx)(l,{...e})}):l(e)}},1151:(e,n,r)=>{r.d(n,{Z:()=>c,a:()=>d});var t=r(7294);const s={},i=t.createContext(s);function d(e){const n=t.useContext(i);return t.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(s):e.components||s:d(e.components),t.createElement(i.Provider,{value:n},e.children)}}}]);