簡體   English   中英

ABP 上的應用服務 URL 映射

[英]Application service URL mapping on ABP

我是 ABP 的新手,我成功地完成了這個官方教程

問題是,然后我添加了另一個 class (Planta)並再次按照教程進行操作(不刪除 The Book 類),但即使我可以創建表並在其上提供數據(已驗證),應用程序也無法加載表,當我檢查 swagger 時,我發現了這個......

在此處輸入圖像描述

我期待它是 Planta 而不是 BookAppServicePlanta,但我找不到我在哪里搞砸了。


我試圖解決的事情

  • 我讀過的關於 ABP 的內容最多。
  • 我已經將每個 Planta 文件與 Book 對應文件進行了對比。
  • 我已經多次刪除數據庫。

這是我所做的(詳情如下):

  1. 我在Acme.BookStore.Domain/Planta/Planta.cs上創建了 class 植物:
  2. 將實體添加到Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs
  3. 將實體映射到Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs上的表
  4. 刪除了數據庫,並刪除了以前的遷移
  5. 創建了一個數據播種Acme.BookStore.Domain/BookStoreDataSeederContributor_Plant.cs
  6. 添加了一個新的遷移,並運行Acme.BookStore.DbMigrator
  7. 創建Acme.BookStore.Application.Contracts/PlantDto.cs
  8. 將其添加到Acme.BookStore.Application/BookStoreApplicationAutoMapperProfile.cs
  9. 創建Acme.BookStore.Application.Contracts/CreateUpdatePlantDto.cs (並將其也添加到自動映射器中,如圖 8 所示))
  10. 創建接口Acme.BookStore.Application.Contracts/IBookAppServicePlanta.cs
  11. Acme.BookStore.Application/BookAppServicePlanta.cs上實現它
  12. 運行應用程序

額外信息:我為 Planta 及其 forms(教程第 2 部分和第 3 部分)創建了頁面,但即使我仔細檢查了這些文件,我也不相信問題出在這些文件上,因為 swagger 問題。


  1. 我在Acme.BookStore.Domain/Planta/Planta.cs上創建了 class 植物:

     using System; using Volo.Abp.Domain.Entities.Auditing; namespace Acme.BookStore.Plantas { public class Planta: AuditedAggregateRoot<Guid> { public string Nombre { get; set; } public string Descripcion { get; set; } public string Dirección { get; set; } public string Lat { get; set; } public string Long { get; set; } public string Extra1 { get; set; } public string Extra2 { get; set; } public string Extra3 { get; set; } } }
  2. 將實體添加到Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContext.cs

     using Microsoft.EntityFrameworkCore; using Acme.BookStore.Users; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.Modeling; using Volo.Abp.Identity; using Volo.Abp.Users.EntityFrameworkCore; using Acme.BookStore.Books; using Acme.BookStore.Plantas; namespace Acme.BookStore.EntityFrameworkCore { /* This is your actual DbContext used on runtime. * It includes only your entities. * It does not include entities of the used modules, because each module has already * its own DbContext class. If you want to share some database tables with the used modules, * just create a structure like done for AppUser. * * Don't use this DbContext for database migrations since it does not contain tables of the * used modules (as explained above). See BookStoreMigrationsDbContext for migrations. */ [ConnectionStringName("Default")] public class BookStoreDbContext: AbpDbContext<BookStoreDbContext> { public DbSet<AppUser> Users { get; set; } public DbSet<Book> Books { get; set; } public DbSet<Planta> Plantas { get; set; } /* Add DbSet properties for your Aggregate Roots / Entities here. * Also map them inside BookStoreDbContextModelCreatingExtensions.ConfigureBookStore */ public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options): base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); /* Configure the shared tables (with included modules) here */ builder.Entity<AppUser>(b => { b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser b.ConfigureByConvention(); b.ConfigureAbpUser(); /* Configure mappings for your additional properties * Also see the BookStoreEfCoreEntityExtensionMappings class */ }); /* Configure your own tables/entities inside the ConfigureBookStore method */ builder.ConfigureBookStore(); } } }
  3. 將實體映射到Acme.BookStore.EntityFrameworkCore/EntityFrameworkCore/BookStoreDbContextModelCreatingExtensions.cs上的表

     using Acme.BookStore.Books; using Acme.BookStore.Plantas; using Microsoft.EntityFrameworkCore; using Volo.Abp; using Volo.Abp.EntityFrameworkCore.Modeling; namespace Acme.BookStore.EntityFrameworkCore { public static class BookStoreDbContextModelCreatingExtensions { public static void ConfigureBookStore(this ModelBuilder builder) { Check.NotNull(builder, nameof(builder)); /* Configure your own tables/entities inside here */ builder.Entity<Book>(b => { b.ToTable(BookStoreConsts.DbTablePrefix + "Books", BookStoreConsts.DbSchema); b.ConfigureByConvention(); //auto configure for the base class props b.Property(x => x.Name).IsRequired().HasMaxLength(128); }); builder.Entity<Planta>(p => { p.ToTable(BookStoreConsts.DbTablePrefix + "Plantas", BookStoreConsts.DbSchema); p.ConfigureByConvention(); //auto configure for the base class props p.Property(y => y.Nombre).IsRequired().HasMaxLength(128); }); } } }
  4. 刪除了數據庫,並刪除了以前的遷移

  5. 創建了一個數據播種Acme.BookStore.Domain/BookStoreDataSeederContributor_Plant.cs

     using System; using System.Threading.Tasks; using Acme.BookStore.Plantas; using Volo.Abp.Data; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; namespace Acme.BookStore { public class BookStoreDataSeederContributor_Plant: IDataSeedContributor, ITransientDependency { private readonly IRepository<Planta, Guid> _plantaRepository; public BookStoreDataSeederContributor_Plant(IRepository<Planta, Guid> plantaRepository) { _plantaRepository = plantaRepository; } public async Task SeedAsync(DataSeedContext context) { if (await _plantaRepository.GetCountAsync() > 0) { return; } await _plantaRepository.InsertAsync( new Planta { Nombre = "Armijo Guajardo", Descripcion = "excel god", Dirección = "las lilas 123", Lat = "564.765.98", Long = "100.102.04", Extra1 = "bla", Extra2 = "bla bla", Extra3 = "bla bla bla" }, autoSave: true ); } } }
  6. 添加了一個新的遷移,並運行Acme.BookStore.DbMigrator

  7. 創建Acme.BookStore.Application.Contracts/PlantDto.cs

     using System; using Volo.Abp.Application.Dtos; namespace Acme.BookStore.Plantas { public class PlantDto: AuditedEntityDto<Guid> { public string Nombre { get; set; } public string Descripcion { get; set; } public string Dirección { get; set; } public string Lat { get; set; } public string Long { get; set; } public string Extra1 { get; set; } public string Extra2 { get; set; } public string Extra3 { get; set; } } }
  8. 將其添加到Acme.BookStore.Application/BookStoreApplicationAutoMapperProfile.cs

     using Acme.BookStore.Books; using Acme.BookStore.Plantas; using AutoMapper; namespace Acme.BookStore { public class BookStoreApplicationAutoMapperProfile: Profile { public BookStoreApplicationAutoMapperProfile() { CreateMap<Book, BookDto>(); CreateMap<CreateUpdateBookDto, Book>(); CreateMap<Planta, PlantDto>(); CreateMap<CreateUpdatePlantDto, Planta>(); } } }
  9. 創建Acme.BookStore.Application.Contracts/CreateUpdatePlantDto.cs (並將其也添加到自動映射器中,如圖 8 所示))

     using System; using System.ComponentModel.DataAnnotations; namespace Acme.BookStore.Plantas { public class CreateUpdatePlantDto { [Required] [StringLength(128)] public string Nombre { get; set; } [Required] [StringLength(128)] public string Descripcion { get; set; } [Required] [StringLength(128)] public string Dirección { get; set; } [Required] [StringLength(128)] public string Lat { get; set; } [Required] [StringLength(128)] public string Long { get; set; } [Required] [StringLength(128)] public string Extra1 { get; set; } [Required] [StringLength(128)] public string Extra2 { get; set; } [Required] [StringLength(128)] public string Extra3 { get; set; } } }
  10. 創建接口Acme.BookStore.Application.Contracts/IBookAppServicePlanta.cs

     using System; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; namespace Acme.BookStore.Plantas { public interface IBookAppServicePlanta: ICrudAppService< //Defines CRUD methods PlantDto, //Used to show books Guid, //Primary key of the book entity PagedAndSortedResultRequestDto, //Used for paging/sorting CreateUpdatePlantDto> //Used to create/update a book { } }
  11. Acme.BookStore.Application/BookAppServicePlanta.cs上實現它

    using System; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Acme.BookStore.Plantas { public class BookAppServicePlanta: CrudAppService< Planta, //The Book entity PlantDto, //Used to show books Guid, //Primary key of the book entity PagedAndSortedResultRequestDto, //Used for paging/sorting CreateUpdatePlantDto>, //Used to create/update a book IBookAppServicePlanta //implement the IBookAppService { public BookAppServicePlanta(IRepository<Planta, Guid> repository): base(repository) { } } }
  12. 運行應用程序


[編輯]

Acme.BookStore.Web/BookStoreWebAutoMapperProfile.cs看起來像這樣

    using Acme.BookStore.Books;
    using Acme.BookStore.Plantas;
    using AutoMapper;

    namespace Acme.BookStore.Web
    {
        public class BookStoreWebAutoMapperProfile : Profile
        {
            public BookStoreWebAutoMapperProfile()
            {
                CreateMap<BookDto, CreateUpdateBookDto>();
                CreateMap<PlantDto, CreateUpdatePlantDto>();
            }
        }
    }

[編輯] 我創建了一個測試文件Acme.BookStore.Application.Tests/BookAppServicePlanta_test.cs ,他們都成功了。

    using System;
    using System.Linq;
    using System.Threading.Tasks;
    using Shouldly;
    using Volo.Abp.Application.Dtos;
    using Volo.Abp.Validation;
    using Xunit;

    namespace Acme.BookStore.Plantas
    {
        public class BookAppService_Tests : BookStoreApplicationTestBase
        {
            private readonly IBookAppServicePlanta _plantaAppService;

            public BookAppService_Tests()
            {
                _plantaAppService = GetRequiredService<IBookAppServicePlanta>();
            }

            [Fact]
            public async Task Should_Get_List_Of_Books()
            {
                //Act
                var result = await _plantaAppService.GetListAsync(
                    new PagedAndSortedResultRequestDto()
                );

                //Assert
                result.TotalCount.ShouldBeGreaterThan(0);
                result.Items.ShouldContain(b => b.Nombre == "Armijo Guajardo");
            }

            [Fact]
            public async Task Should_Create_A_Valid_Planta()
            {
                //Act
                var result = await _plantaAppService.CreateAsync(
                    new CreateUpdatePlantDto
                    {
                        Nombre = "Pedro Cano",
                        Descripcion = "Cirujano",
                        Dirección = "Pedro de Valdivia",
                        Lat = "123213213",
                        Long = "456456456",
                        Extra1 = "emmmm",
                        Extra2 = "no se",
                        Extra3 = "que poner"
                    }
                );

                //Assert
                result.Id.ShouldNotBe(Guid.Empty);
                result.Nombre.ShouldBe("Pedro Cano");
            }
            [Fact]
            public async Task Should_Not_Create_A_Planta_Without_Name()
            {
                var exception = await Assert.ThrowsAsync<AbpValidationException>(async () =>
                {
                    await _plantaAppService.CreateAsync(
                        new CreateUpdatePlantDto
                        {
                            Descripcion = "Cirujano",
                            Dirección = "Pedro de Valdivia",
                            Lat = "123213213",
                            Long = "456456456",
                            Extra1 = "emmmm",
                            Extra2 = "no se",
                            Extra3 = "que poner"
                        }
                    );
                });
                exception.ValidationErrors
                    .ShouldContain(err => err.MemberNames.Any(mem => mem == "Nombre"));
                }
        }
    }

我不熟悉 ABP,但從快速查看文檔來看,您似乎沒有遵循命名約定。

應用程序服務應遵循以下命名約定: Entity AppService

但是您似乎復制/粘貼了以前的 class BookAppService並且剛剛將Planta添加到末尾。 它應該是PlantaAppService

using System;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace Acme.BookStore.Plantas
{
    public interface IPlantaAppService :
        ICrudAppService< //Defines CRUD methods
            PlantDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            CreateUpdatePlantDto> //Used to create/update a book
    {

    }
}
using System;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;


namespace Acme.BookStore.Plantas
{
    public class PlantaAppService:
        CrudAppService<
            Planta, //The Book entity
            PlantDto, //Used to show books
            Guid, //Primary key of the book entity
            PagedAndSortedResultRequestDto, //Used for paging/sorting
            CreateUpdatePlantDto>, //Used to create/update a book
        IPlantaAppService //implement the IPlantaAppService
    {
        public BookAppServicePlanta(IRepository<Planta, Guid> repository)
            : base(repository)
        {

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM