飞扬的Blog
主页
登录
asp.net core Abp 框架笔记
"date"
:
"2020-08-13 15:12:32"
"classfiy"
:
"asp.net core"
"author"
:
"飞扬"
"viewTimes"
:
327
返回
[TOC] ## 文档 >[Abp官网](https://aspnetboilerplate.com/) [项目创建地址](https://aspnetboilerplate.com/Templates) [官方中文文档](https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/tree/master/Markdown) ## 创建一个项目: >按照图示选择,填写好信息和验证码点击Create my project!按钮即可。 ![](https://sqcdn.hasagei.com/fysmartblog/imgs/B99D9BF9FC154D8FA1241F891D0A5140/4b50387e-b886-4097-91d1-0fdc97133900.png!fycprs) >下载完毕后项目架构如下: ![](https://sqcdn.hasagei.com/fysmartblog/imgs/B99D9BF9FC154D8FA1241F891D0A5140/32b148f0-86b3-4d4d-9bd2-12c7ffad37b2.png!fycprs) ## 下载完毕后: >1. 恢复解决方案的Nuget包 2. 分别配置在 `.Web.Host` 和 `.Web.Mvc` 两个项目中 `appsettings.json` 文件内的数据库链接字符串 3. 设置 `.Web.Mvc` 项目为启动项目 4. VS程序包控制台内设置 `.EntityFrameworkCore` 项目为默认项目执行迁移: `Update-Database` 5. 到目前位置可以运行项目了。 6. `.Web.Host` 项目运行后显示的是Swagger界面,`.Web.Mvc` 显示的是后台页面(maybe) ## 各层的作用 >[查看官方文档](https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/1.2ABP%E6%80%BB%E4%BD%93%E4%BB%8B%E7%BB%8D-%E5%A4%9A%E5%B1%82%E6%9E%B6%E6%9E%84%E4%BD%93%E7%B3%BB.md) ## 管理Blog的测试demo的项目代码 ### 在领域层(.Core)创建Blog的实体 >1. 创建Blog文件夹 2. 创建Blog实体类,Blog.cs代码如下: ```csharp using Abp.Domain.Entities; using Abp.Domain.Entities.Auditing; using Abp.Timing; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text; namespace Learn.Blog { [Table("Blogs")] public class Blog: Entity, IHasCreationTime { public const int MaxTitleLength = 256; public const int MaxDescriptionLength = 64 * 1024; //64KB [Required] [MaxLength(MaxTitleLength)] public string Title { get; set; } [MaxLength(MaxDescriptionLength)] public string Description { get; set; } public DateTime CreationTime { get; set; } public Blog() { CreationTime = Clock.Now; } public Blog(string title, string description = null) : this() { Title = title; Description = description; } } } ``` >实体创建完毕后在基础设施层(.EntityFrameworkCore)创建Blogs表 `\EntityFrameworkCore\LearnDbContext.cs` 内新增: ```CSharp public DbSet<Learn.Blog.Blog> Blogs { get; set; } ``` >创建迁移: 1. add-migration add_blog_table 2. update-database ### 在应用层(.Application)创建Blog仓储(Repository)并实现 >1. [IApplicationService文档(ABP应用层 - 应用服务)](https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/4.1ABP%E5%BA%94%E7%94%A8%E5%B1%82-%E5%BA%94%E7%94%A8%E6%9C%8D%E5%8A%A1.md) 2. [DTO对象和自动映射(AutoMap)](https://github.com/ABPFrameWorkGroup/AbpDocument2Chinese/blob/master/Markdown/Abp/4.2ABP%E5%BA%94%E7%94%A8%E5%B1%82-%E6%95%B0%E6%8D%AE%E4%BC%A0%E8%BE%93%E5%AF%B9%E8%B1%A1.md) #### 目录结构如下: ![](https://sqcdn.hasagei.com/fysmartblog/imgs/B99D9BF9FC154D8FA1241F891D0A5140/f17c6439-c212-4723-9c1f-7443da900671.png!fycprs) #### 代码 ```CSharp //IBlogAppService.cs using Abp.Application.Services; using Abp.Application.Services.Dto; using Learn.Blog.Dtos; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Learn.Blog { public interface IBlogAppService : IApplicationService { [HttpGet] Task<ListResultDto<BlogListDto>> GetAll(); [HttpPost] Task Create(CreateBlogInput input); } } ``` ```CSharp //BlogAppService.cs using Abp.Application.Services.Dto; using Abp.Domain.Repositories; using Learn.Blog.Dtos; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace Learn.Blog { public class BlogAppService : LearnAppServiceBase, IBlogAppService { private readonly IRepository<Blog> _blogRepository; public BlogAppService(IRepository<Blog> blogRepository) { _blogRepository = blogRepository; } public async Task Create(CreateBlogInput input) { var blog = ObjectMapper.Map<Blog>(input); await _blogRepository.InsertAsync(blog); } public async Task<ListResultDto<BlogListDto>> GetAll() { var blogs = await _blogRepository.GetAll().ToListAsync(); return new ListResultDto<BlogListDto>(ObjectMapper.Map<List<BlogListDto>>(blogs)); } } } ``` ```CSharp //BlogListDto.cs using Abp.Application.Services.Dto; using Abp.AutoMapper; using Abp.Domain.Entities.Auditing; using System; using System.Collections.Generic; using System.Text; namespace Learn.Blog.Dtos { [AutoMapFrom(typeof(Blog))] public class BlogListDto : EntityDto, IHasCreationTime { public string Title { get; set; } public string Description { get; set; } public DateTime CreationTime { get; set; } } } ``` ```CSharp //CreateBlogInput.cs using Abp.AutoMapper; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text; namespace Learn.Blog.Dtos { [AutoMapTo(typeof(Blog))] public class CreateBlogInput { [Required] [MaxLength(Blog.MaxTitleLength)] public string Title { get; set; } [MaxLength(Blog.MaxDescriptionLength)] public string Description { get; set; } } } ``` ## 动态Web Api >作用:无需创建Controller层直接将`IBlogAppService`的方法暴漏出去 ### 步骤 1. `.Application` 安装 `Abp.AspNetCore` 包 2. 在 LearnApplicationModule.cs 的 Initialize 方法内新增: ```CSharp Configuration.Modules.AbpAspNetCore().CreateControllersForAppServices(typeof(IBlogAppService).Assembly, moduleName: "app", useConventionalHttpVerbs: true); ``` #### 此时运行 `.Web.Host` 项目在Swagger内就可看见IBlogAppService暴露出的方法。 ![](https://sqcdn.hasagei.com/fysmartblog/imgs/B99D9BF9FC154D8FA1241F891D0A5140/603c2fdd-eebf-4ad3-9b3d-6649abbd47d6.png!fycprs) >`/api/services/` 为默认前缀,`app` 为 `moduleName` 设置的