目录
一、前言
二、上节回顾
编辑
三、新建Dto和添加映射
1.新建dto
2.添加映射规则
四、新建WebApi控制器用EFcore进行增删改查
1.新建Webapi控制器接口
2.新建Webapi控制器实现
3.跑项目测试
五、WebApi控制器调用底层代码
1.webapi控制器(高层代码)
2.底层代码接口
3.底层代码实现
六、常见问题
1.注释不显示
步骤一:新建xml输出
步骤二:代码引用xml文件
2.底层代码调用失败
一、前言
更详细介绍:官网的框架列表
二、上节回顾
上节我们写了实体、枚举、创建了数据表,添加了种子数据,最后还卸载了迁移项目
三、新建Dto和添加映射
1.新建dto
using System;
using Volo.Abp.Application.Dtos;namespace Acme.BookStore.Books
{public class BookDto : AuditedEntityDto<Guid>{public string Name { get; set; }public BookType Type { get; set; }public DateTime PublishDate { get; set; }public float Price { get; set; }}
}
2.添加映射规则
CreateMap<Book, BookDto>().ReverseMap();//添加Model-Dto映射规则
四、新建WebApi控制器用EFcore进行增删改查
1.新建Webapi控制器接口
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;namespace Acme.BookStore.Books
{public interface IBookAppService : IApplicationService{Task<List<BookDto>> GetListAsync();Task<bool> DeleteAsync(Guid id);Task<bool> CreateAsync(BookDto input);Task<bool> UpdateAsync(BookDto input);}}
2.新建Webapi控制器实现
若缺少mvc包,需要安装!
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;namespace Acme.BookStore.Books
{[Route("api/books")]public class BookAppService : ApplicationService, IBookAppService{private readonly IRepository<Book, Guid> _bookRepository;public BookAppService(IRepository<Book, Guid> bookRepository){_bookRepository = bookRepository;}/// <summary>/// 创建书本/// </summary>/// <param name="input"></param>/// <returns></returns>[Route("createbook")][HttpPost]public async Task<bool> CreateAsync(BookDto input)//可上拉接口,下同{var book = ObjectMapper.Map<BookDto, Book>(input);await _bookRepository.InsertAsync(book);return true; // 创建成功}/// <summary>/// 获取书本/// </summary>/// <returns></returns>[Route("getbook")][HttpGet]public async Task<List<BookDto>> GetListAsync(){var books = await _bookRepository.GetListAsync();return ObjectMapper.Map<List<Book>, List<BookDto>>(books);}/// <summary>/// 更新书本/// </summary>/// <param name="input"></param>/// <returns></returns>[Route("updatebook")][HttpPut]public async Task<bool> UpdateAsync(BookDto input){var book = await _bookRepository.GetAsync(input.Id);ObjectMapper.Map(input, book);await _bookRepository.UpdateAsync(book);return true;}/// <summary>/// 删除书本/// </summary>/// <param name="id"></param>/// <returns></returns>[Route("delbook")][HttpDelete]public async Task<bool> DeleteAsync(Guid id){var book = await _bookRepository.FirstOrDefaultAsync(x=>x.Id==id);if (book != null){await _bookRepository.DeleteAsync(id);return true; // 删除成功}return false; // 找不到该书籍}}}
3.跑项目测试
设置它为启动项目
开启redis,检查连接字符串是否正确,然后跑项目,出现swagger界面后可以调试接口:
五、WebApi控制器调用底层代码
1.webapi控制器(高层代码)
using Acme.BookStore.BooksService;
using Acme.BookStore.IBooksSevice;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;namespace Acme.BookStore.Books
{[Route("api/books")]public class BookAppService : ApplicationService, IBookAppService{private readonly IBookService _bookService;public BookAppService(BookService bookService){_bookRepository = bookRepository;_bookService = bookService;}/// <summary>/// 通过底层代码获取书本/// </summary>/// <returns></returns>[Route("getbookbybll")][HttpGet]public async Task<List<BookDto>> GetListByBLLAsync(){var list = await _bookService.GetBooks();return list;}}}
2.底层代码接口
namespace Acme.BookStore.IBooksSevice
{public interface IBookService{Task<List<BookDto>> GetBooks();}
}
3.底层代码实现
using Acme.BookStore.Books;
using Acme.BookStore.IBooksSevice;
using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Application.Services;
using static System.Reflection.Metadata.BlobBuilder;
using Volo.Abp.DependencyInjection;namespace Acme.BookStore.BooksService
{public class BookService : ApplicationService, IBookService//必须继承ApplicationService,因为其继承了ITransientDependency,保证了服务被正确注入到依赖容器,高层代码才能调用这个底层代码,继承了它还可以调用ObjectMapper方法{private readonly IRepository<Book, Guid> _bookRepository;public BookService(IRepository<Book, Guid> bookRepository){_bookRepository = bookRepository;}public async Task<List<BookDto>> GetBooks() //上拉到接口{var entitylist = await _bookRepository.ToListAsync();var dtolist = ObjectMapper.Map<List<Book>, List<BookDto>>(entitylist);return dtolist;}}
}
六、常见问题
1.注释不显示
步骤一:新建xml输出
方法一:哪个类库不显示,改对应类库的工程文件
<GenerateDocumentationFile>true</GenerateDocumentationFile><DocumentationFile>bin\Debug\net6.0\Acme.BookStore.HttpApi.Host.xml</DocumentationFile>
方法二: 对应类库的右键属性设置
输入框填写的内容应该和工程文件种的一致:
bin\Debug\net6.0\Acme.BookStore.HttpApi.Host.xml
步骤二:代码引用xml文件
API模块种补上代码,当然也可以直接字符串写死 xmlFile = " Acme.BookStore.HttpApi.Host.xml " ,怎么命名都好,总之,xmlFile这个文件名和上面步骤一设置的文件名一致。
private static void ConfigureSwaggerServices(ServiceConfigurationContext context, IConfiguration configuration)
{var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";//"Acme.BookStore.HttpApi.Host.xml"var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);context.Services.AddAbpSwaggerGenWithOAuth(configuration["AuthServer:Authority"],new Dictionary<string, string>{{"BookStore", "BookStore API"}},options =>{options.SwaggerDoc("v1", new OpenApiInfo { Title = "BookStore API", Version = "v1" });options.DocInclusionPredicate((docName, description) => true);options.CustomSchemaIds(type => type.FullName);options.IncludeXmlComments(xmlPath); // 添加这一行});
}
修改后的效果:
注意:工程文件只需要配置对应的应用层即可!不要再配置Host,否则会覆盖你应用层生成的xml。
2.底层代码调用失败
报错信息:Castle.Proxies.BookAppServiceProxy
原因:底层代码接口未注入到依赖容器,服务找不到这个底层代码接口
解决方案:底层代码继承ApplicationService或实现ITransientDependency
(未完待续...)