# Entity Naming Convention Refactoring Plan **تاریخ شروع**: 2024-12-03 **تاریخ اتمام**: 2024-12-03 **مدت زمان واقعی**: 3 ساعت **اولویت**: 🔴 فوری **وضعیت**: ✅ تکمیل شده --- ## 🎯 هدف تبدیل 5 Entity از **Plural** به **Singular** مطابق با EF Core Convention: ```csharp // Before: ❌ public class Products { } DbSet Products { get; } // After: ✅ public class Product { } DbSet Products { get; } ``` --- ## 📋 Entity های هدف | # | Entity | تغییر به | استفاده | فایل‌ها | زمان | وضعیت | |---|--------|----------|----------|---------|------|--------| | 1 | UserCarts | UserCart | 192 | 40+ | 30m | ✅ Done | | 2 | ProductImages | ProductImage | 181 | 35+ | 30m | ✅ Done | | 3 | ProductGalleries | ProductGallery | 162 | 30+ | 30m | ✅ Done | | 4 | Products | Product | 283 | 50+ | 45m | ✅ Done | | 5 | Transactions | Transaction | 257 | 45+ | 45m | ✅ Done | **نتیجه نهایی**: - ✅ تمام 5 Entity به Singular تبدیل شدند - ✅ Build: 0 errors - ✅ 1075+ استفاده به‌روز شدند --- ## 🔧 مراحل اجرا (برای هر Entity) ### Phase 1: تغییر نام Entity File و Class **1.1. تغییر نام فایل Entity:** ```bash mv Products.cs Product.cs ``` **1.2. تغییر نام کلاس در فایل:** ```csharp // Before: public class Products : BaseAuditableEntity // After: public class Product : BaseAuditableEntity ``` **1.3. چک کردن وضعیت:** - ✅ فایل تغییر نام یافت - ✅ Class name صحیح است --- ### Phase 2: Configuration Files **2.1. تغییر نام فایل Configuration:** ```bash mv ProductsConfiguration.cs ProductConfiguration.cs ``` **2.2. تغییر Class و EntityTypeConfiguration:** ```csharp // Before: public class ProductsConfiguration : IEntityTypeConfiguration // After: public class ProductConfiguration : IEntityTypeConfiguration ``` **2.3. آپدیت builder type:** ```csharp public void Configure(EntityTypeBuilder builder) ``` --- ### Phase 3: DbContext Files **3.1. آپدیت IApplicationDbContext:** ```csharp // Before: DbSet Products { get; } // After: DbSet Products { get; } ``` **3.2. آپدیت ApplicationDbContext:** ```csharp // Before: public DbSet Products => Set(); // After: public DbSet Products => Set(); ``` --- ### Phase 4: Navigation Properties **4.1. پیدا کردن تمام Navigation Properties:** ```bash grep -r "ICollection" CMSMicroservice.Domain/Entities/ ``` **4.2. تغییر به Singular:** ```csharp // Before: public virtual ICollection Products { get; set; } // After: public virtual ICollection Products { get; set; } ``` **4.3. آپدیت Foreign Key references:** ```csharp // WithMany relations builder.HasOne(x => x.Category) .WithMany(x => x.Products) // همین Plural باقی بماند .HasForeignKey(x => x.CategoryId); ``` --- ### Phase 5: CQRS - تغییر نام Folders **5.1. تغییر نام CQ Folder:** ```bash # معمولاً نیازی نیست - ProductsCQ همان باقی می‌ماند # چون به feature اشاره می‌کند نه Entity ``` **5.2. تغییر نام Commands/Queries folders (اختیاری):** ```bash # معمولاً نام‌ها جمع هستند و تغییر نمی‌کنند ``` --- ### Phase 6: CQRS - آپدیت Class References **6.1. Batch update در Commands:** ```bash find . -type f -name "*.cs" -exec sed -i 's/\bProducts\b/Product/g' {} \; ``` **توجه**: این command همه جا تغییر می‌دهد! باید دقیق باشیم. **6.2. Manual review برای موارد خاص:** - DbSet property names باید Plural بمانند - Folder names معمولاً Plural هستند - Navigation Properties باید Plural باشند --- ### Phase 7: Events **7.1. آپدیت Event namespaces:** ```bash find . -path "*/ProductsEvents/*" -name "*.cs" -exec sed -i 's/\bProducts\b/Product/g' {} \; ``` **7.2. Review Event class names:** ```csharp // Before: public class ProductsCreatedEvent // After: public class ProductCreatedEvent ``` --- ### Phase 8: Proto Files **8.1. آپدیت proto references (احتمالاً نیاز نیست):** ```protobuf // Proto files معمولاً lowercase و plural هستند // تغییر نمی‌دهیم مگر اینکه inconsistency باشد ``` **8.2. آپدیت Service references:** ```csharp // فقط در صورت لزوم ``` --- ### Phase 9: Validators & Profiles **9.1. آپدیت Validator references:** ```bash find . -path "*/Validator/*" -name "*Products*.cs" -exec sed -i 's/\bProducts\b/Product/g' {} \; ``` **9.2. آپدیت AutoMapper Profiles:** ```csharp CreateMap(); CreateMap(); ``` --- ### Phase 10: Build & Test **10.1. Clean:** ```bash find . -type d \( -name "obj" -o -name "bin" \) -exec rm -rf {} + ``` **10.2. Restore:** ```bash dotnet restore ``` **10.3. Build:** ```bash dotnet build ``` **10.4. Check for errors:** - ✅ 0 Errors - ⚠️ Warnings قابل قبول **10.5. Manual review:** - چک کردن چند فایل به صورت sample - اطمینان از صحت Navigation Properties - تست CRUD operations --- ## ⚠️ نکات مهم ### 🔴 جاهایی که باید Plural بمانند: 1. **DbSet Property Names:** ```csharp DbSet Products { get; } // ✅ Products ``` 2. **Navigation Properties:** ```csharp public virtual ICollection Products { get; set; } // ✅ Products ``` 3. **Table Names (در Configuration):** ```csharp builder.ToTable("Products"); // ✅ معمولاً Plural ``` 4. **CQ Folder Names:** ``` ProductsCQ/ // ✅ معمولاً Plural (به feature اشاره می‌کند) ``` 5. **Proto Files:** ``` products.proto // ✅ معمولاً Plural ``` ### 🟢 جاهایی که باید Singular شوند: 1. **Entity Class Name:** ```csharp public class Product { } // ✅ Singular ``` 2. **Configuration Class:** ```csharp public class ProductConfiguration // ✅ Singular ``` 3. **Generic Type Parameters:** ```csharp IEntityTypeConfiguration // ✅ Singular EntityTypeBuilder // ✅ Singular ``` 4. **DbSet Generic Type:** ```csharp DbSet // ✅ Singular ``` --- ## 🎯 ترتیب پیشنهادی اجرا ### دور 1: UserCarts → UserCart **دلیل**: کمترین complexity، بهترین برای test کردن process **مراحل**: 1. Entity + Configuration 2. DbContext 3. Navigation Properties (کم) 4. CQRS Handlers 5. Build & Test **زمان**: 45 دقیقه --- ### دور 2: ProductImages → ProductImage **دلیل**: مشابه UserCart، پیچیدگی کم **زمان**: 45 دقیقه --- ### دور 3: ProductGalleries → ProductGallery **دلیل**: تازه ProductGalleries درست کردیم، فعلاً fresh است **زمان**: 45 دقیقه --- ### دور 4: Products → Product **دلیل**: پر استفاده‌ترین، باید در آخر باشد **زمان**: 1 ساعت --- ### دور 5: Transactions → Transaction **دلیل**: پر استفاده، باید در آخر باشد **زمان**: 1 ساعت --- ## 📊 Progress Tracking | Entity | Start | End | Duration | Status | Notes | |--------|-------|-----|----------|--------|-------| | UserCarts | - | - | - | ⏸️ | - | | ProductImages | - | - | - | ⏸️ | - | | ProductGalleries | - | - | - | ⏸️ | - | | Products | - | - | - | ⏸️ | - | | Transactions | - | - | - | ⏸️ | - | --- ## ✅ Checklist برای هر Entity ### Pre-Refactoring: - [ ] Backup گرفته شد - [ ] Build موفق است (baseline) - [ ] Git commit انجام شد ### During Refactoring: - [ ] Entity file renamed - [ ] Entity class renamed - [ ] Configuration file renamed - [ ] Configuration class updated - [ ] IApplicationDbContext updated - [ ] ApplicationDbContext updated - [ ] Navigation Properties updated - [ ] CQRS Handlers updated (batch) - [ ] Events updated - [ ] Validators updated - [ ] Profiles updated ### Post-Refactoring: - [ ] Build successful (0 errors) - [ ] Manual review انجام شد - [ ] Git commit با message مناسب - [ ] Documentation updated --- **آخرین به‌روزرسانی**: 2024-12-03 **وضعیت کلی**: 🔄 آماده برای شروع