本文示例源代碼地址https://github.com/lcyhjx/abp-traininggit
上一篇咱們已經對ABP是什麼,能作什麼、有了一個印象。那麼接下來咱們將動手使用ABP框架快速開發一個API,你將會發現使用ABP框架有多麼便利,會實實在在感覺到它的魅力。github
咱們使用ABP模板來建立應用程序,訪問http://www.aspnetboilerplate.com/Templates,你將會看到以下頁面
數據庫
點擊「建立項目」, 接着咱們就會從ABP模板網站上得到一個項目源碼的壓縮包AbpTraining.zip. 解壓縮AbpTraining.zip就會得到初始項目的源代碼。編程
"ConnectionStrings": { "Default": "Server=localhost; Database=AbpTrainingDb; Trusted_Connection=True;" }
功能: 根據商品名查詢商品信息json
AbpTraining.Core\Products\Product.csc#
using Abp.Domain.Entities.Auditing; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace AbpTraining.Products { //能夠顯示的指定表名,不指定默認是實體名+s [Table("Product")] public class Product : FullAuditedEntity<long> { [Required] [StringLength(128)] public string Name { get; set; } public decimal Price { get; set; } } }
AbpTraining.Core\Products\ProductDomainService.csapi
using Abp.Domain.Repositories; using Abp.Domain.Services; using System.Threading.Tasks; using System.Linq; using Abp.UI; using Microsoft.EntityFrameworkCore; namespace AbpTraining.Products { public class ProductDomainService : DomainService { private readonly IRepository<Product, long> _productRepository; public ProductDomainService(IRepository<Product, long> productRepository) { _productRepository = productRepository; } public async Task<Product> GetProductByName(string name) { var query = from p in _productRepository.GetAll() where p.Name == name select p; var product = await query.FirstOrDefaultAsync(); if (product == null) { throw new UserFriendlyException($"商品({name})不存在"); } if (product.Price < 0) { throw new UserFriendlyException($"商品({name})的價格小於0,請檢查"); } return product; } } }
AbpTraining.Application\Products\Dto\ProductDtoapp
using Abp.AutoMapper; namespace AbpTraining.Products.Dto { [AutoMapFrom(typeof(Product))] public class ProductDto { public string Name { get; set; } public decimal Price { get; set; } } }
AbpTraining.Application\Products\Dto\GetProductByNameInput框架
using System.ComponentModel.DataAnnotations; namespace AbpTraining.Products.Dto { public class GetProductByNameInput { [Required] public string Name { get; set; } } }
Mysoft.RDC.Application\Products\Dto\GetProductByNameOutputdom
namespace AbpTraining.Products.Dto { public class GetProductByNameOutput : ProductDto { } }
AbpTraining.Application\Products\IProductAppService.cs
using Abp.Application.Services; using AbpTraining.Products.Dto; using System.Threading.Tasks; namespace AbpTraining.Products { public interface IProductAppService : IApplicationService { Task<GetProductByNameOutput> GetProductByName(GetProductByNameInput input); } }
AbpTraining.Application\Products\ProductAppService.cs
using System.Threading.Tasks; namespace AbpTraining.Products.Dto { public class ProductAppService : AbpTrainingAppServiceBase, IProductAppService { private readonly ProductDomainService _productDomainService; public ProductAppService(ProductDomainService productDomainService) { _productDomainService = productDomainService; } public async Task<GetProductByNameOutput> GetProductByName(GetProductByNameInput input) { //1.將input dto轉換爲domain obj //2.調用doamin service var item = await _productDomainService.GetProductByName(input.Name); //call other doamin serivce //3.將domain obj轉換爲output dto var output = ObjectMapper.Map<GetProductByNameOutput>(item); return output; } } }
在AbpTraining.EntityFrameworkCore\EntityFrameworkCore\AbpTrainingDbContext.cs 中添加以下代碼片斷
public DbSet<Product> Products { get; set; }
在包管理器控制檯中,執行以下命令,生成遷移腳本文件
Add-Migration AddProduct -Verbos
在包管理器控制檯中,執行以下命令,將新的實體同步數據庫
Update-Database -Verbos
設置AbpTraining.Web.Host爲啓動項目,直接在Visual Studio中運行, 在Swagger的API列表中找到/api/services/app/Product/GetProductByName 便可以開始測試。
到此,咱們的第一個API就成功的完成了.