SSD Backend Setup — การตั้งค่าโปรเจค Backend
บริบท
Backend ของ SSD ใช้ ASP.NET Core 6 ด้วยภาษา C# v10 ทุกโปรเจคต้องใช้ Template ของ SSD จาก GitHub Organization Repository และใช้ Package ชุดเดียวกัน
กฎหลัก
- ต้องใช้ Template ของ SSD เป็นพื้นฐาน ห้ามสร้างโปรเจคเปล่า
- ต้องบันทึกไฟล์เป็น encoding UTF-8, CRLF line endings
- ต้องใช้ Package ที่กำหนดเท่านั้น หากจะใช้ Package อื่นต้องปรึกษาหัวหน้างานก่อน
- ต้องเริ่มจากการออกแบบฐานข้อมูลก่อน แล้วทำ EF Core Reverse Engineer
เครื่องมือที่ต้องติดตั้ง
- Visual Studio 2022
- Extension: EF Core Power Tools — สำหรับ Reverse Engineer
- Extension: NUnit Visual Studio Test Generator — สำหรับ unit test
VS Code Settings (Tool → Options):
- Text Editor → C# → Tabs: Indenting=Smart, Tab size=4, Indent size=4, Insert spaces=True
- Text Editor → C# → Code Style → Naming:
- Interface: Begin with I
- Type: Pascal Case
- Non-Field Members: Pascal Case
- Private/Internal Field: Begin with
_ (camelCase ต่อท้าย)
Package มาตรฐานที่ต้องใช้
ต้องใช้:
- Entity Framework Core (SQL Server) 6.0.16
- Swashbuckle.AspNetCore — Swagger UI
- Serilog 6.1.0 — Logging
- AutoMapper 12.0.1 — Object mapping
- RestSharp 109.0.1 — HTTP client
- Quartz 3.6.2 — Background jobs
ควรใช้ตามความเหมาะสม:
- MassTransit 7.2.4 — Message Queue
- NPOI 2.6.0 — Excel
- FluentValidation — Input validation
- CSVHelper — CSV files
Naming Conventions
Code (C#)
| รูปแบบ |
ใช้กับ |
PascalCase |
class, method, enum, public field, public property, namespace |
camelCase |
local variable, parameter |
_camelCase |
private/protected/internal field, property |
INameHere |
interface (ขึ้นต้นด้วย I) |
กฎสำหรับตัวย่อ:
- ตัวย่อ 2+ คำ: ตัวใหญ่ทั้งหมด เช่น
DTO, IT, SMI, API, HTML
- ตัวย่อ 1 คำ: ตัวใหญ่เฉพาะตัวแรก เช่น
Id, Cust, Doc, Temp
- ใน camelCase พยางค์แรก: ตัวเล็กทั้งหมด เช่น
html, xml
- ใน camelCase พยางค์ 2+: ตัวใหญ่ทั้งหมด เช่น
docHTML, docXML
- ยกเว้นตัวย่อ 1 คำ พยางค์ 2+: camelCase เช่น
docId, custId
Files
- ชื่อไฟล์และ folder ใช้ PascalCase เช่น
MyRequestDTO.cs, SMIDataService.cs
- ชื่อไฟล์ต้องเป็นชื่อเดียวกับ class หลักในไฟล์
- 1 ไฟล์ = 1 class หลัก
การเรียงลำดับ Modifiers
public protected internal private new abstract virtual override sealed static readonly extern unsafe volatile async
การเรียงลำดับ Class Members
- class ย่อย, enums, delegates, events
- static, const, readonly fields
- fields และ properties
- constructors และ finalizers
- methods
ภายในแต่ละกลุ่ม: public → internal → protected internal → protected → private
โครงสร้าง Folder มาตรฐาน
ProjectName/
├── Attributes/
│ ├── Auth/ # Permission attributes
│ └── Validation/ # Validation attributes
├── Clients/ # RestSharp API clients
├── Configurations/ # Configuration classes (Option Pattern)
├── Consumers/ # MassTransit consumers
├── Controllers/ # API controllers
├── Data/ # EF Core DbContext
├── DTOs/
│ ├── Auth/ # Auth DTOs
│ └── Pagination/ # Pagination DTOs
├── Exceptions/
├── Helpers/ # Helper / Extension Methods
├── HostedServices/ # Quartz background jobs
├── Logs/
├── Middleware/
├── Models/
│ ├── Base/ # Base models
│ ├── Partial/ # Partial class extensions
│ └── Response/ # Response models
├── Services/
│ ├── Auth/ # User authentication service
│ └── Base/ # Base service examples
├── Startups/
├── AutoMapperProfile.cs # AutoMapper mappings
└── ProjectSetup.cs # DI configuration
ไฟล์สำคัญ:
AutoMapperProfile.cs — เก็บ Mapping ระหว่าง Model และ DTO ทั้งหมด
ProjectSetup.cs — เก็บ DI setup ทั้งหมด มีส่วน ConfigDependency, ConfigQuartz, ConfigRabbitMQ, ConfigKafka
EF Core Database Reverse Engineer
ขั้นตอนที่ 1: คลิกขวาที่ Project
เลือก EF Core Power Tools → Reverse Engineer
ขั้นตอนที่ 2: เลือก Database Connection
เลือกฐานข้อมูลที่ต้องการ หรือ Add Connection String ใหม่ → EF Core version = EF Core 6.0
ขั้นตอนที่ 3: เลือกตาราง
เลือกตารางที่จะใช้งาน
ขั้นตอนที่ 4: ตั้งค่า Settings
- Context Name:
- ฐานข้อมูลเดียว:
AppDbContext
- หลายฐานข้อมูล:
[DatabaseName]Context เช่น LogDbContext
- EntityTypes path:
Models
- DbContext path:
Data
- Naming — เลือก Configuration:
- ✔ Pluralize or singularize generated object names (English)
- ✔ Use table and column names directly from the database
- ✔ Use DataAnnotations attributes to configure the model
ขั้นตอนที่ 5: หากต้องการเปลี่ยนชื่อ Class/Field
กด F2 บนชื่อ table หรือ field ที่ต้องการเปลี่ยน → โปรแกรมจะสร้างไฟล์ efpt.renaming.json อัตโนมัติ
(ต้องปิด option "Use table and column names directly from the database" ด้วย)
กรณีหลายฐานข้อมูล
- เปลี่ยนชื่อ
efpt.config.json เป็น efpt.[ชื่อDb].config.json
- Reverse Engineer ฐานข้อมูลที่ 2: Context Name =
[ชื่อDb]Context, EntityTypes path = Models/[ชื่อDb]
- เพิ่ม
AddDbContext ใน Startup.cs และ Connection String ใน appsettings.json
Model Relationships ด้วย Partial Class
เมื่อต้องเพิ่ม relationship ที่ไม่ได้สร้างใน Database ให้ใช้ Partial Class:
// /Models/Partial/Agent.cs
namespace MyProject.Models
{
public partial class Agent
{
public virtual ICollection<CallResult> CallResults { get; set; }
}
}
// /Data/Partial/AppDbContext.cs
namespace MyProject.Data
{
public partial class AppDbContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Agent>(entity =>
{
entity.HasMany(e => e.CallResults)
.WithOne(e => e.Agent)
.HasForeignKey(e => e.AgentId);
});
}
}
}
1---2name: ssd-backend-setup3description: ใช้ skill นี้เมื่อตั้งค่าโปรเจค .NET Backend ใหม่, ถามเรื่องโครงสร้าง folder, naming conventions ของ C#, หรือ EF Core Reverse Engineer ตามมาตรฐาน SSD4---56# SSD Backend Setup — การตั้งค่าโปรเจค Backend78## บริบท910Backend ของ SSD ใช้ ASP.NET Core 6 ด้วยภาษา C# v10 ทุกโปรเจคต้องใช้ Template ของ SSD จาก GitHub Organization Repository และใช้ Package ชุดเดียวกัน1112## กฎหลัก13141. ต้องใช้ Template ของ SSD เป็นพื้นฐาน ห้ามสร้างโปรเจคเปล่า152. ต้องบันทึกไฟล์เป็น encoding UTF-8, CRLF line endings163. ต้องใช้ Package ที่กำหนดเท่านั้น หากจะใช้ Package อื่นต้องปรึกษาหัวหน้างานก่อน174. ต้องเริ่มจากการออกแบบฐานข้อมูลก่อน แล้วทำ EF Core Reverse Engineer1819## เครื่องมือที่ต้องติดตั้ง2021- Visual Studio 202222- Extension: **EF Core Power Tools** — สำหรับ Reverse Engineer23- Extension: **NUnit Visual Studio Test Generator** — สำหรับ unit test2425**VS Code Settings (Tool → Options):**26- Text Editor → C# → Tabs: Indenting=Smart, Tab size=4, Indent size=4, Insert spaces=True27- Text Editor → C# → Code Style → Naming:28 - Interface: Begin with I29 - Type: Pascal Case30 - Non-Field Members: Pascal Case31 - Private/Internal Field: Begin with `_` (camelCase ต่อท้าย)3233## Package มาตรฐานที่ต้องใช้3435**ต้องใช้:**36- Entity Framework Core (SQL Server) 6.0.1637- Swashbuckle.AspNetCore — Swagger UI38- Serilog 6.1.0 — Logging39- AutoMapper 12.0.1 — Object mapping40- RestSharp 109.0.1 — HTTP client41- Quartz 3.6.2 — Background jobs4243**ควรใช้ตามความเหมาะสม:**44- MassTransit 7.2.4 — Message Queue45- NPOI 2.6.0 — Excel46- FluentValidation — Input validation47- CSVHelper — CSV files4849## Naming Conventions5051### Code (C#)5253| รูปแบบ | ใช้กับ |54|--------|--------|55| `PascalCase` | class, method, enum, public field, public property, namespace |56| `camelCase` | local variable, parameter |57| `_camelCase` | private/protected/internal field, property |58| `INameHere` | interface (ขึ้นต้นด้วย I) |5960**กฎสำหรับตัวย่อ:**61- ตัวย่อ 2+ คำ: ตัวใหญ่ทั้งหมด เช่น `DTO`, `IT`, `SMI`, `API`, `HTML`62- ตัวย่อ 1 คำ: ตัวใหญ่เฉพาะตัวแรก เช่น `Id`, `Cust`, `Doc`, `Temp`63- ใน camelCase พยางค์แรก: ตัวเล็กทั้งหมด เช่น `html`, `xml`64- ใน camelCase พยางค์ 2+: ตัวใหญ่ทั้งหมด เช่น `docHTML`, `docXML`65- ยกเว้นตัวย่อ 1 คำ พยางค์ 2+: camelCase เช่น `docId`, `custId`6667### Files68- ชื่อไฟล์และ folder ใช้ PascalCase เช่น `MyRequestDTO.cs`, `SMIDataService.cs`69- ชื่อไฟล์ต้องเป็นชื่อเดียวกับ class หลักในไฟล์70- 1 ไฟล์ = 1 class หลัก7172### การเรียงลำดับ Modifiers73```csharp74public protected internal private new abstract virtual override sealed static readonly extern unsafe volatile async75```7677### การเรียงลำดับ Class Members781. class ย่อย, enums, delegates, events792. static, const, readonly fields803. fields และ properties814. constructors และ finalizers825. methods8384ภายในแต่ละกลุ่ม: public → internal → protected internal → protected → private8586## โครงสร้าง Folder มาตรฐาน8788```89ProjectName/90├── Attributes/91│ ├── Auth/ # Permission attributes92│ └── Validation/ # Validation attributes93├── Clients/ # RestSharp API clients94├── Configurations/ # Configuration classes (Option Pattern)95├── Consumers/ # MassTransit consumers96├── Controllers/ # API controllers97├── Data/ # EF Core DbContext98├── DTOs/99│ ├── Auth/ # Auth DTOs100│ └── Pagination/ # Pagination DTOs101├── Exceptions/102├── Helpers/ # Helper / Extension Methods103├── HostedServices/ # Quartz background jobs104├── Logs/105├── Middleware/106├── Models/107│ ├── Base/ # Base models108│ ├── Partial/ # Partial class extensions109│ └── Response/ # Response models110├── Services/111│ ├── Auth/ # User authentication service112│ └── Base/ # Base service examples113├── Startups/114├── AutoMapperProfile.cs # AutoMapper mappings115└── ProjectSetup.cs # DI configuration116```117118**ไฟล์สำคัญ:**119- `AutoMapperProfile.cs` — เก็บ Mapping ระหว่าง Model และ DTO ทั้งหมด120- `ProjectSetup.cs` — เก็บ DI setup ทั้งหมด มีส่วน `ConfigDependency`, `ConfigQuartz`, `ConfigRabbitMQ`, `ConfigKafka`121122## EF Core Database Reverse Engineer123124### ขั้นตอนที่ 1: คลิกขวาที่ Project125เลือก **EF Core Power Tools → Reverse Engineer**126127### ขั้นตอนที่ 2: เลือก Database Connection128เลือกฐานข้อมูลที่ต้องการ หรือ Add Connection String ใหม่ → EF Core version = **EF Core 6.0**129130### ขั้นตอนที่ 3: เลือกตาราง131เลือกตารางที่จะใช้งาน132133### ขั้นตอนที่ 4: ตั้งค่า Settings134- **Context Name:**135 - ฐานข้อมูลเดียว: `AppDbContext`136 - หลายฐานข้อมูล: `[DatabaseName]Context` เช่น `LogDbContext`137- **EntityTypes path:** `Models`138- **DbContext path:** `Data`139- **Naming — เลือก Configuration:**140 - ✔ Pluralize or singularize generated object names (English)141 - ✔ Use table and column names directly from the database142 - ✔ Use DataAnnotations attributes to configure the model143144### ขั้นตอนที่ 5: หากต้องการเปลี่ยนชื่อ Class/Field145กด F2 บนชื่อ table หรือ field ที่ต้องการเปลี่ยน → โปรแกรมจะสร้างไฟล์ `efpt.renaming.json` อัตโนมัติ146(ต้องปิด option "Use table and column names directly from the database" ด้วย)147148### กรณีหลายฐานข้อมูล1491. เปลี่ยนชื่อ `efpt.config.json` เป็น `efpt.[ชื่อDb].config.json`1502. Reverse Engineer ฐานข้อมูลที่ 2: Context Name = `[ชื่อDb]Context`, EntityTypes path = `Models/[ชื่อDb]`1513. เพิ่ม `AddDbContext` ใน Startup.cs และ Connection String ใน appsettings.json152153## Model Relationships ด้วย Partial Class154155เมื่อต้องเพิ่ม relationship ที่ไม่ได้สร้างใน Database ให้ใช้ Partial Class:156157```csharp158// /Models/Partial/Agent.cs159namespace MyProject.Models160{161 public partial class Agent162 {163 public virtual ICollection<CallResult> CallResults { get; set; }164 }165}166```167168```csharp169// /Data/Partial/AppDbContext.cs170namespace MyProject.Data171{172 public partial class AppDbContext173 {174 partial void OnModelCreatingPartial(ModelBuilder modelBuilder)175 {176 modelBuilder.Entity<Agent>(entity =>177 {178 entity.HasMany(e => e.CallResults)179 .WithOne(e => e.Agent)180 .HasForeignKey(e => e.AgentId);181 });182 }183 }184}185```