Trong các dự án enterprise .NET, chúng ta thường có:
- Coding convention riêng
- Architecture pattern riêng
- Folder structure riêng
Vấn đề là GitHub Copilot thường generate code theo pattern phổ biến trên internet, chứ không theo convention của project.
Trong bài viết này mình thử nghiệm một cách “huấn luyện nhẹ” Copilot để nó tuân theo structure của project.
- Setup GitHub Copilot trong Visual Studio 2026
- Tạo file
copilot-instructions.md - Structured prompt với constraints
Setup GitHub Copilot trong Visual Studio 2026
Đăng nhập GitHub
View → GitHub Copilot Chat
Login GitHub account của bạn.
Copilot sẽ hoạt động nếu bạn có:
- Copilot Individual
- Copilot Business
- Copilot Enterprise
Test Copilot
Tạo file:
TestRepository.cs
Viết comment:
// create a generic repository for EF Core
Nếu Copilot gợi ý code → setup thành công.
Vấn đề khi dùng Copilot trong dự án thật
Mục tiêu là rewrite theo pattern:
- Entity Framework
- Repository Pattern
- Unit Of Work
Nhưng Copilot thường:
- generate generic repository phổ biến trên internet
- không theo folder structure của project
- sai namespace
- thêm code không cần thiết
Ví dụ Copilot hay generate:
public class GenericRepository<T> where T : class
{
private readonly DbContext _context;
public GenericRepository(DbContext context)
{
_context = context;
}
}
Markdown Guideline
Visual Studio 2026 chưa có Agent Skills, nên mình dùng workaround đơn giản:
copilot-instructions.md
File Markdown này đóng vai trò như một guideline cho Copilot.
Ví dụ copilot-instructions.md
# Copilot Coding Guidelines
Follow these rules when generating code.
## Architecture
- Repository Pattern
- Unit of Work
- Entity Framework DbContext
## Folder Structure
DataAccess
├── Entities
├── Interfaces
├── Repositories
└── UnitOfWork
## Namespace Convention
Company.Project.DataAccess.Entities
Company.Project.DataAccess.Repositories
Company.Project.DataAccess.Interfaces
## Repository Rules
Each entity must have:
I<Entity>Repository
<Entity>Repository
Structured Prompt
Nếu prompt quá đơn giản, Copilot thường thêm code không cần thiết.
Dùng structured prompt:
Task:
Create repository for Customer entity.
Constraints:
- Follow folder structure from copilot-instructions.md
- Do not modify DbContext
- Do not register services
- Only generate repository class
Constraints giúp Copilot không “sáng tạo quá đà”.
Workflow hiệu quả
Step 1 — Generate plan
Based on copilot-instructions.md,
generate folder structure for DataAccess layer.
Step 2 — Generate interface
Create interface ICustomerRepository
following copilot guidelines.
Step 3 — Generate repository
Create CustomerRepository implementation
following copilot guidelines.
Copilot làm tốt những gì
Copilot rất mạnh khi generate:
- CRUD methods
- DTO
- EF configuration
- repetitive boilerplate
public async Task<IEnumerable<Customer>> GetAllAsync()
{
return await _context.Customers.ToListAsync();
}
Copilot Planning Mode
Một tính năng khá thú vị trong GitHub Copilot trên Visual Studio 2026 là Planning Mode.
Thay vì viết code ngay lập tức, Copilot có thể lập kế hoạch trước khi triển khai. Điều này đặc biệt hữu ích khi yêu cầu phức tạp.
Ví dụ:
Implement JWT authentication
Trong trường hợp này, Copilot sẽ không nhảy vào viết code ngay mà đề xuất một kế hoạch triển khai trước.
Cách bật Planning Mode
Mở Visual Studio và vào:
Tools → Options → GitHub → Copilot → Enable Planning
Sau khi bật, bạn nên restart Visual Studio để đảm bảo tính năng hoạt động ổn định.
Planning Mode hoạt động như thế nào?
Khi Copilot nhận một yêu cầu lớn, nó sẽ thực hiện ba bước chính.
1. Tạo kế hoạch
Copilot có thể tạo một file Markdown tạm thời chứa các bước triển khai.
Implementation Plan
1. Create JwtTokenService
2. Update Authentication middleware
3. Add configuration in appsettings.json
4. Update dependency injection
2. Phân rã tác vụ
Thay vì generate code một lần, Copilot sẽ chia yêu cầu thành nhiều bước nhỏ:
- Tạo class
- Cập nhật configuration
- Sửa các file liên quan
- Triển khai từng phần theo thứ tự
Điều này giúp developer dễ review và kiểm soát quá trình generate code.
3. Xác nhận trước khi triển khai
Trước khi Copilot bắt đầu generate code, bạn có thể:
- Chỉnh sửa kế hoạch
- Thêm hoặc bỏ bước
- Thay đổi approach
Sau đó mới cho Copilot Execute / Implement.
Cách sử dụng trong Copilot Chat
Sau khi bật Planning Mode, bạn có thể kích hoạt theo hai cách.
1. Tự động
Nếu prompt đủ phức tạp, Copilot sẽ hỏi:
Would you like me to create a plan first?
2. Chủ động
Một số phiên bản Copilot Chat hỗ trợ lệnh:
/plan
Hoặc bạn có thể chọn Plan Mode từ menu trong cửa sổ Copilot Chat.
Tại sao Planning Mode hữu ích với copilot-instructions.md?
Nếu project có file guideline như:
copilot-instructions.md
Copilot thường sẽ đọc file hướng dẫn này ngay từ bước lập kế hoạch.
Điều này giúp:
- Giảm sai namespace
- Giảm sai folder structure
- Hạn chế generate code ngoài scope
Nói cách khác, Copilot sẽ hiểu kiến trúc trước khi viết code.
Khi kết hợp Planning Mode + structured prompt + guideline file, Copilot bắt đầu hoạt động giống một developer có checklist thay vì chỉ là một autocomplete engine.
Kết luận
GitHub Copilot rất tốt cho:
- CRUD scaffolding
- repetitive code
- common patterns
Nhưng vẫn còn hạn chế với:
- project-specific architecture
- custom folder convention
- pattern generalization
Nhận xét
Đăng nhận xét