# Dependency Injection (.NET) > Lifetime ของ service (singleton, scoped, transient), การลงทะเบียน service, IServiceProvider, แนวทางปฏิบัติที่ดี - 24 คำถามสัมภาษณ์ - Mid-Level - [คำถามสัมภาษณ์: .NET](https://sharpskill.dev/th/technologies/dotnet/interview-questions.md) ## 1. ความแตกต่างหลักระหว่าง lifetime Singleton, Scoped และ Transient คืออะไร? **คำตอบ** Singleton สร้าง instance เดียวตลอด lifetime ของแอปพลิเคชัน, Scoped สร้างหนึ่ง instance ต่อ HTTP request (หรือ scope) และ Transient สร้าง instance ใหม่ทุกครั้งที่ถูก inject lifetime เหล่านี้กำหนดว่าเมื่อใดและกี่ครั้งที่ container DI จะสร้าง instance ของ service ใช้ Singleton สำหรับ service ที่ไม่มี state แบบแชร์, Scoped สำหรับ service ที่ผูกกับ request (เช่น DbContext) และ Transient สำหรับ service น้ำหนักเบาที่ไม่มี state ## 2. ควรใช้ lifetime ใดในการลงทะเบียน DbContext ของ Entity Framework Core? **คำตอบ** ควรลงทะเบียน DbContext ด้วย lifetime แบบ Scoped เพราะมันไม่ thread-safe และเก็บสถานะ tracking ของ entity ไว้ แต่ละคำขอ HTTP ควรมี instance ของ DbContext เป็นของตัวเองเพื่อหลีกเลี่ยงปัญหาการทำงานพร้อมกัน การลงทะเบียนทำได้ด้วย AddDbContext ซึ่งกำหนดค่า lifetime แบบ Scoped ให้โดยอัตโนมัติ การใช้ Singleton จะทำให้เกิดข้อผิดพลาดด้านการทำงานพร้อมกัน และ Transient จะสิ้นเปลืองทรัพยากรเพราะสร้าง instance มากเกินไป ## 3. จะลงทะเบียน service ด้วย interface และการ implement ของมันได้อย่างไร? **คำตอบ** การลงทะเบียน service ทำได้ด้วยเมธอด AddSingleton, AddScoped หรือ AddTransient โดยระบุ interface เป็นประเภทของ service และคลาสที่เป็นรูปธรรมเป็นการ implement ไวยากรณ์คือ services.Add{Lifetime}() แนวทางนี้ยึดตามหลักการ dependency inversion และช่วยให้การทดสอบง่ายขึ้นโดยอนุญาตให้แทนที่การ implement ได้ คอนเทนเนอร์ DI จะ resolve การ implement ที่เป็นรูปธรรมโดยอัตโนมัติเมื่อ inject interface ## มีอีก 21 คำถาม - ในกรณีใดที่ควรเลือกใช้ AddSingleton มากกว่า AddScoped? - IServiceProvider มีบทบาทอย่างไรในระบบ DI? สมัครฟรี: https://sharpskill.dev/th/login ## หัวข้อสัมภาษณ์ .NET อื่นๆ - [พื้นฐาน C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-basics.md): 25 คำถาม, Junior - [LINQ & Delegates](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-linq-delegates.md): 20 คำถาม, Junior - [พื้นฐานสำคัญของภาษา C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-language-essentials.md): 15 คำถาม, Junior - [พื้นฐาน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/aspnet-core-fundamentals.md): 18 คำถาม, Junior - [วงจรชีวิตของ request ใน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/aspnet-core-request-lifecycle.md): 20 คำถาม, Junior - [Configuration & Settings](https://sharpskill.dev/th/technologies/dotnet/interview-questions/configuration-settings.md): 20 คำถาม, Junior - [วงจรชีวิตของแอปพลิเคชัน](https://sharpskill.dev/th/technologies/dotnet/interview-questions/application-lifecycle.md): 20 คำถาม, Junior - [Entity Framework Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/entity-framework-core.md): 25 คำถาม, Mid-Level - [Minimal APIs](https://sharpskill.dev/th/technologies/dotnet/interview-questions/minimal-apis.md): 18 คำถาม, Mid-Level - [การพัฒนา Web API](https://sharpskill.dev/th/technologies/dotnet/interview-questions/web-api-development.md): 22 คำถาม, Mid-Level - [Async ใน ASP.NET Core](https://sharpskill.dev/th/technologies/dotnet/interview-questions/async-aspnet-core.md): 20 คำถาม, Mid-Level - [Authentication & Authorization](https://sharpskill.dev/th/technologies/dotnet/interview-questions/authentication-authorization.md): 18 คำถาม, Mid-Level - [HttpClient & Networking](https://sharpskill.dev/th/technologies/dotnet/interview-questions/httpclient-networking.md): 20 คำถาม, Mid-Level - [การซีเรียลไลซ์ JSON](https://sharpskill.dev/th/technologies/dotnet/interview-questions/json-serialization.md): 20 คำถาม, Mid-Level - [Entity Framework Core ขั้นสูง](https://sharpskill.dev/th/technologies/dotnet/interview-questions/ef-core-advanced.md): 25 คำถาม, Mid-Level - [ฟีเจอร์ขั้นสูงของ C#](https://sharpskill.dev/th/technologies/dotnet/interview-questions/csharp-advanced-features.md): 20 คำถาม, Mid-Level - [Clean Architecture](https://sharpskill.dev/th/technologies/dotnet/interview-questions/clean-architecture.md): 20 คำถาม, Mid-Level - [Logging, Monitoring & Observability](https://sharpskill.dev/th/technologies/dotnet/interview-questions/logging-monitoring.md): 22 คำถาม, Mid-Level - [Unit Testing และ xUnit](https://sharpskill.dev/th/technologies/dotnet/interview-questions/unit-testing-xunit.md): 20 คำถาม, Mid-Level - [การทดสอบแบบบูรณาการ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/integration-testing.md): 18 คำถาม, Mid-Level - [Docker & Containerization](https://sharpskill.dev/th/technologies/dotnet/interview-questions/docker-containerization.md): 16 คำถาม, Mid-Level - [การจัดการแพ็กเกจ NuGet](https://sharpskill.dev/th/technologies/dotnet/interview-questions/nuget-package-management.md): 16 คำถาม, Mid-Level - [Memory Management & GC](https://sharpskill.dev/th/technologies/dotnet/interview-questions/memory-management-gc.md): 22 คำถาม, Senior - [การเขียนโปรแกรมแบบรีแอกทีฟ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/reactive-programming.md): 24 คำถาม, Senior - [Async/await และรูปแบบ](https://sharpskill.dev/th/technologies/dotnet/interview-questions/async-await-patterns.md): 26 คำถาม, Senior - [.NET Design Patterns](https://sharpskill.dev/th/technologies/dotnet/interview-questions/dotnet-design-patterns.md): 24 คำถาม, Senior - [การเพิ่มประสิทธิภาพการทำงาน](https://sharpskill.dev/th/technologies/dotnet/interview-questions/performance-optimization.md): 22 คำถาม, Senior - [ความปลอดภัยและ Best Practices](https://sharpskill.dev/th/technologies/dotnet/interview-questions/security-best-practices.md): 24 คำถาม, Senior - [SignalR และเรียลไทม์](https://sharpskill.dev/th/technologies/dotnet/interview-questions/signalr-realtime.md): 22 คำถาม, Senior - [สถาปัตยกรรม Microservices](https://sharpskill.dev/th/technologies/dotnet/interview-questions/microservices-architecture.md): 25 คำถาม, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/th/technologies/dotnet/interview-questions/dependency-injection