Posts

Attendance App: Real-Time Use of DI Lifetimes

Image
Dependency Injection (DI) in ASP.NET Core is more than just a design pattern — it’s a powerful tool for managing service lifetimes and dependencies in real-world applications. In this guide, we'll explore how an Attendance App uses different DI lifetimes (Singleton, Scoped, Transient) to handle services like logging, attendance tracking, OTP generation, and email notifications. ๐ŸŽฏ App Features Students check-in/out Admins manage students Operations are logged (Singleton) Attendance tracked per request (Scoped) Email/OTP as lightweight utilities (Transient) ๐Ÿ” Quick Overview: DI Lifetimes in ASP.NET Core Before diving into the app, here’s a quick refresher on DI lifetimes: Singleton : One instance shared across the entire application lifetime. Good for stateless or shared resources (e.g., logging). Scoped : One instance per client request. Ideal for services that maintain request-specific state or database transactions . ...

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

Image
In C#, both Dispose() and Finalize() are used to release unmanaged resources such as file handles, database connections, or network sockets. These are resources not handled by the .NET runtime's garbage collector. Learn more about unmanaged resources on the official Microsoft documentation . ๐Ÿงน What is Dispose() ? Part of the IDisposable interface Must be called manually or via a using block Releases resources deterministically Ideal for: Files, database connections, network streams ๐Ÿ—‘️ What is Finalize() ? Special method: ~ClassName() (destructor syntax) Called automatically by the Garbage Collector Executes non-deterministically Used as a backup if Dispose() is not called Warning: Finalize can slow down garbage collection ๐Ÿ“Š Ke...

Difference Between Abstraction and Encapsulation in C#

Image
Difference Between Abstraction and Encapsulation in C# Both Abstraction and Encapsulation are fundamental concepts of Object-Oriented Programming (OOP), but they serve different purposes. Let’s break them down clearly with code examples and key comparisons. ๐Ÿ” What is Abstraction? Abstraction is the process of hiding implementation details and exposing only the essential features of an object. It focuses on what an object does rather than how it does it . ✅ How Abstraction is Implemented: Interfaces Abstract classes ๐Ÿงช Example: public interface IPrinter { void Print(); } public class LaserPrinter : IPrinter { public void Print() { // Complex logic hidden from user Console.WriteLine("Printing document..."); } } Here, the user interacts with the IPrinter interface without knowing the internal logic of LaserPrinter . ๐Ÿ” What is Encapsulation? Encapsulation is the technique of wrapping data (fields) and code (methods...

Difference Between Interface and Abstract Class in C#

Image
Difference Between Interface and Abstract Class in C# In object-oriented programming with C#, both interfaces and abstract classes are used to achieve abstraction . Though they sound similar, they serve different purposes and are applied in different design scenarios. ๐Ÿ”น What is an Interface? An interface defines a contract: what a class must do, without specifying how. It contains method signatures , properties , events , or indexers , but no implementation (except default interface methods in C# 8+). A class can implement multiple interfaces. ✅ Example: public interface IAnimal { void Speak(); } ๐Ÿ”ธ What is an Abstract Class? An abstract class is a partially implemented class. It can have both abstract members (without implementation) and concrete members (with implementation). Cannot be instantiated directly. Supports only single inheritance . ✅ Example: public abstract class Animal { publi...

Difference Between Clustered and Non-Clustered Indexes in SQL Server

Image
Difference Between Clustered and Non-Clustered Indexes in SQL Server Indexes in SQL Server are used to  improve the speed  of data retrieval. There are two main types: Clustered Index Non-Clustered Index Understanding the differences between them helps in designing efficient database schemas and optimizing query performance .   Clustered Index A  clustered index  determines the  physical order  of data in the table. A table can have  only one  clustered index. The table  data is sorted and stored  in the order of the clustered index key . Often created on the  primary key  column by default. Example: CREATE CLUSTERED INDEX IX_Employees_Id ON Employees(Id) Analogy:  Think of a phone book arranged alphabetically by last name — the data itself is in sorted order.   Non-Clustered Index A  non-clustered index  creates a  separate structure ...

Difference Between WHERE and HAVING in SQL Server

Image
Difference Between WHERE and HAVING in SQL Server In SQL Server, both WHERE and HAVING clauses are used to filter records in a query. However, they serve different purposes and are applied at different stages of the query processing.   WHERE Clause Used to filter rows before any grouping or aggregation . Applies to individual rows in the table. Cannot be used with aggregate functions like SUM(), COUNT(), etc. Example: SELECT * FROM Employees WHERE Department = 'HR' This query returns only those rows where the Department is "HR".   HAVING Clause Used to filter groups after aggregation has been performed (usually with GROUP BY ). Applies to the result of the GROUP BY clause. Can be used with aggregate functions like COUNT(), SUM(), AVG(). Example: SELECT Department, COUNT(*) AS EmployeeCount FROM Employees GROUP BY Department HAVING COUNT(*) > 10 This query shows only those d...

Step-by-Step: How to Enable Session in .NET Core

Image
๐Ÿ“– Overview Sessions in ASP.NET Core allow you to persist user-specific data across multiple HTTP requests. Whether you're tracking login state, temporary selections, or user preferences, sessions offer a simple and effective way to store short-term data on the server side. This guide is ideal for: Developers new to ASP.NET Core sessions Anyone migrating from classic ASP.NET .NET 6+ users following minimal hosting model ๐Ÿ“ฆ 1. Add the Required NuGet Package If you're not using the default SDK ( Microsoft.AspNetCore.App ), manually install the session package: dotnet add package Microsoft.AspNetCore.Session ๐Ÿ› ️ 2. Configure Services in Program.cs Register memory cache and session services in the DI container: var builder = WebApplication.CreateBuilder(args); // In-memory cache for session storage builder.Services.AddDistributedMemoryCache(); // Register session services builder.Services.AddSession(options => { o...