Building with Nano ASP.NET Boilerplate: Creating a New CRUD Service
入门指南
In this walkthrough, we’ll create a new web application using the Nano CLI tool.
We will create a new 供应商 entity, with an API 控制器 和 服务. 该 供应商 entity will form a one-to-many relationship with the 产品 实体。.
The Nano ASP.NET boilerplate is distributed as a NuGet package. For installation instructions, follow this guide. Once installed, you can use the project templates from the Developer PowerShell terminal.
Open a new terminal window and navigate to a folder to create the new project. In the terminal, use the dotnet nano command to create a new project:
dotnet new nano -n MyApp
You can replace the name attribute (-n) with the name of your project. By passing options for multi-tenancy (-m) 或 user interface (-ui) you can create projects with varying specifications. To learn more, check out the using the CLI tool guide.
Create a Supplier entity
We will start by creating a new entity called 供应商. This entity will be part of a 一对多 relationship with the 产品 实体。.
The code is divided into layers, with the entities being part of the 域名 layer.
Open the 域名 layer and in the /Catalog folder, create a new entity class called 供应商. 供应商 will have a 姓名 field and navigation property for 产品. The navigation property will establish one side of the one-to-many relationship for Entity Framework. Inherit from the abstract class 可审计实体.
public 类 供应商 : 可审计实体
{
public 字符串 姓名 { get; set; }
public ICollection<产品> 产品 { get; set; }
}Next we need to modify the 产品 entity to complete the one-to-many relationship for Entity Framework. Add a 供应商ID field and 供应商 navigation property.
public 类 产品 : 可审计实体
{
public 字符串 姓名 { get; set; }
public 字符串 描述 { get; set; }
public 供应商 供应商 { get; set; }
public Guid SupplierId { get; set; }
}All entities should directly or indirectly inherit from abstract class BaseEntity, found in /Entities/Common.
可审计实体 is an abstract class that inherits from TenantBaseEntity (or directly from BaseEntity in single-tenant deployments). 可审计实体 adds the CreatedBy, CreatedOn, LastModifiedBy, LastModifiedOn fields. By implementing the IAuditableEntity interface, the values of these fields are handled automatically whenever save changes occurs. Business entities usually inherit from 可审计实体.
TenantBaseEntity inherits from the BaseEntity class and adds the 租户ID field. By implementing the IMustHaveTenant interface, query filters in the ApplicationDbContext will isolate tenant data.
打开 ApplicationDbContext 在 Infrastructure/Persistence/Contexts/ and create the DbSet 用于 供应商
public DbSet<供应商> 供应商 { 获取; set; }Create a Migration
Next we need to create a migration to apply these schema changes to the database.
Open the 包管理器控制台 and set the default project to infrastructure. Use the following command to create a new migration:
add-migration -Context ApplicationDbContext -o Persistence/Migrations/AppDb App-SupplierEntity
If you deployed the project with single database multi-tenancy (-m singledb) or as single tenant (-m singletenant), then use this command:
add-migration -Context ApplicationDbContext -o Persistence/Migrations SupplierEntity
Run the application to apply the migrations or use the 更新数据库 command.
在 ApplicationDbContext you can find a quick reference for all the 迁移 commands in the code comments.
Create the Supplier Service
Next we’ll use the Nano CLI to create a service for the new entity.
在 Developer PowerShell, change directory to MyApp.Application\Services and then run the following command:
dotnet new nano-service -s Supplier -p Suppliers -ap MyApp
If building with Razor pages, use this command:
dotnet new nano-service -s Supplier -p Suppliers -ap MyApp -ui razor
All the code for a new service feature will be created within the SupplierService folder:

Passing the -ui razor简体中文(大陆) option will scaffold slightly different code for the pagination method, otherwise it’s the same
The new service contains all the methods for basic CRUD operations. Here is the scaffolded ISupplierService code for reference:
public 接口 ISupplierService : ITransientService
{
任务<回复<IEnumerable<供应商DTO>>> GetSuppliersAsync(字符串 keyword = "");
任务<分页响应<供应商DTO>> GetSuppliersPaginatedAsync(SupplierTableFilter filter);
任务<回复<供应商DTO>> GetSupplierAsync(Guid id);
任务<回复<指南>> CreateSupplierAsync(CreateSupplierRequest 请求);
任务<回复<指南>> UpdateSupplierAsync(UpdateSupplierRequest request, Guid id);
任务<回复<指南>> DeleteSupplierAsync(Guid id);
}This service will register as a transient service since it implements the ITransientService interface. To register as scoped, change to IScopedService. You can read more about services in the application services guide.
Customize the Supplier DTOs
DTOs 用于 supplier service can be found within Application/Services/SupplierService/DTOs. All DTOs implement the IDto marker interface.
Since the dotnet CLI tool is not aware of entities and their properties, a scaffolded DTO from dotnet new nano-service will only contain an Id 和 姓名 property. Customize your entities as you need to.
In this case, the 供应商DTO should also contain a list of 产品. Add a new list of ProductDTO List to the 供应商DTO:
public 类 供应商DTO : IDto
{
public Guid Id { get; set; }
public 字符串 姓名 { get; set; }
public IList<ProductDTO> 产品 { get; set; }
}Creating a new service with the dotnet new nano-service command also creates DTOs for 创建 和 更新 requests. The generated DTO contains a 姓名 field with validation and should be modified per your needs in regards to the properties in your entity.
public 类 CreateSupplierRequest : IDto
{
public 字符串 姓名 { get; set; }
}
public 类 CreateSupplierValidator : AbstractValidator<CreateSupplierRequest>
{
public CreateSupplierValidator()
{
RuleFor(x => x.姓名).NotEmpty();
}
}Update the Product DTOs
Because we now have a 一对多 relationship between 供应商 和 产品, creating and updating 产品 requires a supplier ID. Navigate to Application/Services/ProductService/DTOs and add a 供应商ID property for the 更新 和 创建 product requests.
public 类 CreateProductRequest : IDto
{
public 字符串 姓名 { get; set; }
public 字符串 描述 { get; set; }
public Guid SupplierId { get; set; }
}
public 类 CreateProductValidator : AbstractValidator<CreateProductRequest>
{
public CreateProductValidator()
{
_ = RuleFor(x => x.姓名).NotEmpty();
_ = RuleFor(x => x.描述).NotEmpty();
_ = RuleFor(x => x.供应商ID).NotEmpty();
}
}在 ProductDTO, add a GUID 供应商ID and a 供应商名称 string property. In the next step, we will configure Automapper to populate the 供应商名称 whenever mapping occurs.
public 类 ProductDTO : IDto
{
public Guid Id { get; set; }
public 字符串? 姓名 { get; set; }
public 字符串? 描述 { get; set; }
public DateTime CreatedOn { get; set; }
public 字符串 供应商名称 { get; set; }
public Guid SupplierId { get; set; }
}Configure Automapping
Next we need to set up mapping profiles for the new DTOs
在 基础设施/地图绘制器/映射配置文件 add the following configurations for supplier:
// supplier mappings...
_ = CreateMap<供应商, 供应商DTO>();
_ = CreateMap<CreateSupplierRequest, 供应商>();
_ = CreateMap<更新供应商请求, 供应商>();This will ensure that Automapper knows how to map a 供应商 entity to a 供应商DTO 以及 创建 / 更新 requests to a 供应商 实体。.
Modify the product mappings so that when going from 产品 到 ProductDTO, the 供应商名称 field is mapped from the 供应商 实体。.
// product mappings
_ = CreateMap<产品, ProductDTO>().ForMember(x => x.供应商名称, o => o.MapFrom(s => s.供应商.姓名));
_ = CreateMap<CreateProductRequest, 产品>();
_ = CreateMap<更新产品请求, 产品>();With that bit of manual labor out of the way, we can wrap things up in the next step by creating an API controller.
Create the Supplier Controller
We’ll use the Nano CLI again to create an API controller.
在 Developer PowerShell, change directory to MyApp.WebApi\Controllers and run the following command:
dotnet new nano-controller -s Supplier -p Suppliers -ap MyApp
If building with Razor pages, change directory to MyApp.RazorApp\Controllers use this command:
dotnet new nano-controller -s Supplier -p Suppliers -ap MyApp -ui razor
This will create a new controller with endpoints for the CRUD methods provided in the SupplierService we scaffolded earlier.
Testing with Postman
Now we can test with 邮差. Create a new folder for 供应商 用一个 帖子 request called create-supplier. Create a few suppliers; remember to first obtain a token.

创建一个新的 获取 request called get-suppliers-full-list and retrieve the list of suppliers. Copy one of the supplier IDs so that we can use it to create a product in the next step.

在 产品 folder, modify the create-product request body to have a 供应商ID field. Create a new 产品 for one of the suppliers

Now when you retrieve a list of 供应商, an array of 产品 will be returned.

Returning a list of 产品 works as well, with Automapper taking care of the supplier name field for us

With the Nano Boilerplate CLI tool, creating a new app feature is easy. The bulk of the code can be generated using the dotnet new commands, allowing you to focus on the unique aspects of your project.
That concludes the walkthrough, hopefully now you have a better understanding of how to build API endpoints with the Nano ASP.NET boilerplate!