2020-10-14

手撸ORM浅谈ORM框架之基础篇

好奇害死猫

一直觉得ORM框架好用、功能强大集众多优点于一身,当然ORM并非完美无缺,任何事物优缺点并存!我曾一度认为以为使用了ORM框架根本不需要关注Sql语句如何执行的,更不用关心优化的问题!!! 随着发际线后移高亮意识到优秀程序员写的优秀的ORM框架会做一些Sql优化,Sql优化不是一成不变的,ORM框架不会根据项目业务场景等主动优化Sql语句。如果ORM真的强大的到开发人员不需要关注Sql,会针对当前项目情况做出相对应很好的优化,必然会增加ORM框架的体积、带来性能等相关问题。知己知彼,百战不殆。一直想探索ORM原理,为什么调用ORM Add或Insert方法都可以把实体写入到数据库???由于好奇之心我准备写一个ORM系列随笔,浅谈对ORM的理解,作者水平有限欢迎园友纠正错误及不恰当之处。

千里之行始于足下


ORM对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序设计技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。

技术栈: .NET Core 3.1 + MySql + Autofac + Swagger

官方地址传送:

微软NET Core 3.1: 中文https://docs.microsoft.com/zh-cn/dotnet/   英文https://docs.microsoft.com/en-us/dotnet/

MySql 8.x 文档: https://dev.mysql.com/doc/

Autofac文档: 中文https://autofaccn.readthedocs.io/zh/latest/   英文https://autofaccn.readthedocs.io/en/latest/

Swagger文档: https://swagger.io/docs/

目前计划

手撸ORM浅谈ORM框架之基础篇

手撸ORM浅谈ORM框架之Add篇

手撸ORM浅谈ORM框架之Update篇

手撸ORM浅谈ORM框架之Delete篇

手撸ORM浅谈ORM框架之Query篇

后续待定。。。。。。

MySql 8.x 坎(待解决)


 //Grant all privileges on learndb.* to 'learn_user@'@'%'

//> 1410 - You are not allowed to create a user with GRANT
//Sql = @"CREATE DATABASE IF NOT EXISTS learndb;"

因此,权限需要先手动授予: 服务器权限-》Create

注:遇到的项目中一般都是数据库优先或者使用的EntityFramework来创建数据库,MySql8.x命令授权给用户不太熟练。

 新风尚WebApi

.NET Framework使用静态资源除了保护文件夹可以直接添加需要的静态资源文件夹即可;.NET Core使用静态文件需要在Startup-》Configure 方法中启用静态文件UseStaticFiles并且文件夹的名称:wwwroot,否则禁止访问静态资源。WebApi添加wwwroot文件夹存放静态资源,.Net Core中需要手动添加swagger静态样式资源,下载地址 : swagger-ui ,您也可以使用项目中的wwwroot里面的样式。

Api神器Swagger

Startup-》ConfigureServices 方法中添加Swagger 文档doc、

System.UriFormatException
HResult=0x80131537
Message=Invalid URI: The format of the URI could not be determined.

 1 private const string ProjectName = "Learn.WebApi"; 2  3 // This method gets called by the runtime. Use this method to add services to the container. 4 public void ConfigureServices(IServiceCollection services) 5 { 6  #region Swagger 7  8  services.AddSwaggerGen(c => 9  {10   c.SwaggerDoc("v1", new OpenApiInfo11   {12    Title = ProjectName,13    Version = "v1",14    Description = $"{ProjectName} HTTP API ",15    TermsOfService = new Uri("https://github.com/dingshuanglei"),16    Contact = new OpenApiContact { Name = "丁双磊", Email = "shuangleiding@163.com", Url = new Uri("https://github.com/dingshuanglei") }17   });18 19   var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);20   var "{ProjectName}.");21   c.Includetrue);22   var model"Learn.Model.");23   c.Includetrue);24 25   // Add security definitions26   c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()27   {28    Description = "Please enter into field the word 'Bearer' followed by a space and the JWT value",29    Name = "token",30    In = ParameterLocation.Header,31    Type = SecuritySchemeType.ApiKey,32   });33   c.AddSecurityRequirement(new OpenApiSecurityRequirement34   {35    {36     new OpenApiSecurityScheme37     {38      Reference = new OpenApiReference()39      {40       Id = "Bearer",41       Type = ReferenceType.SecurityScheme42      }43     }, Array.Empty<string>()44    }45   });46  });47 48  

No comments:

Post a Comment