《Asp.Net Core3 + Vue3入坑教程》 此教程适合新手入门或者前后端分离尝试者。可以根据图文一步一步进操作编码也可以选择直接查看源码。每一篇文章都有对应的源码
教程后期会将 .Net Core 3升级成 .Net Core 5
目录 《Asp.Net Core3 + Vue3入坑教程》系列教程目录Asp.Net Core后端项目
后端项目搭建与Swagger配置步骤
配置CROS策略解决跨域问题
(本文)AutoMapper & Restful API & DI
(暂未发表敬请期待...)EF Core & Postgresql
(暂未发表敬请期待...).Net Core 3升级成 .Net Core 5
(暂未发表敬请期待...)JWT
Vue3 前端项目
暂未发表敬请期待...
本文简介本文为《Asp.Net Core3 + Vue3入坑教程》系列教程的后端第三篇 - AutoMapper & Restful API & DI。本文将利用AutoMapper与依赖注入等内容实现一个简单的Restful API。
AutoMapper & Restful API 引入NewtonsoftJson3.1.12版本的Nuget包当前项目使用的SKD是 .net core 3后续将SDK升级之后再升级此Nuget包的版本
配置Startup.cs代码如下:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json.Serialization; using Simple_Asp.Net_Core.Data; using Simple_Asp.Net_Core.ServiceProvider; using System; namespace Simple_Asp.Net_Core { public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddCORS(); services.AddMvc(); services.AddSwagger(); services.AddControllers().AddNewtonsoftJson(s => { s.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1"); }); } app.UseCors("CorsTest"); app.UseRouting(); app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute()); } } } 新建文件夹Models,新建实体Command.cs代码如下: using System.ComponentModel.DataAnnotations; namespace Simple_Asp.Net_Core.Models { public class Command { [Key] [Required] public int Id { get; set; } [Required] [MaxLength(250)] public string HowTo { get; set; } [Required] public string Line { get; set; } [Required] public string Platform { get; set; } } } 新建Data文件夹 新建 ICommanderRepo 仓储层接口,用来定义与数据库交互的接口 using Simple_Asp.Net_Core.Models; using System.Collections.Generic; namespace Simple_Asp.Net_Core.Data { public interface ICommanderRepo { IEnumerable<Command> GetAllCommands(); } } 现在我们还没有数据库,就先模拟数据返回!新建MockCommanderRepo.cs来实现ICommanderRepo接口 using Simple_Asp.Net_Core.Models; using System.Collections.Generic; namespace Simple_Asp.Net_Core.Data { public class MockCommanderRepo : ICommanderRepo { public IEnumerable<Command> GetAllCommands() { var commands = new List<Command> { new Command{Id=0, HowTo="Boil an egg", Line="Boil water", Platform="Kettle & Pan"}, new Command{Id=1, HowTo="Cut bread", Line="Get a knife", Platform="knife & chopping board"}, new Command{Id=2, HowTo="Make cup of tea", Line="Place teabag in cup", Platform="Kettle & cup"} }; return commands; } } } 新建Dtos文件夹,新建类CommandReadDto.cs
上一步模拟实现了从数据库返回Command实体,但是在返回给前端的时候不能直接返回实体,而是需要转换成Dto。根据不同的业务场景需要建立不同的Dto
namespace Simple_Asp.Net_Core.Dtos { public class CommandReadDto { public int Id { get; set; } public string HowTo { get; set; } public string Line { get; set; } } } 实现ICommanderRepo的依赖注入生命周期是依赖注入里非常重要的内容,具体可以参照官方的文档
这里使用的注册方式是AddScoped,让每一次请求都会创建一个实例
For web applications, a scoped lifetime indicates that services are created once per client request (connection). Register scoped services with AddScoped.