التطوير باستخدام Nano ASP.NET Boilerplate: إنشاء خدمة CRUD جديدة
البدء
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 المنتج entity.
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 one-to-many relationship with the المنتج entity.
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 AuditableEntity.
عام فئة المورد : AuditableEntity
{
عام سلسلة الاسم { احصل على؛; تعيين؛; }
عام ICollection<المنتج> المنتجات { احصل على؛; تعيين؛; }
}Next we need to modify the المنتج entity to complete the one-to-many relationship for Entity Framework. Add a رقم المورد field and المورد navigation property.
عام فئة المنتج : AuditableEntity
{
عام سلسلة الاسم { احصل على؛; تعيين؛; }
عام سلسلة الوصف { احصل على؛; تعيين؛; }
عام المورد المورد { احصل على؛; تعيين؛; }
عام Guid SupplierId { احصل على؛; تعيين؛; }
}All entities should directly or indirectly inherit from abstract class الكيان الأساسي, found in /Entities/Common.
AuditableEntity is an abstract class that inherits from TenantBaseEntity (or directly from الكيان الأساسي in single-tenant deployments). AuditableEntity adds the تم إنشاؤه بواسطة, تاريخ الإنشاء, 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 AuditableEntity.
TenantBaseEntity inherits from the الكيان الأساسي class and adds the رقم المستأجر field. By implementing the يجب أن يكون لدي مستأجر interface, query filters in the ApplicationDbContext will isolate tenant data.
افتح ApplicationDbContext في Infrastructure/Persistence/Contexts/ and create the DbSet من أجل المورد
عام DbSet<المورد> الموردون { احصل على; مجموعة; }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 البنية التحتية. 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 تحديث قاعدة البيانات الأمر.
في 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:
عام واجهة ISupplierService : ITransientService
{
المهمة<Response<IEnumerable<المورد DTO>>> GetSuppliersAsync(سلسلة keyword = "");
المهمة<PaginatedResponse<المورد DTO>> GetSuppliersPaginatedAsync(SupplierTableFilter filter);
المهمة<Response<المورد DTO>> GetSupplierAsync(Guid id);
المهمة<Response<Guid>> CreateSupplierAsync(طلب إنشاء مورد (CreateSupplierRequest));
المهمة<Response<Guid>> UpdateSupplierAsync(UpdateSupplierRequest request, Guid id);
المهمة<Response<Guid>> 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
منظمات تجارة المخدرات من أجل 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 المعرّف و الاسم 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:
عام فئة المورد DTO : IDto
{
عام Guid Id { احصل على؛; تعيين؛; }
عام سلسلة الاسم { احصل على؛; تعيين؛; }
عام IList<ProductDTO> المنتجات { احصل على؛; تعيين؛; }
}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.
عام فئة إنشاء طلب مورد : IDto
{
عام سلسلة الاسم { احصل على؛; تعيين؛; }
}
عام فئة CreateSupplierValidator : AbstractValidator<إنشاء طلب مورد>
{
عام CreateSupplierValidator()
{
RuleFor(x => x.الاسم).NotEmpty();
}
}Update the Product DTOs
Because we now have a one-to-many relationship between المورد و المنتج, creating and updating المنتجات requires a supplier ID. Navigate to Application/Services/ProductService/DTOs and add a رقم المورد property for the تحديث و إنشاء product requests.
عام فئة طلب إنشاء منتج : IDto
{
عام سلسلة الاسم { احصل على؛; تعيين؛; }
عام سلسلة الوصف { احصل على؛; تعيين؛; }
عام Guid SupplierId { احصل على؛; تعيين؛; }
}
عام فئة CreateProductValidator : AbstractValidator<طلب إنشاء منتج>
{
عام CreateProductValidator()
{
_ = RuleFor(x => x.الاسم).NotEmpty();
_ = RuleFor(x => x.الوصف).NotEmpty();
_ = RuleFor(x => x.رقم المورد).NotEmpty();
}
}في ProductDTO, add a GUID رقم المورد وـ اسم المورد string property. In the next step, we will configure Automapper to populate the اسم المورد whenever mapping occurs.
عام فئة ProductDTO : IDto
{
عام Guid Id { احصل على؛; تعيين؛; }
عام سلسلة? الاسم { احصل على؛; تعيين؛; }
عام سلسلة? الوصف { احصل على؛; تعيين؛; }
عام التاريخ والوقت: تاريخ الإنشاء { احصل على؛; تعيين؛; }
عام سلسلة اسم المورد { احصل على؛; تعيين؛; }
عام Guid SupplierId { احصل على؛; تعيين؛; }
}Configure Automapping
Next we need to set up mapping profiles for the new DTOs
في Infrastructure/Mapper/MappingProfiles add the following configurations for supplier:
// supplier mappings...
_ = CreateMap<المورد, المورد DTO>();
_ = CreateMap<إنشاء طلب مورد, المورد>();
_ = CreateMap<تحديث طلب المورد, المورد>();This will ensure that Automapper knows how to map a المورد entity to a المورد DTO and the إنشاء / تحديث requests to a المورد entity.
Modify the product mappings so that when going from المنتج إلى ProductDTO, ، الـ اسم المورد field is mapped from the المورد entity.
// product mappings
_ = CreateMap<المنتج, ProductDTO>().ForMember(x => x.اسم المورد, o => o.MapFrom(s => s.المورد.الاسم));
_ = CreateMap<طلب إنشاء منتج, المنتج>();
_ = 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-وحدة التحكم -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 الموردون with a منشور request called create-supplier. Create a few suppliers; remember to first obtain a token.

Create a new احصل على 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 رقم المورد 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!