You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
9.7 KiB
JavaScript

"use strict";(self.webpackChunkfurion=self.webpackChunkfurion||[]).push([[4823],{380:(n,e,s)=>{s.r(e),s.d(e,{assets:()=>c,contentTitle:()=>t,default:()=>u,frontMatter:()=>r,metadata:()=>a,toc:()=>d});var o=s(5893),i=s(1151),l=s(4996);const r={slug:"global-usings",title:"7. GlobalUsings \u7684\u4f7f\u7528",author:"dotNET China",author_title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",author_url:"https://gitee.com/dotnetchina",author_image_url:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png",tags:["furion","furos",".net",".netcore",".net5",".net6"]},t=void 0,a={permalink:"/blog/global-usings",editUrl:"https://gitee.com/dotnetchina/Furion/tree/v4/handbook/blog/2022-05-31-global-usings.mdx",source:"@site/blog/2022-05-31-global-usings.mdx",title:"7. GlobalUsings \u7684\u4f7f\u7528",description:"\u7b80\u4ecb",date:"2022-05-31T00:00:00.000Z",formattedDate:"May 31, 2022",tags:[{label:"furion",permalink:"/blog/tags/furion"},{label:"furos",permalink:"/blog/tags/furos"},{label:".net",permalink:"/blog/tags/net"},{label:".netcore",permalink:"/blog/tags/netcore"},{label:".net5",permalink:"/blog/tags/net-5"},{label:".net6",permalink:"/blog/tags/net-6"}],readingTime:2.715,hasTruncateMarker:!1,authors:[{name:"dotNET China",title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",url:"https://gitee.com/dotnetchina",imageURL:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png"}],frontMatter:{slug:"global-usings",title:"7. GlobalUsings \u7684\u4f7f\u7528",author:"dotNET China",author_title:"\u8ba9 .NET \u5f00\u53d1\u66f4\u7b80\u5355\uff0c\u66f4\u901a\u7528\uff0c\u66f4\u6d41\u884c\u3002",author_url:"https://gitee.com/dotnetchina",author_image_url:"https://i.loli.net/2021/01/19/M8q5a3OTZWUKicl.png",tags:["furion","furos",".net",".netcore",".net5",".net6"]},unlisted:!1,nextItem:{title:"6. .NET 6 Preview 7 \u5c1d\u9c9c",permalink:"/blog/net6-preview7"}},c={authorsImageUrls:[void 0]},d=[{value:"\u7b80\u4ecb",id:"\u7b80\u4ecb",level:3},{value:"\u5fc5\u8981\u914d\u7f6e",id:"\u5fc5\u8981\u914d\u7f6e",level:3},{value:"\u57fa\u672c\u4f7f\u7528",id:"\u57fa\u672c\u4f7f\u7528",level:3},{value:"\u9ed8\u8ba4\u5168\u5c40 <code>using</code>",id:"\u9ed8\u8ba4\u5168\u5c40-using",level:3}];function g(n){const e={admonition:"admonition",code:"code",h3:"h3",li:"li",ol:"ol",p:"p",pre:"pre",strong:"strong",...(0,i.a)(),...n.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(e.h3,{id:"\u7b80\u4ecb",children:"\u7b80\u4ecb"}),"\n",(0,o.jsxs)(e.p,{children:["\u5728 ",(0,o.jsx)(e.code,{children:".NET6/C#10"})," \u4e4b\u540e\uff0c\u5fae\u8f6f\u65b0\u589e\u4e86 ",(0,o.jsx)(e.code,{children:"GlobalUsings"})," \u673a\u5236\uff0c\u53ef\u4ee5\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u6587\u4ef6\uff0c\u628a\u5e38\u7528\u7684 ",(0,o.jsx)(e.code,{children:"using"})," \u653e\u7f6e\u5176\u4e2d\u3002"]}),"\n",(0,o.jsxs)(e.p,{children:["\u8fd9\u6837 ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u6240\u5728\u7684\u9879\u76ee ",(0,o.jsx)(e.code,{children:".cs"})," \u6587\u4ef6\u5c31\u65e0\u9700\u91cd\u590d ",(0,o.jsx)(e.code,{children:"using"})," \u4e86\uff0c\u5927\u5927\u7684\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u4e5f\u8ba9\u4ee3\u7801\u53d8\u7684\u66f4\u52a0\u7b80\u6d01\u3002"]}),"\n",(0,o.jsx)(e.h3,{id:"\u5fc5\u8981\u914d\u7f6e",children:"\u5fc5\u8981\u914d\u7f6e"}),"\n",(0,o.jsx)(e.p,{children:(0,o.jsxs)(e.strong,{children:["\u542f\u7528 ",(0,o.jsx)(e.code,{children:"GlobalUsings"})," \u673a\u5236\u9700\u8981\u4ee5\u4e0b\u4e24\u4e2a\u6b65\u9aa4\uff1a"]})}),"\n",(0,o.jsxs)(e.ol,{children:["\n",(0,o.jsxs)(e.li,{children:["\u5728\u4f60\u9700\u8981\u5168\u5c40 ",(0,o.jsx)(e.code,{children:"using"})," \u7684\u9879\u76ee\u5c42\u6839\u76ee\u5f55\u521b\u5efa ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u6587\u4ef6\uff0c\u5982\u679c\u591a\u4e2a\u9879\u76ee\u5c42\u9700\u8981\uff0c\u5219\u6bcf\u4e2a\u5c42\u90fd\u5e94\u8be5\u6709\u4e00\u4e2a ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})]}),"\n",(0,o.jsxs)(e.li,{children:["\u7f16\u8f91\u9879\u76ee\u7684 ",(0,o.jsx)(e.code,{children:".csproj"})," \u6587\u4ef6\uff0c\u6dfb\u52a0 ",(0,o.jsx)(e.code,{children:"<ImplicitUsings>enable</ImplicitUsings>"}),"\uff0c\u6ce8\u610f\u662f\u5728 ",(0,o.jsx)(e.code,{children:"<PropertyGroup>"})," \u4e2d\u6dfb\u52a0\uff0c\u901a\u5e38\u548c ",(0,o.jsx)(e.code,{children:"<TargetFramework>"})," \u540c\u7236\u540c\u7ea7"]}),"\n"]}),"\n",(0,o.jsx)(e.h3,{id:"\u57fa\u672c\u4f7f\u7528",children:"\u57fa\u672c\u4f7f\u7528"}),"\n",(0,o.jsxs)(e.p,{children:["\u914d\u7f6e\u4e4b\u540e\uff0c\u73b0\u5728\u5c31\u53ef\u4ee5\u628a\u5e38\u7528\u7684 ",(0,o.jsx)(e.code,{children:"using"})," \u653e\u5230 ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u4e2d\u4e86\uff0c\u5199\u6cd5\u5982\u4e0b\uff1a"]}),"\n",(0,o.jsx)(e.pre,{children:(0,o.jsx)(e.code,{className:"language-cs",metastring:'showLineNumbers title="Furion \u63a8\u8350\u7684\u5168\u5c40\u547d\u540d\u7a7a\u95f4"',children:"global using Furion;\r\nglobal using Furion.DatabaseAccessor;\r\nglobal using Furion.DataEncryption;\r\nglobal using Furion.DataValidation;\r\nglobal using Furion.DependencyInjection;\r\nglobal using Furion.DynamicApiController;\r\nglobal using Furion.Extensions;\r\nglobal using Furion.FriendlyException;\r\nglobal using Mapster;\r\nglobal using Microsoft.AspNetCore.Authorization;\r\nglobal using Microsoft.AspNetCore.Http;\r\nglobal using Microsoft.AspNetCore.Mvc;\r\nglobal using Microsoft.CodeAnalysis;\r\nglobal using Microsoft.EntityFrameworkCore;\r\nglobal using System.ComponentModel.DataAnnotations;\n"})}),"\n",(0,o.jsx)(e.p,{children:(0,o.jsxs)(e.strong,{children:["\u6ce8\u610f\u5fc5\u987b\u4ee5 ",(0,o.jsx)(e.code,{children:"global"})," \u5f00\u5934\uff01"]})}),"\n",(0,o.jsxs)(e.admonition,{title:"\u5c0f\u77e5\u8bc6",type:"tip",children:[(0,o.jsxs)(e.p,{children:["\u4e00\u822c\u63a8\u8350\u628a\u5b9e\u4f53\u7c7b\u7684\u547d\u540d\u7a7a\u95f4\u4e5f\u653e\u8fdb\u53bb\uff0c\u56e0\u4e3a\u4ed3\u50a8 ",(0,o.jsx)(e.code,{children:"IRepository<T>"})," \u4f7f\u7528\u7684\u9891\u7387\u975e\u5e38\u9ad8\u3002"]}),(0,o.jsxs)(e.p,{children:["\u53e6\u5916\u63a8\u8350\u5927\u5bb6\u5728 ",(0,o.jsx)(e.code,{children:"Visual Studio"})," \u4e2d\u5b89\u88c5 ",(0,o.jsx)(e.code,{children:"CodeMaid"})," \u63d2\u4ef6\u54e6\uff0c\u81ea\u52a8\u6e05\u7406\u89e3\u51b3\u65b9\u6848\u6240\u6709\u65e0\u7528\u7684 ",(0,o.jsx)(e.code,{children:"using"}),"\uff0c\u7ed3\u5408 ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u975e\u5e38\u68d2\uff01"]})]}),"\n",(0,o.jsxs)(e.admonition,{title:"\u4e2a\u522b\u60c5\u51b5",type:"important",children:[(0,o.jsxs)(e.p,{children:["\u53ef\u80fd\u7531\u4e8e ",(0,o.jsx)(e.code,{children:"Visual Studio"})," \u7248\u672c\u7684\u95ee\u9898\uff0c\u5bfc\u81f4 ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u5b9a\u4e49\u51fa\u9519\uff0c\u8fd9\u65f6\u5019\u9700\u8981\u5728 ",(0,o.jsx)(e.code,{children:"using"})," \u540e\u9762\u52a0 ",(0,o.jsx)(e.code,{children:"global::"}),"\uff0c\u5982\uff1a"]}),(0,o.jsx)(e.pre,{children:(0,o.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:"global using global::Furion;\n"})})]}),"\n",(0,o.jsx)(e.p,{children:"\u63a5\u4e0b\u6765\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\uff1a"}),"\n",(0,o.jsx)(e.pre,{children:(0,o.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers {1}",children:"// \u65e0\u9700 using Furion \u7684\u547d\u540d\u7a7a\u95f4\u4e86\u54e6\uff0c\u6e05\u723d\u4e86\u4e0d\u5c11\r\n\r\nnamespace Your.Application;\r\n\r\npublic class DefaultAppService : IDynamicApiController\r\n{\r\n private readonly IRepository<BoardCard> _boardCardRepository;\r\n private readonly IRepository<BoardGroup> _boardGroupRepository;\r\n private readonly IRepository<BoardCardAttachment> _boardCardAttachmentRepository;\r\n private readonly IRepository<BoardCardUser> _boardCardUserRepository;\r\n}\r\n\r\n// ....\n"})}),"\n",(0,o.jsx)("img",{src:(0,l.Z)("img/cd2223.png")}),"\n",(0,o.jsxs)(e.h3,{id:"\u9ed8\u8ba4\u5168\u5c40-using",children:["\u9ed8\u8ba4\u5168\u5c40 ",(0,o.jsx)(e.code,{children:"using"})]}),"\n",(0,o.jsxs)(e.p,{children:[(0,o.jsxs)(e.strong,{children:["\u5b9e\u9645\u4e0a\u5fae\u8f6f\u5df2\u7ecf\u81ea\u52a8\u628a\u4e00\u4e9b\u5e38\u7528\u7684 ",(0,o.jsx)(e.code,{children:"using"})," \u5728\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u4e2d\u81ea\u52a8\u8865\u4e0a\u4e86"]}),"\uff0c\u8def\u5f84\u5728 ",(0,o.jsx)(e.code,{children:"\u9879\u76ee/obj/Debug/net6.0/\u9879\u76ee.GlobalUsings.cs"})," \u6587\u4ef6\u4e2d\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a"]}),"\n",(0,o.jsx)(e.pre,{children:(0,o.jsx)(e.code,{className:"language-cs",metastring:"showLineNumbers",children:"// <auto-generated/>\r\nglobal using global::System;\r\nglobal using global::System.Collections.Generic;\r\nglobal using global::System.IO;\r\nglobal using global::System.Linq;\r\nglobal using global::System.Net.Http;\r\nglobal using global::System.Threading;\r\nglobal using global::System.Threading.Tasks;\n"})}),"\n",(0,o.jsx)(e.p,{children:(0,o.jsxs)(e.strong,{children:["\u4e5f\u5c31\u662f\u4ee5\u4e0a\u7684 ",(0,o.jsx)(e.code,{children:"using"})," \u65e0\u9700\u5199\u5728\u4f60\u521b\u5efa\u7684 ",(0,o.jsx)(e.code,{children:"GlobalUsings.cs"})," \u4e2d\u4e86\uff0c\u5fae\u8f6f\u4f1a\u5728\u7f16\u8bd1\u65f6\u81ea\u52a8\u5408\u5e76\u3002"]})})]})}function u(n={}){const{wrapper:e}={...(0,i.a)(),...n.components};return e?(0,o.jsx)(e,{...n,children:(0,o.jsx)(g,{...n})}):g(n)}},1151:(n,e,s)=>{s.d(e,{Z:()=>t,a:()=>r});var o=s(7294);const i={},l=o.createContext(i);function r(n){const e=o.useContext(l);return o.useMemo((function(){return"function"==typeof n?n(e):{...e,...n}}),[e,n])}function t(n){let e;return e=n.disableParentContext?"function"==typeof n.components?n.components(i):n.components||i:r(n.components),o.createElement(l.Provider,{value:e},n.children)}}}]);