Sunday, June 25, 2023

.NET Core - Understanding Scoped, Transient, and Singleton Lifetime

Scoped, Transient, and Singleton Lifetime

Scoped, Transient, and Singleton are three lifetime options available in .NET Core for registering and managing services within the dependency injection container. Understanding these options is crucial for building scalable and maintainable applications. Let's explore each of them:

  1. Transient Lifetime:

    A transient service is created each time it is requested from the dependency injection container. This means a new instance is created for every resolution. Transient services are suitable for lightweight and stateless components that don't require shared state. For instance, if you have a service that performs simple calculations or generates random numbers, using the transient lifetime is appropriate.

    To register a transient service in .NET Core, you can use the 'AddTransient' method during service registration:

    services.AddTransient<ITransientService, TransientService>();

Friday, June 23, 2023

Understanding the Use, Run, and Map Functions for Middleware in .NET Core

use,run and map in .net core

Introduction:

Middleware plays a vital role in handling and processing HTTP requests within a .NET Core application's request pipeline. It enables developers to customize and extend the application's behavior. In this post, we will delve into three crucial functions used for configuring middleware: Use, Run, and Map.



  1. Use:

    The Use function is extensively used when configuring middleware in .NET Core. It allows the addition of middleware components to the request pipeline. This function accepts a delegate or a middleware class as a parameter. The delegate or middleware class is responsible for processing an HTTP request and generating an appropriate response.

    Consider the following example that demonstrates the Use function in adding custom middleware:

    public void Configure(IApplicationBuilder app)
    {
        app.Use(async (context, next) =>
        {
            // Perform some logic before the request reaches the next middleware
            await next.Invoke();
            // Perform some logic after the request has been processed by subsequent middleware
        });
        // Add more middleware components using the Use function if necessary
    }
    

Wednesday, June 21, 2023

Middleware in .NET Core: A Developer's Guide

middleware .net core

Introduction:

Middleware plays a vital role in web development, and having a clear understanding of its concept and implementation is crucial for .NET Core developers. Middleware acts as a bridge between incoming requests and outgoing responses in an application, enabling developers to customize and extend the request-processing pipeline. In this article, we will explore the world of middleware in .NET Core, discussing its significance, usage, and providing practical examples.


Understanding Middleware:

In the context of .NET Core, middleware refers to a software component or a set of components that are executed sequentially to process HTTP requests and responses. It forms a chain of components that intercept requests, perform specific actions, and pass control to the next component in the pipeline. Middleware empowers developers to add, remove, or modify behavior at various stages of request processing without altering the core application code.

Middleware in .NET Core:

In .NET Core, the request pipeline is constructed using middleware components. It comprises a series of middleware components that receive an incoming HTTP request and pass it along until a response is generated. Each middleware component can inspect, modify, or terminate the request pipeline.

Middleware components in .NET Core are represented by classes that implement either the IMiddleware interface or the RequestDelegate delegate. The IMiddleware interface provides a convenient way to encapsulate middleware logic, while the RequestDelegate delegate offers finer control over middleware behavior.

Saturday, June 10, 2023

Angular CLI and Essential Commands for Efficient Development

Angular CLI and Essential Commands

Angular CLI (Command Line Interface) is a powerful tool that aids in the streamlining of Angular development by automating various tasks and providing a standardized project structure. In this post, we will explore the Angular CLI and highlight some of the most useful commands that can enhance your productivity as an Angular developer.

  1. Installation: To begin using Angular CLI, ensure that you have Node.js installed on your system. Once Node.js is installed, open your terminal or command prompt and execute the following command to install Angular CLI globally:
    npm install -g @angular/cli
    
    This command installs the CLI tool globally, making it accessible as a command-line tool.
  2. Creating a New Angular Project: Angular CLI simplifies project creation through a straightforward command. To create a new Angular project, navigate to your desired directory in the terminal and execute the following command:
    ng new my-angular-app
    
    This command generates a new Angular project named "my-angular-app" and sets up the necessary files and dependencies.

Monday, June 5, 2023

Angular Trackby to improve ngFor Performance

Angular Trackby to improve ngFor Performance

Do you utilize ngFor in Angular to iterate over arrays or collections? If that's the case, you might have come across performance issues or unexpected behavior when updating or deleting items. But fret not! Angular offers a practical solution known as trackBy that can greatly enhance the performance and stability of your ngFor loops. Let's delve into it and learn how to effectively use it.


The Challenge

By default, Angular employs the index of each item in an array to track changes in ngFor loops. This implies that if the array's order changes or items are added or removed, Angular will re-render the entire list. Such a scenario can lead to performance bottlenecks, particularly when dealing with large arrays or complex components within the loop.

The following example shows what happens when we refresh the entire list. The App displays the list of names. it has option to add a name, remove a name and refresh the entire name list.

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  names = new Array<string>();

  name: string = '';

  ngOnInit() {
    this.Refresh();
  }

  remove(i: number) {
    this.names.splice(i, 1);
  }

  addMovie() {
    this.names.push(this.name);
    this.name = '';
  }

  Refresh() {
    console.log('refresh');
    this.names = ['Manish', 'Rahul', 'Ashish', 'Aditya', 'Rohan'];
  }
}

HTML

<p>App Component</p>
<ul>
    <li *ngFor="let name of names; index as i">
        {{i}}. {{name}} <button (click)="remove(i)">remove</button>
    </li>
</ul>

<button (click)="Refresh()">Refresh>/<button> <br/>

Name : <input type="text" [(ngModel)]="name">
<button (click)="addMovie()">Add Name</button>
^ Scroll to Top