30 April 2024

Eager Loading, Lazy Loading

Eager Loading, Lazy Loading

Eager Loading helps you to load all your needed entities at once; i.e., all your child entities will be loaded at single database call. This can be achieved, using the Include method, which returs the related entities as a part of the query and a large amount of data is loaded at once.

User usr = dbContext.Users.Include(=> a.UserDetails).FirstOrDefault(=> a.UserId == userId);

Lazy Loading
It is the default behavior of an Entity Framework, where a child entity is loaded only when it is accessed for the first time. It simply delays the loading of the related data, until you ask for it.

User usr = dbContext.Users.FirstOrDefault(=> a.UserId == userId);

Kestrel Server in Dotnet

 Kestrel Server in Dotnet

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the recommended server for ASP.NET Core, and it's configured by default in ASP.NET Core project templates.  

ASP.NET Core project templates use Kestrel by default when not hosted with IIS. In the following template-generated Program.cs, the WebApplication.CreateBuilder method calls UseKestrel internally:

var builder = WebApplication.CreateBuilder(args);

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

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...