Posts

Attendance App: Real-Time Use of DI Lifetimes

Attendance App: Real-Time Use of DI Lifetimes App Features: Students check-in/out Admin manages students Log every operation (via Singleton) Attendance processed per request (Scoped) Utility services like OTP, Email (Transient)   1. Service Lifetimes in Real Context Service Lifetime Why? ILoggingService Singleton One instance, logs everything app-wide AttendanceService Scoped Scoped per HTTP request (to avoid stale state) EmailNotificationService Transient Lightweight utility, recreated each time OtpGeneratorService Transient Stateless, new per use DbContext (EF Core) Scoped Scoped per request to manage transactions safely   2. Interface Definitions public interface ILoggingService {     void Log(string m...

Difference Between Dispose() and Finalize() in C#

Difference Between Dispose() and Finalize() in C# In C#, both Dispose() and Finalize() are used to release unmanaged resources (like file handles, database connections, or network sockets), but they differ in how and when they are used.   What is Dispose()? Part of the IDisposable interface. Must be called manually by the developer or via a using block. Releases unmanaged resources deterministically (i.e., at a known time). Ideal for cleaning up resources as soon as you're done with them. Commonly used with: Files, database connections, network streams.   What is Finalize()? A special method: ~ClassName() (destructor syntax in C#). Called automatically by the Garbage Collector (GC) . Runs non-deterministically —you don’t know exactly when. Typically used as a backup in case Dispose() wasn’t called. Note: You should avoid using Finalize() unless absolutely necessary because it can slow down garbag...