Asynchronous Programming in C# - Comparing it with multi-threading

async and await in c#

Asynchronous task is a non-blocking task. Main thread goes back to the thread pool (and free to do other tasks) when it reaches to await – and new thread is assigned when wait is complete.

It is different from the multi-threading. In multi-threding, task is divided between multiple threads. Cores of your CPU are utilized.

Analogy

Let’s say you have some chores like:

  1. Boiling eggs 🫕 (or sweet potatoes🍠🍠 if you dont eat eggs 🥚🥚)
  2. Clean 🧹🪣 the house

There are multiple ways to achieve this:

[Read More]

Inversion of Control - Dependency Injection - Dependency Inversion Principle

inversion of control, dependency injection and dependency inversion principle

  • Direct or traditional approach: Object creates its own dependencies.

  • Inverted approach (inversion of control): Dependencies are created outside the object. This responsibility is given to someone else.

Example 1 (Direct approach):

We have a service named EmailService which is an implementation of INotificationService.

public interface INotificationService
{
    void SendNotification();
}

public class EmailService : INotificationService
{
    public void SendNotification()
    {
        // code to send notification 
    }
}

OrderService need to use the INotificationService.

[Read More]

Node Js: Jwt Authentication and refresh token

jwt auth in node js

Please visit this post first, where I have made this project from scratch and wrote CRUD APIs.

Source code

JSON web token (JWT) and why it is needed

REST Apis are session less. You can not maintain the authentication session for the whole application. For that purpose, JSON web token (JWT) comes in play. How it works

[Read More]

Build a Node.js REST APIs with Express, Sequelize & SQL Server (2025 Guide)

rest apis with node js and sql server

In this tutorial, you’ll learn how to build a simple CRUD (Create, Read, Update, Delete) REST API using Node.js, Express.js, Sequelize, and SQL Server. I have tried to make it a less verbose. It is more like a technical documentation to create the rest apis in this tech stack. However, I have tried to explain all the essential things. This is ideal if you’re looking to integrate a SQL Server database with a Node.js backend using Sequelize as the ORM. We are using sql server in this tutorial but you can easily replace it with other sql databases.

[Read More]

Dotnet: All you need to know about middlewares

Middlewares in .net core

  • In dotnet, a Middleware is a piece of code that runs in the request pipeline.
  • Middlewares are put together in a sequence, and their order matters.
  • When a request is made, it goes through each of the middleware.
  • Response flow back in reverse order through the middleware.
  • Each middleware has capability to modify the request or short circuits the pipeline.
  • Each middleware can change the response before it’s sent to the client.

middleware pipeline in .net core

[Read More]

Jwt Authention and role base authorization in Dotnet Core

jwt-authentication-in-dotnet

Create a new web api project

Run these commands in a sequence to create a new project.

dotnet new sln -o JwtDotnet9

cd JwtDotnet9

dotnet sln add JwtDotnet9/JwtDotnet9.csproj

Open the project in vs code.

code .

=> Source Code

=> Securing The .NET 9 App: Signup, Login, JWT, Refresh Tokens, and Role Based Access with PostgreSQL

Install the required nuget packages

dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer

Jwt configuration in appsettings

Open appsettings.json and add these lines

[Read More]

C#: Byte and Its Use Cases

c# byte

  • byte is a value type in c#
  • byte is an unsigned integer which stores value from 0 to 255, which means it can not store negetive numbers.
  • Size of byte is 8-bit (1 byte)
  • CTS equivalent of byte is System.Byte
  • It’s default value is 0
  • It is great for memory optimization when dealing with small numbers.

Examples:

byte a = 10;

Console.WriteLine(a);

Use cases

  1. Working with ASCII value
 byte asciiValue = (byte)'R'; // 82
 byte asciiValue2 = (byte)'r'; // 114
  1. Encoding text into bytes
 using System.Text;

 string message = "Ravindra";
 byte[] encoded = Encoding.UTF8.GetBytes(message);

 Console.WriteLine($"{string.Join(",",encoded)}");

 // op: 82,97,118,105,110,100,114,97

Note: Each character is encoded using UTF-8. For standard letters, the byte values match their ASCII codes.. (R: 82, a:97, v:118 …)

[Read More]

IEnumerable Vs IQueryable In C#

IEnumerable-vs-IQuerable

There has been a discussion around town about the difference between an IEnumerable and an IQueryable, especially in c# interviews. I won’t be diving into the fact that IEnumerable is part of the System.Collections namespace and IQueryable belongs to System.Linq namespace (or did I???). Rather, I’ll focus on the practical usage of both—how they work, and when to use each.

IQueryable

 public IActionResult GetPeople()
 {
  // It will retrieve 2 records from database 
  IQueryable<Person> people = _context.People.Take(2); 
  //  Note: At the above line, no data will be retrieved from the database

  return Ok(people); // Data will be retrieved here
 }

Corresponding sql

Note that, I am using Sqlite, the above code is translated to this query:

[Read More]

Bulk insert in dapper with table valued parameter

How to insert bulk data in dapper?

There might be instances when you want to insert bulk data. For an instance, you want to create an order, where you need to add multiple items. Let’s see how can we insert bulk data in c# using dapper.

Note: It is only good for adding bunch of rows. But if you are looking for adding hundreds of rows then better to use other approaches. There are many, if you look out.

[Read More]

EF Core under the hood: Count() vs Any()

coun_vs_any_thumb

Let’s say you want to execute a code block when Book table is not empty. In Entity Framework Core, we can achieve this in two ways (there might be others but I am unaware of them):

Option 1:

 if(context.Books.Count()>0)
 {
     // do something
 }

Option 2:

 if (context.Books.Any())
 {
     // do something
 }

Note 📢: I am testing these queries against a table containing 1 million rows.

[Read More]