30 April 2024

When to use which Service, Singleton, Scoped and Transient

 

When to use which Service, Singleton, Scoped and Transient

Singleton approach => We can use this for logging service, feature flag (to on and off module while deployment), and email service,

Scoped approach => This is a better option when you want to maintain a state within a request,

Transient approach =>  Use this approach for the lightweight service with little or no state.

Scoped Approach in detail
The same instance lives for the entire scope of that request, for example; let's suppose one controller has two parameters, both are the objects of the same "Sample" class, then both the objects will share the same instance across the request,

Partitioning in SQL Server(Performance Improvement)

 Partitioning in SQL Server(Performance Improvement)

Used to divide large tables or indexes into smaller , more manageable pieces, yet treat them as single entity.

Types
  1. Partition Function
  2. Partition Scheme
  3. Table and Index partitioning

Await example in C#

Await example in C# 

using System;
using System.Threading.Tasks;

class Program {
 private static string result="test123";
 
 static void Main() {
   SaySomething();
   Console.WriteLine(result);
 }
 
 static async Task<string> SaySomething() {
   await Task.Delay(5);
   Console.WriteLine("test");
   result = "Hello world!";
    Console.WriteLine(result);
   return “Something”;
 }
}

Output

test123
test
Hello world!

Middleware vs Filters

 Middleware vs Filters

Use Middleware When
      You need to handle every request or response in a consistent manner accross the application 

Use Filters When
      Suppose if you want to implement certain logic to particular API/Controller

C# Web API Flow

 C# Web API Flow

  1. Receive Request
  2. Middleware Pipeline(Authentication, authorization, Logging, Error handling, Routing)
  3. Routing
  4. Controller Activation
  5. Model Binding
  6. Action Filters
  7. Execute Actions
  8. Result Execution
  9. Response Sent
  10. Logging and Cleanup

OAuth and C# Integration

 OAuth and C# Integration

Authenticate users with Microsoft Accounts
      
Steps
  1. Register your application - Microsoft Identity Platform(Azure Active Directory)
  2. Install required Nuget packages.
    1. Microsoft.Identity.Client
  3. Configure OAuth Settings - including the ClientID, tenantId, Client Secret
  4. Implementing Authentication
  5. Handling Token acquistion - method AcquireTokenByAuthorization Code
  6. Refresh Token

Solid Principles - Project example

 Solid Principles - Project example


Single Responsibility Principle

your code should have only one job - Mail and SMS
Open/Closed Principle
We can override the class functionality, but not change the existing class. Abstract -> Overide. Example - Version Maintain

Liskov/Substitution Principle

Derived class must be substitutable for its base class.
Abstract Class -> Interface -> No Override method

Use Abstract class in interface and give more explanation

Interface Segregation Principle
clients should not be forced to implement interfaces they don't use. Instead of one fat interface, many small interfaces are preferred.
     

Dependency Inversion Principle

high-level modules/classes should not depend on low-level modules/classes. should depend upon abstractions.

Use dependency injection for injecting the dependency

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...