From a60a17fb85fd1b3b1dd05c5b0f1cc97b44f77459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=A9=89=E5=A9=B7?= <1029854305@qq.com> Date: Sun, 30 Jun 2024 23:00:27 +0800 Subject: [PATCH] . --- ...44\345\231\250\346\227\245\345\277\227.md" | 46 ++++--- ...73\351\233\206\345\220\210\347\261\273.md" | 49 ++++---- ...36\347\216\260\346\216\245\345\217\243.md" | 84 +++++++------ ...346\237\245\345\242\236\346\224\271Dto.md" | 118 ++++++++++++++++-- .../20240604.md" | 64 ++++++++++ .../20240605.md" | 2 - .../20240607.md" | 40 ++++-- .../20240617.md" | 1 + .../20240619.md" | 19 +++ .../20240621.md" | 1 + 10 files changed, 330 insertions(+), 94 deletions(-) rename "\347\231\275\345\251\211\345\251\267/20240603.md" => "\347\231\275\345\251\211\345\251\267/20240603\346\237\245\345\242\236\346\224\271Dto.md" (30%) create mode 100644 "\347\231\275\345\251\211\345\251\267/20240604.md" delete mode 100644 "\347\231\275\345\251\211\345\251\267/20240605.md" create mode 100644 "\347\231\275\345\251\211\345\251\267/20240617.md" create mode 100644 "\347\231\275\345\251\211\345\251\267/20240619.md" create mode 100644 "\347\231\275\345\251\211\345\251\267/20240621.md" diff --git "a/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" "b/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" index e547d79..a3cb3c5 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240528mvc\346\216\247\345\210\266\345\231\250\350\277\207\346\273\244\345\231\250\346\227\245\345\277\227.md" @@ -21,6 +21,7 @@ } ## 控制器的基本写法 1. ApiResultFilter.cs + using Microsoft.AspNetCore.Mvc; namespace DemoOne.Controllers { @@ -52,43 +53,47 @@ return CreatedAtAction(nameof(Get), new { id = data.Id }, data); } } + } 2. ApiResult.cs + public class ApiResult { public int Id { get; set; } public string Content { get; set; } } - } ## 过滤器注册使用 处理成功才有结果 1. ApiResultFilter.cs + public class ApiResultFilter : IAsyncResultFilter - { - public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - //判断返回结果是否是内容,是返回Context.Result赋一个新的对象ApiResult - //判断是不是objectresult类型 - if(context.Result is ObjectResult objectResult) + public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - /* var objectResult =(ObjectResult)context.Result; */ + //判断返回结果是否是内容,是返回Context.Result赋一个新的对象ApiResult + //判断是不是objectresult类型 + if(context.Result is ObjectResult objectResult) + { + /* var objectResult =(ObjectResult)context.Result; */ - context.Result =new ObjectResult (new ApiResult + context.Result =new ObjectResult (new ApiResult + { + Code=1000, + Msg="yesss", + Data=objectResult.Value + }); + } + else { - Code=1000, - Msg="yesss", - Data=objectResult.Value - }); - } - else - { - context.Result = new ObjectResult(new ApiResult {Code=1000}); + context.Result = new ObjectResult(new ApiResult {Code=1000}); + } + await next(); //理解为中间件,需要注册为服务 } - await next(); //理解为中间件,需要注册为服务 } - } -2. Startup.cs + +1. Startup.cs + public class Startup { public void Configure(IApplicationBuilder app) @@ -108,6 +113,7 @@ ## 获取appsetting的链接字符串,注入数据库的上下文服务 1. appsettings.json文件中添加数据库的链接字符串 + { "AllowedHosts": "*", "ConnectionStrings": { @@ -115,7 +121,9 @@ "pgsql":"" } } + 2. Startup.cs文件配置引用 + public class Startup { public void ConfigureServices(IServiceCollection services) diff --git "a/\347\231\275\345\251\211\345\251\267/20240530api\346\216\247\345\210\266\345\231\250\345\256\236\344\275\223\347\261\273\351\233\206\345\220\210\347\261\273.md" "b/\347\231\275\345\251\211\345\251\267/20240530api\346\216\247\345\210\266\345\231\250\345\256\236\344\275\223\347\261\273\351\233\206\345\220\210\347\261\273.md" index 61b017f..1b076a3 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240530api\346\216\247\345\210\266\345\231\250\345\256\236\344\275\223\347\261\273\351\233\206\345\220\210\347\261\273.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240530api\346\216\247\345\210\266\345\231\250\345\256\236\344\275\223\347\261\273\351\233\206\345\220\210\347\261\273.md" @@ -1,11 +1,11 @@ ## api接口层面的控制器 - 完成了第一个阶段的api搭建, 第五阶段Restfull风格的控制器和路由 Controllers目录下的文件 - AuthorsController.cs + using Microsoft.AspNetCore.Mvc; using BookStoreApi.Db; using BookStoreApi.Domain; namespace BookStoreApi.AddControllers; - [ApiController] [Route(("api/[controller]"))] public class AuthorsController : ControllerBase @@ -23,20 +23,22 @@ { return Ok(); } - + [HttpPut("{id}")] public IActionResult Put(int id) { return Ok(id); } - + [HttpDelete("{id}")] public IActionResult Del(int id) { return Ok(id); } } + - BooksController.cs + using Microsoft.AspNetCore.Mvc; namespace BookStoreApi.AddControllers; [ApiController] @@ -64,20 +66,24 @@ return Ok(id); } } + ## Domain 实体类 - 模型设计-图书管理系统 - Authors.cs - - using System.Data; - namespace BookStoreApi.Domain; - public class Authors - { - public Guid Id {get; set;} - public string AuthorName { get; set; } =null!; - public int Gender { get; set; } - public DateTime Birthday { get; set; } - } + + using System.Data; + namespace BookStoreApi.Domain; + public class Authors + { + public Guid Id {get; set;} + public string AuthorName { get; set; } =null!; + public int Gender { get; set; } + public DateTime Birthday { get; set; } + } + - Books.cs - - namespace BookStoreApi.Domain; + + namespace BookStoreApi.Domain; public class Books { public Guid Id { get; set; } @@ -87,11 +93,12 @@ } ## Db内存型数据库 自定义集合类 - bookStoreDb.cs - - using BookStoreApi.Domain; - namespace BookStoreApi.Db; - public class BookStoreDb - { - public static BookStoreDb Instance { get; set; } = new BookStoreDb(); - public ICollection Authors { get; set; } = new List{new Authors()}; - public ICollection Books { get; set; } = new List{new Books()}; - } + + using BookStoreApi.Domain; + namespace BookStoreApi.Db; + public class BookStoreDb + { + public static BookStoreDb Instance { get; set; } = new BookStoreDb(); + public ICollection Authors { get; set; } = new List{new Authors()}; + public ICollection Books { get; set; } = new List{new Books()}; + } diff --git "a/\347\231\275\345\251\211\345\251\267/20240531\345\256\236\347\216\260\346\216\245\345\217\243.md" "b/\347\231\275\345\251\211\345\251\267/20240531\345\256\236\347\216\260\346\216\245\345\217\243.md" index 8892915..46168e2 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240531\345\256\236\347\216\260\346\216\245\345\217\243.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240531\345\256\236\347\216\260\346\216\245\345\217\243.md" @@ -7,40 +7,45 @@ ## Services 实现 - Serivices-AuthorRepository.cs - public class AuthorRepository : IAuthorRepository - { - public ICollection GetAllthors() + + public class AuthorRepository : IAuthorRepository { - //这个实现应该从持久化的数据源中获得:数据库、文件、异构数据、从各种api中获取的数据 - return BookStoreDb.Instance.Authors.ToList(); + public ICollection GetAllthors() + { + //这个实现应该从持久化的数据源中获得:数据库、文件、异构数据、从各种api中获取的数据 + return BookStoreDb.Instance.Authors.ToList(); + } } - } +# Get ## 作者信息 图书绑定作者信息 - Db-BookStoreDb.cs - public class BookStoreDb - { - public BookStoreDb() + + public class BookStoreDb { - Authors.Add(new Authors - { - Id = Guid.NewGuid(), - AuthorName = "张三", - Gender = 1, - Birthday = DateTime.Now - }); - Books.Add(new Books + public BookStoreDb() { - Id = Guid.NewGuid(), - BookName = "张", - Publisher = "出版社", - Price = 199, - AuthorId = Authors.SingleOrDefault(item => item.AuthorName == "张三").Id - }); + Authors.Add(new Authors + { + Id = Guid.NewGuid(), + AuthorName = "张三", + Gender = 1, + Birthday = DateTime.Now + }); + Books.Add(new Books + { + Id = Guid.NewGuid(), + BookName = "张", + Publisher = "出版社", + Price = 199, + AuthorId = Authors.SingleOrDefault(item => item.AuthorName == "张三").Id + }); + } } - } + ## 判断Id查询 有传对应Id 无传输出所有 - Controllers-AuthorsController.cs + public AuthorsController (IAuthorRepository authorRepository) { _authorRepository = authorRepository; @@ -57,23 +62,28 @@ return Ok(item); } } + - Interfaces-IAuthorRespository.cs - public interface IAuthorRepository - { - //通过Id获取指定作者的方法 - //获取所有作者的方法 - //函数三要素:函数名称、参数、返回值 - AuthorsDto? GetAuthorById(Guid id); - ICollectionGetAllthors(); - } + + public interface IAuthorRepository + { + //通过Id获取指定作者的方法 + //获取所有作者的方法 + //函数三要素:函数名称、参数、返回值 + AuthorsDto? GetAuthorById(Guid id); + ICollectionGetAllthors(); + } + - Serivices-AuthorRepository.cs - public class AuthorRepository : IAuthorRepository - { - public AuthorsDto? GetAuthorById(Guid id) + + public class AuthorRepository : IAuthorRepository { - return BookStoreDb.Instance.Authors.SingleOrDefault(item=>item.Id == id); + public AuthorsDto? GetAuthorById(Guid id) + { + return BookStoreDb.Instance.Authors.SingleOrDefault(item=>item.Id == id); + } } - } + ## C#常用集合类 集合接口 1. IEnumerable 2. ICollction diff --git "a/\347\231\275\345\251\211\345\251\267/20240603.md" "b/\347\231\275\345\251\211\345\251\267/20240603\346\237\245\345\242\236\346\224\271Dto.md" similarity index 30% rename from "\347\231\275\345\251\211\345\251\267/20240603.md" rename to "\347\231\275\345\251\211\345\251\267/20240603\346\237\245\345\242\236\346\224\271Dto.md" index c6c6298..7f288bf 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240603.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240603\346\237\245\345\242\236\346\224\271Dto.md" @@ -1,5 +1,11 @@ ## Domain 领域/模型 ## Dto 传输模型 +## Dto 好处 + 数据封装与传输:DTO用于在不同层之间传递数据,特别是在控制器和服务层之间。它能够将请求中的数据进行封装,从而便于传输。 + 解耦:通过使用DTO,可以避免直接暴露数据库模型(这里指Domain中的文件),从而降低各层之间的耦合度。 + 数据验证与格式化:DTO可以添加数据注释和验证属性,确保传入的数据符合预期格式和约束。这样可以在数据到达业务逻辑层之前进行初步验证。 + 安全性:可以避免将敏感的数据库字段直接暴露给客户端。 + 数据转换:以用于将复杂的数据结构转换为适合传输的简单结构,或者将客户端传入的数据转换为适合业务逻辑处理的结构。 ## 引用型数据 - =null! 表示不指向任何一个值 - ="" 指向一个值 值是空字符 @@ -12,14 +18,110 @@ 2. 直接填充其属性 - Sevices-AuthorRepository.cs resultList.Add(new AuthorsDto { Id = item.Id, AuthorName = item.AuthorName, Gender = item.Gender }); - +## Post +- Dto-AuthorCreateDto.cs + + public class AuthorCreateDto + { + public String AuthorName { get; set; }=null!; + public int Gender { get; set;} + public DateTime Birthday{ get; set;} + } + +- Controllers-AuthorsControllers.cs + + [HttpPost] + public IActionResult Post(AuthorCreateDto authorCreateDto) + { + /* + 1. 拿到AuthorCreateDto类型的实例化数据-模型绑定 + 2. 将相关数据保存到数据库 + -转换AuthorCreateDto类型的数据为Authors + -调用数据库上下文,将数据插入 + */ + var result = _authorRepository.Insert(authorCreateDto); + return Ok(result); + } + +- Interfaces-IAuthorRespository.cs + + public interface IAuthorRepository + { + AuthorDto Insert(AuthorCreateDto authorCreateDto); + } + +- Services-AuthorRespository.cs + + public class AuthorRepository : IAuthorRepository + { + //authorCreateDto转换为Domain-Authors.cs + //将传入的dot转为保存到数据库需要的实体类型 + public AuthorDto Insert(AuthorCreateDto authorCreateDto) + { + var author = new Authors + { + Id = Guid.NewGuid(), + AuthorName = authorCreateDto.AuthorName, + Gender = authorCreateDto.Gender, + Birthday = authorCreateDto.Birthday, + }; + } + } + +## Update +- Controllers-Authorller.cs + + [HttpPut("{id}")] + public IActionResult Put(Guid id, AuthorUpdateDto authorUpdateDto) + { + var result = _authorRepository.Update(id,authorUpdateDto); + return Ok(result); + } + +- Dto-AuthorUpdateDto.cs + + public class AuthorUpdateDto + { + //作者更新设置 + public string AuthorName { get; set; } =null!; + public int Gender { get; set;} + public DateTime Birthday { get; set; } + } + +- InterFaces-IAuthorRespository.cs + + public interface IAuthorRepository + { + AuthorDto? Update(Guid authorId,AuthorUpdateDto authorUpdateDto); + } + +- Services-AuthorRespository.cs + + public class AuthorRepository : IAuthorRepository + { + public AuthorDto? Update(Guid authorId, AuthorUpdateDto authorUpdateDto) + { + var author = BookStoreDb.Instance.Authors.FirstOrDefault(item => item.Id == authorId); + if (author == null) + { + return null; + } + // 修改值 + author.AuthorName = authorUpdateDto.AuthorName; + author.Gender = authorUpdateDto.Gender; + author.Birthday = authorUpdateDto.Birthday; + var result = new AuthorDto + { + Id=authorId, + AuthorName = authorUpdateDto.AuthorName, + Gender = authorUpdateDto.Gender, + }; + return result; + } + } + + + \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240604.md" "b/\347\231\275\345\251\211\345\251\267/20240604.md" new file mode 100644 index 0000000..7cfed77 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240604.md" @@ -0,0 +1,64 @@ +## ICollection 集合接口 + +## Delete 作者 +- Interfaces-IAuthorRespository.cs +- Services-AuthorRespository.cs +- Controllers-AuthorsController.cs + +## 图书管理 +- Interfaces.IBookRepository.cs +- Dto +- Sevices-BookRepository.cs +- Controllers-BooksController.cs +## Get +- Dto-BookDto.cs + + public class BookDto + { + public Guid Id { get; set; } + public string BookName { get; set; } = null!; + public Guid AuthorId{ get; set; } + + public dynamic? Author { get; set; } + } + +- Interfaces-IBookRepository.cs + + public interface IBookRepository + { + BookDto? GetBookId(Guid id); + } +## Create +- Dto-BookCreateDto.cs + + namespace BookStore.Api.Dto; + public class BookCreateDto + { + public string BookName { get; set; }=null!; + public string? Publisher { get; set; } + public int Price { get; set; } + public Guid AuthorId{ get; set;} + } + +- Interfaces-IBookRepository.cs + + public interface IBookRepository + { + BookDto Insert(BookCreateDto bookCreateDto); + } +## Update +- Dto-BookUpdateDto.cs + + namespace BookStore.Api.Dto; + public class BookUpdateDto + { + public string BookName { get; set; }=null!; + public string? Publisher { get; set; } + public int Price { get; set; } + } +- Interfaces-IBookRepository.cs + + public interface IBookRepository + { + BookDto? Update(Guid BookId,BookUpdateDto bookUpdateDto); + } \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240605.md" "b/\347\231\275\345\251\211\345\251\267/20240605.md" deleted file mode 100644 index 7003b21..0000000 --- "a/\347\231\275\345\251\211\345\251\267/20240605.md" +++ /dev/null @@ -1,2 +0,0 @@ -## 配置接口 -## 实现 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240607.md" "b/\347\231\275\345\251\211\345\251\267/20240607.md" index 40bb7fd..4f69c80 100644 --- "a/\347\231\275\345\251\211\345\251\267/20240607.md" +++ "b/\347\231\275\345\251\211\345\251\267/20240607.md" @@ -1,10 +1,36 @@ -一、EF Core的两种使用方法 -1.代码优先(推荐) +## 添加EF Cpre - 根据先创建好的实体类来创建数据库和表 - EF Core会将对实体类的修改同步到数据库中,都是对数据库手工修改将会在EF Core同步数据后丢失 - 用于同步代码到数据库的方法是迁移(Migration),就是提供以增量的方式来修改数据库和表结构,使实体类和数据库保持一致 + dotnet add package Microsoft.EntityFrameworkCore -2.数据库优先 +## 添加驱动包 + + dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +## Json文件中添加 + + { + "ConnectionStrings": { + "AuthorDbConnection": "Server=Author;Database=BookStoreDb;User Id=sa;Password=123456;" + } + } + +## 注册数据库上下文到容器 链接字符串 +- Db-AuthorDbContext.cs +- Startup.cs + +## DbSet 公共形式 + +## 数据迁移 +- dotnet add package Microsoft.EntityFrameworkCore.Design +- dotnet ef migrations add Firstii +- ef migrations remove 撤销迁移 +- dotnet ef database update 数据更新 + +## 安装更新 .NET EF +- dotnet tool install -g dotnet-ef 安装 +- dotnet tool update -g dotnet-ef 更新到最新版本 + +## ssql查询 +- select * from __EFMigrationsHistory 迁移生成记录 +- select * from Authors -根据先创建好的数据库生成相应的代码 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240617.md" "b/\347\231\275\345\251\211\345\251\267/20240617.md" new file mode 100644 index 0000000..c80c8a6 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240617.md" @@ -0,0 +1 @@ +在ASP.NET Core应用程序中配置CORS(跨域资源共享)策略 \ No newline at end of file diff --git "a/\347\231\275\345\251\211\345\251\267/20240619.md" "b/\347\231\275\345\251\211\345\251\267/20240619.md" new file mode 100644 index 0000000..226c311 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240619.md" @@ -0,0 +1,19 @@ + +博客表考前梳理 +后端webapi + + 配置入口文件Program.cs + 配置启动类,注册依赖Startup.cs + 创建封装表格Domain/Blog.cs + 创建表格数据实体类Dto:BlogCreateDto、BlogDto、BlogUpdateDto + 在配置文件中编写数据库连接字符串 + + "ConnectionStrings": { + "SqlServer": "server=.;database=BlogDemo;uid=sa;pwd=123456;TrustServerCertificate=true;" + } + + 创建数据库上下文,连接数据库Db/BlogsDbContext + 创建通用仓储接口Interfaces/IRepositoryBase.cs + 实现接口Services/RepositoryBase.cs + 编写控制器 + 生成迁移和同步数据库 diff --git "a/\347\231\275\345\251\211\345\251\267/20240621.md" "b/\347\231\275\345\251\211\345\251\267/20240621.md" new file mode 100644 index 0000000..8137307 --- /dev/null +++ "b/\347\231\275\345\251\211\345\251\267/20240621.md" @@ -0,0 +1 @@ +仓储接口实现 \ No newline at end of file -- Gitee