2.3 KiB
name, description
| name | description |
|---|---|
| GenerateNTierFeature | Hướng dẫn tạo một feature theo chuẩn N-Tier (bao gồm Entity, Interface, Service, Controller và cấu hình Dependency Injection). |
GenerateNTierFeature Skill
Khi user yêu cầu tạo một feature theo luồng N-Tier với đầu vào là Tên Feature (VD: Category), bạn BẮT BUỘC phải thực hiện các bước sau để sinh ra code và file tương ứng:
1. Đầu ra thư mục và file (Outputs)
Hãy tạo các file sau với nội dung phù hợp cho {FeatureName}:
-
Domain Entity
- Đường dẫn:
Domain/Entities/{FeatureName}.cs - Nội dung: Lớp Entity cơ bản định nghĩa các thuộc tính.
- Đường dẫn:
-
Service Interface
- Đường dẫn:
Application/Interfaces/I{FeatureName}Service.cs - Nội dung: Các interface định nghĩa hợp đồng hàm cho {FeatureName}.
- Đường dẫn:
-
Service Implementation
- Đường dẫn:
Application/Services/{FeatureName}Service.cs - Nội dung: Lớp kế thừa từ
I{FeatureName}Service. Yêu cầu: Nhận Dependency Injection (DI) thông qua constructor (ví dụ:IRepository<{FeatureName}>).
- Đường dẫn:
-
WebAPI Controller
- Đường dẫn:
WebAPI/Controllers/{FeatureName}sController.cs(Lưu ý thêm số nhiều cho tên Controller). - Nội dung: Lớp Controller. Yêu cầu: Phải nhận DI
I{FeatureName}Servicethông qua constructor và định nghĩa các endpoint tương ứng.
- Đường dẫn:
2. Cấu hình Dependency Injection (BƯỚC BẮT BUỘC THỰC HIỆN)
⚠️ Quan trọng: Không giống như CQRS với MediatR ở trên, luồng N-Tier đòi hỏi bạn phải chèn thủ công service mới tạo vào Container IoC.
Bạn BẮT BUỘC phải sử dụng tool để mở file cấu hình DI của project (có thể là DependencyInjection.cs hoặc ServiceCollectionExtensions.cs tùy cấu trúc thư mục) và chèn đoạn mã sau vào hàm cấu hình service liên quan:
services.AddScoped<I{FeatureName}Service, {FeatureName}Service>();
Hãy đảm bảo bạn dùng công cụ sửa file chính xác (replace_file_content hoặc multi_replace_file_content) để không làm hỏng cú pháp của file DI.