Giter Club home page Giter Club logo

.nettipsbywaseem's Introduction

Table of Content

Episode 1 : What is .AsNoTracking() and its benefits

Episode 2 : SingleAsync and FirstAsync Methods of LINQ in .NET

Episode 3 : Basic overview of Monolithic and Microservices applications

Episode 4 : Difference b/w Boxing and Unboxing in C#

Episode 5 : Benefit of using AsReadOnly Method of List in .NET

Episode 6 : Difference b/w Any and All Method for Collection in .NET

Episode 7 : Lazy Loading vs Eager Loading in EntityFramework

Episode 8 : Aggregate function over List by Default Provided

Episode 9 : Difference b/w Include and ThenInclude in Entity Framework

Episode 10 : Use ToQueryString() Extension method while debugging

Episode 11 : How to avoid DbContext threading issues in Entity Framework

Episode 12 : How to register Open Generics in .NET Core Dependency Injection

Episode 13 : What are CORS and how to enable them in .NET at API Level

Episode 14 : Common Middlewares in .NET API

Episode 15 : Response Compression in .NET Core and how to configure its middleware

Episode 16 : Count() vs TryGetNonEnumeratedCount() and Which one is better ?

Episode 17 : Everything about Rate Limiting in .NET

Episode 18 : A basic visit to Response Caching along with its implementation in .NET

Episode 19 : Do you know how to initialize an Empty Enumerable in .NET ?

Episode 20 : Dependency Injection Explained in .NET

Episode 21 : IEnumerable vs IQueryable in .NET

Episode 22 : Why is it generally considered good practice to keep Dependency Injections in seperate class !

Episode 23 : Difference b/w GetType() and typeOf() Methods in .NET

Episode 24 : Difference b/w VAR and DYNAMIC keyword in C#

Episode 25 : StringBuilder vs string in C#


Episode 1 : What is .AsNoTracking() and its benefits

๐–๐ก๐ข๐ฅ๐ž ๐ฎ๐ฌ๐ข๐ง๐  ๐€๐ฌ๐๐จ๐“๐ซ๐š๐œ๐ค๐ข๐ง๐  1. The entity is not tracked by the context. 2. EF does not know the state of its entity. 3 Not recommended when you are performing Add/Update/Delete kind operations 4. Improved performance over regular LINQ queries. 5. Only recommended when you are doing read-only operations. 6. Most efficient when we have to retrieve large set of data.

๐–๐ข๐ญ๐ก๐จ๐ฎ๐ญ ๐€๐ฌ๐๐จ๐“๐ซ๐š๐œ๐ค๐ข๐ง๐  1. The entity is tracked by the context. 2. EF knows the state of this entity. 3. We can use this entity to save/update and we don't need to set the state of entity again.

1


Episode 2 : SingleAsync and FirstAsync Methods of LINQ in .NET

  1. In .NET, SingleAsync and FirstAsync are methods that can be used to retrieve a single element from a collection of elements.
  2. Both methods are similar in that they return the first element in a collection that satisfies a specified condition.
  3. The main difference between SingleAsync and FirstAsync is that SingleAsync will throw an exception if there is more than one element in the collection that satisfies the specified condition, while FirstAsync will return the first element it finds and then stop.
  4. SingleAsync can be used to ensure that there is exactly one element in the collection that satisfies the condition, while FirstAsync can be used to simply retrieve the first element that satisfies the condition, regardless of how many elements in the collection satisfy the condition.

2


Episode 3 : Basic overview of Monolithic and Microservices applications

Lets see the difference b/w monolithic and microservices

๐Œ๐จ๐ง๐จ๐ฅ๐ข๐ญ๐ก๐ข๐œ:

  1. A monolithic application is a single, self-contained program that consists of a single codebase.
  2. It is typically built and deployed as a single unit
  3. It tends to be more tightly coupled
  4. It can be more brittle, since a failure in one part of the application can affect the entire system

๐Œ๐ข๐œ๐ซ๐จ๐ฌ๐ž๐ซ๐ฏ๐ข๐œ๐ž๐ฌ:

  1. A microservices application is a collection of small, independent services that communicate with each other to form a complete application.
  2. It is built as a set of independent services that are deployed and managed separately
  3. It is flexible and scalable as individual services can be updated and deployed without affecting the entire application.
  4. It tends to be loosely coupled.
  5. These applications can be more complex to build and manage, since they require coordination among multiple services.
  6. They can also be more expensive to run, since each service needs to be individually managed and scaled.

3


Episode 4 : Difference b/w Boxing and Unboxing in C#

๐๐จ๐ฑ๐ข๐ง๐  and ๐”๐ง๐›๐จ๐ฑ๐ข๐ง๐  are used to convert value types to reference types and vice versa. When value type is moved to a reference type itโ€™s called as ๐๐จ๐ฑ๐ข๐ง๐  . The vice-versa is termed as ๐”๐ง๐›๐จ๐ฑ๐ข๐ง๐ .

4


Episode 5 : Benefit of using AsReadOnly Method of List in .NET

Here are some benefits of ๐€๐ฌ๐‘๐ž๐š๐๐Ž๐ง๐ฅ๐ฒ Method of ๐‹๐ข๐ฌ๐ญ<๐“>

  1. It gives you read only view of your collection.
  2. It allows you to prevent the collection from being modified, either accidentally or intentionally
  3. It can improve the performance of your code in some cases because the read-only wrapper provides a more restricted view of the collection.
  4. This can be particularly beneficial when working with large collections, where even small performance improvements can make a significant difference.

5


Episode 6 : Difference b/w Any and All Method for Collection in .NET

๐€๐ง๐ฒ ๐ฏ๐ฌ ๐€๐ฅ๐ฅ ๐„๐ฑ๐ญ๐ž๐ง๐ฌ๐ข๐จ๐ง ๐Œ๐ž๐ญ๐ก๐จ๐ ๐จ๐Ÿ ๐‹๐ข๐ฌ๐ญ<๐“>

1.The ๐€๐ฅ๐ฅ method checks if all elements of a list satisfy a specified condition.

2.The ๐€๐ง๐ฒ method checks if any elements of a list satisfy a specified condition.

6


Episode 7 : Lazy Loading vs Eager Loading in EntityFramework

๐‹๐š๐ณ๐ฒ ๐‹๐จ๐š๐๐ข๐ง๐  (๐‹๐‹)

  1. Lazy Loading is a process where EF loads the related entities on demand.

  2. It is the default behavior of EF

  3. It delays the loading of related entities until you specifically request it.

  4. You can go with it when you are sure that you are not using the related entities Instantly.

  5. The number of round trips to the database is more as for each master entity data, it will issue a separate SQL query to get the child-related entity data.

  6. It simply uses the SELECT Statement without any join.

  7. If you are not interested in related entities or the related entities are not used instantly, then you can use it.

๐„๐š๐ ๐ž๐ซ ๐‹๐จ๐š๐๐ข๐ง๐  (๐„๐‹)

  1. Eager loading is a Process where EF loads the related entities along with the main entity

  2. EF will not execute separate SQL queries for loading the related entities.

  3. All the entities are loaded from the database with a single query saving bandwidth and server CPU time.

  4. You can go with EL when you are sure that you will be using the related entities with the main entity everywhere

  5. It is a good practice to reduce the number of SQL queries to be sent to the database server to fetch the related entities

  6. It will use SQL Joining to join the related tables with the main table and then return the Main entity data along with the related entities.

  7. If you are interested in related entities used instantly in your application, then you need to go with it.

๐๐จ๐ญ๐ž : If you want to check the ๐’๐๐‹ ๐†๐ž๐ง๐ž๐ซ๐š๐ญ๐ž๐ ๐๐ฎ๐ž๐ซ๐ฒ when a LINQ query executes then you can check it by clicking on ๐“๐จ๐จ๐ฅ๐ฌ -> ๐’๐๐‹ ๐’๐ž๐ซ๐ฏ๐ž๐ซ ๐๐ซ๐จ๐Ÿ๐ข๐ฅ๐ž๐ซ in SQL Server Management Studio.

7

Episode 8 : Aggregate function over List by Default Provided

You would be familiar with aggregate functions from SQL, letโ€™s see how to use Entity Framework Queryable Extension Methods for aggregate functions over the List

  1. Sum
  2. Average
  3. Minimum
  4. Maximum
  5. Count

.NET 6.0 has 12 overloads of these all methods for all numeric data types used in code โฌ

8


Episode 9 : Difference b/w Include and ThenInclude in Entity Framework

Include and ThenInclude are two methods in EF that can be useful for improving the performance of a query by reducing the number of databases round trips.

Include is used for eager loading thatโ€™s why related entities come in a single query and database round trips are reduced.

Main difference b/w them is level, include is used for ๐’๐ข๐ง๐ ๐ฅ๐ž ๐‹๐ž๐ฏ๐ž๐ฅ travelling along entities and ThenInclude is helpful in ๐Œ๐ฎ๐ฅ๐ญ๐ข๐ฅ๐ž๐ฏ๐ž๐ฅ ๐‘๐ž๐ญ๐ซ๐ข๐ž๐ฏ๐š๐ฅ.

9


Episode 10 : Use ToQueryString() Extension method while debugging

ToQueryString is a custom extension method that converts IQueryable to SQL Query at the back-end side, especially helpful for debugging. โฌ

10


Episode 11 : How to avoid DbContext threading issues in Entity Framework

When EF Core detects an attempt to use a DbContext instance concurrently, you'll see an InvalidOperationException with a message like this:

"A second operation started in this context before a previous operation was completed."

There are few ways that can help you to avoid threading issues in EF.

  1. Use a separate DbContext instance for each thread. This ensures that each thread has its own DbContext instance, which means that there is no shared state between threads and no potential for threading conflicts.

๐๐ซ๐จ๐›๐ฅ๐ž๐ฆ: Costly in terms of memory usage

  1. Use a thread-safe DbContext wrapper. In this approach, you would create a wrapper class for DbContext that uses synchronization techniques, such as the lock keyword to ensure that only one thread can access the DbContext instance at a time.

๐๐ซ๐จ๐›๐ฅ๐ž๐ฆ: It can impact performance because threads may have to wait for access to the DbContext instance.

  1. Use ๐š๐ฌ๐ฒ๐ง๐œ๐ก๐ซ๐จ๐ง๐จ๐ฎ๐ฌ methods that allows you to write code that can run concurrently on multiple threads. It can help improve the performance of your application by allowing multiple operations to be executed concurrently. We can use ๐š๐ฐ๐š๐ข๐ญ and ๐š๐ฌ๐ฒ๐ง๐œ to perform asynchronous operation.

I have so far used ๐š๐ฌ๐ฒ๐ง๐œ๐ก๐ซ๐จ๐ง๐จ๐ฎ๐ฌ methods, which one do you practice frequently?

11


Episode 12 : How to register Open Generics in .NET Core Dependency Injection

If you have a generic interface and its generic implementation like we mostly do when we make a generic repository for CRUD operations and you want to register its dependency injection at startup, then there is a simple way of registering the DI.

  1. For .NET 3.1 register in ConfigureServices in ๐’๐ญ๐ซ๐š๐ญ๐ฎ๐ฉ.๐œ๐ฌ

  2. For latest versions of .NET(6.0 ,7.0) register in ๐๐ซ๐จ๐ ๐ซ๐š๐ฆ.๐œ๐ฌ

12


Episode 13 : What are CORS and how to enable them in .NET at API Level

CORS stands for Cross Origin Resource sharing, so what exactly is cross origin.

These two URLs have the same origin: ๐—๐—๐—๐—‰๐—Œ://๐—†๐—’-๐—Œ๐—‚๐—๐–พ-๐—‡๐—ˆ-๐Ÿฃ.๐–ผ๐—ˆ๐—†/๐–ฆ๐–พ๐—/๐–ง๐–บ๐—„๐—Ž๐—‡๐–บ๐–ฌ๐–บ๐—๐–บ๐—๐–บ ๐—๐—๐—๐—‰๐—Œ://๐—†๐—’-๐—Œ๐—‚๐—๐–พ-๐—‡๐—ˆ-๐Ÿฃ.๐–ผ๐—ˆ๐—†/๐–ฆ๐–พ๐—/๐– ๐—…๐—…๐–จ๐—Œ๐–ถ๐–พ๐—…๐—…

These URLs have different origins ๐—๐—๐—๐—‰๐—Œ://๐—†๐—’-๐—Œ๐—‚๐—๐–พ-๐—‡๐—ˆ-๐Ÿฃ.๐–ผ๐—ˆ๐—†/๐–ฆ๐–พ๐—/๐–ง๐–บ๐—„๐—Ž๐—‡๐–บ๐–ฌ๐–บ๐—๐–บ๐—๐–บ ๐—๐—๐—๐—‰://๐—†๐—’-๐—Œ๐—‚๐—๐–พ-๐—‡๐—ˆ-๐Ÿฃ.๐—‡๐–พ๐—/๐–ฆ๐–พ๐—/๐– ๐—…๐—…๐–จ๐—Œ๐–ถ๐–พ๐—…๐—…

To facilitate requests from different origins you need to enable CORS in .NET.

In .NET 6 by using the combination of these methods you can enable CORS as per your requirement.

๐€๐ฅ๐ฅ๐จ๐ฐ๐€๐ง๐ฒ๐Ž๐ซ๐ข๐ ๐ข๐ง: This policy allows requests from any origin.

๐–๐ข๐ญ๐ก๐Ž๐ซ๐ข๐ ๐ข๐ง๐ฌ: This policy allows requests from specific origins. You can specify one or more origins as arguments to this method.

๐€๐ฅ๐ฅ๐จ๐ฐ๐€๐ง๐ฒ๐‡๐ž๐š๐๐ž๐ซ: This policy allows requests with any header.

๐–๐ข๐ญ๐ก๐‡๐ž๐š๐๐ž๐ซ๐ฌ: This policy allows requests with specific headers. You can specify one or more headers as arguments to this method.

๐€๐ฅ๐ฅ๐จ๐ฐ๐€๐ง๐ฒ๐Œ๐ž๐ญ๐ก๐จ๐: This policy allows requests with any HTTP method (e.g., GET, POST, PUT, DELETE).

๐–๐ข๐ญ๐ก๐Œ๐ž๐ญ๐ก๐จ๐๐ฌ: This policy allows requests with specific HTTP methods. You can specify one or more methods as arguments to this method.

Few Things to Keep in mind

โœ”๏ธCORS is not a security feature. CORS is a W3C standard that allows a server to relax the same-origin policy.

โœ”๏ธAn API isn't safer by allowing CORS.

โœ”๏ธIt's a way for a server to allow browsers to execute a cross-origin request that otherwise would be forbidden.

โœ”๏ธBrowsers without CORS can't do cross-origin requests.

CORS


Episode 14 : Common Middlewares in .NET API

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:

Chooses whether to pass the request to the next component in the pipeline.

Can perform work before and after the next component in the pipeline.

Here are some common types of middleware that might be used in a .NET API program:

  1. Routing
  2. Exception handling
  3. Authentication and authorization
  4. CORS (Cross-Origin Resource Sharing)
  5. Response compression
  6. Request validation
  7. Response caching 8.Static file serving

๐‘๐จ๐ฎ๐ญ๐ข๐ง๐  ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for determining which endpoint should handle a particular request based on the request's path and method.

๐„๐ฑ๐œ๐ž๐ฉ๐ญ๐ข๐จ๐ง ๐ก๐š๐ง๐๐ฅ๐ข๐ง๐  ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for catching and handling exceptions that occur during the processing of a request.

๐€๐ฎ๐ญ๐ก๐ž๐ง๐ญ๐ข๐œ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐š๐ฎ๐ญ๐ก๐จ๐ซ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for verifying that a request is from an authenticated and authorized user.

๐‚๐Ž๐‘๐’ (๐‚๐ซ๐จ๐ฌ๐ฌ-๐Ž๐ซ๐ข๐ ๐ข๐ง ๐‘๐ž๐ฌ๐จ๐ฎ๐ซ๐œ๐ž ๐’๐ก๐š๐ซ๐ข๐ง๐ ) ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for adding the necessary headers to allow a browser to make cross-origin requests to the API.

๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐œ๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for compressing the response payload in order to reduce the size of the response and improve performance.

๐‘๐ž๐ช๐ฎ๐ž๐ฌ๐ญ ๐ฏ๐š๐ฅ๐ข๐๐š๐ญ๐ข๐จ๐ง ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for validating incoming requests to ensure that they conform to the expected format.

๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐œ๐š๐œ๐ก๐ข๐ง๐  ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for caching responses in order to reduce the load on the server and improve performance.

๐’๐ญ๐š๐ญ๐ข๐œ ๐Ÿ๐ข๐ฅ๐ž ๐ฌ๐ž๐ซ๐ฏ๐ข๐ง๐  ๐ฆ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž: This middleware is responsible for serving static files, such as HTML, CSS, and JavaScript files, from the file system.

Itโ€™s important to note that the order in which middleware is added to the pipeline can be important, as the middleware will be executed in the order in which it is added. For example, if the authentication middleware is added before the routing middleware, the routing middleware will not be executed until the authentication middleware has completed.

14


Episode 15 : Response Compression in .NET Core and how to configure its middleware

Network bandwidth is a limited resource. Reducing the size of the response usually increases the responsiveness of an app, often dramatically. One way to reduce payload sizes is to compress an app's responses.

๐–๐ก๐š๐ญ ๐ข๐ฌ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง Response compression is a technique that can be used to reduce the size of HTTP responses, which can improve the performance of a web application by reducing the amount of data that needs to be transmitted over the network.

๐๐ž๐ง๐ž๐Ÿ๐ข๐ญ๐ฌ ๐จ๐Ÿ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง

1.Improved performance: Compressing the response can reduce the amount of data that needs to be transmitted over the network, which can lead to faster page load times and a better user experience.

2.Reduced bandwidth usage: By compressing the response, you can reduce the amount of data that is transmitted over the network, which can lead to reduced bandwidth usage and lower costs for hosting and bandwidth.

3.Better SEO: Search engines take page load times into account when ranking websites, so a faster loading website may rank higher in search results.

๐‡๐จ๐ฐ ๐ญ๐จ ๐œ๐จ๐ง๐Ÿ๐ข๐ ๐ฎ๐ซ๐ž ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐Œ๐ข๐๐๐ฅ๐ž๐ฐ๐š๐ซ๐ž ๐ข๐ง .๐๐„๐“ We can configure it using the . NET middleware, AddResponseCompression.

.NET also Provides built in providers for compression we can configure their options as per our need.

1.BrotliCompressionProvider Using it a text file response at 2,044 bytes was compressed to ~979 bytes.

  1. GzipCompressionProvider Using it a Scalable Vector Graphics (SVG) image response at 9,707 bytes was compresses to ~4,459 bytes

๐”๐‘๐‹: https://bit.ly/3G3rsIj

๐‚๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง ๐‹๐ž๐ฏ๐ž๐ฅ๐ฌ ๐Ž๐ฉ๐ญ๐ข๐ฆ๐š๐ฅ - The compression operation should be optimally compressed, even if the operation, it takes a longer time to complete.

๐…๐š๐ฌ๐ญ๐ž๐ฌ๐ญ - The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.

๐๐จ ๐‚๐จ๐ฆ๐ฉ๐ซ๐ž๐ฌ๐ฌ๐ข๐จ๐ง - No compression should be performed on the file.

๐’๐ฆ๐š๐ฅ๐ฅ๐ž๐ฌ๐ญ ๐’๐ข๐ณ๐ž - The compression operation should create output as small as possible, even if the operation takes a longer time to complete.

๐Œ๐ˆ๐Œ๐„ ๐“๐ฒ๐ฉ๐ž๐ฌ ๐›๐ฒ ๐ƒ๐ž๐Ÿ๐š๐ฎ๐ฅ๐ญ ๐’๐ฎ๐ฉ๐ฉ๐จ๐ซ๐ญ๐ž๐ ๐๐ฒ .๐๐„๐“ ๐๐ซ๐จ๐ฏ๐ข๐๐ž๐ซ๐ฌ

  1. text/plain
  2. text/css
  3. application/javascript
  4. text/html
  5. application/xml
  6. text/xml
  7. application/json
  8. text/json
  9. application/wasm

15


Episode 16 : Count() vs TryGetNonEnumeratedCount() and Which one is better ?

๐‚๐จ๐ฎ๐ง๐ญ()

This method will make an enumeration to calculate the count of elements.

Available in all versions of .NET

๐“๐ซ๐ฒ๐†๐ž๐ญ๐๐จ๐ง๐„๐ง๐ฎ๐ฆ๐ž๐ซ๐š๐ญ๐ž๐๐‚๐จ๐ฎ๐ง๐ญ()

This attempts to determine the number of elements in a sequence without forcing an enumeration.

It is available in .NET 6 and .NET 7

It is available only on the ICollection interface.

It is typically a constant-time operation, but ultimately this depends on the complexity characteristics of the underlying collection's implementation.

16


Episode 17 : Everything about Rate Limiting in .NET

Rate limiting is a technique used to control the amount of incoming and outgoing traffic to a network or service. It is often used to protect servers and other resources from being overwhelmed by too many requests, or to prevent abuses such as distributed denial of service (DDoS) attacks.

๐‡๐จ๐ฐ ๐ข๐ญ ๐ฐ๐จ๐ซ๐ค๐ฌ? Rate limiting works by setting a limit on the number of requests that a client can make to a server within a specified time period. If the client exceeds the rate limit, the server will return an error, typically an HTTP status code 429 (Too Many Requests), to the client.

๐๐ž๐ง๐ž๐Ÿ๐ข๐ญ๐ฌ ๐จ๐Ÿ ๐‘๐š๐ญ๐ž ๐‹๐ข๐ฆ๐ข๐ญ๐ข๐ง๐ ?

  1. Protecting against denial-of-service attacks of specific types.
  2. Maintaining service availability.
  3. Reducing resource consumption.
  4. Detecting & blocking maliciousbehavior.
  5. Improving user experience.

๐‚๐จ๐ง๐ฌ ๐จ๐Ÿ ๐‘๐š๐ญ๐ž ๐‹๐ข๐ฆ๐ข๐ญ๐ข๐ง๐ ? Rate limiting cannot distinguish between good and bad traffic, it will just look into IP and number of requests, so in some cases by changing the IP address attack is still possible.

๐€๐ญ ๐ฐ๐ก๐ข๐œ๐ก ๐ฉ๐จ๐ข๐ง๐ญ ๐ฐ๐ž ๐œ๐š๐ง ๐š๐ฉ๐ฉ๐ฅ๐ฒ ๐‘๐š๐ญ๐ž ๐‹๐ข๐ฆ๐ข๐ญ๐ข๐ง๐ ?

  1. Network Edge
  2. Application Layer
  3. Database Layer
  4. Service Level

๐‡๐จ๐ฐ ๐œ๐š๐ง ๐ˆ ๐ฏ๐ž๐ซ๐ข๐Ÿ๐ฒ ๐ญ๐ก๐š๐ญ ๐‘๐š๐ญ๐ž ๐‹๐ข๐ฆ๐ข๐ญ๐ข๐ง๐  ๐ก๐š๐ฌ ๐›๐ž๐ž๐ง ๐š๐๐๐ž๐? You can check from response headers of your request there would be complete information that is your remaining limit, what is limit time etc. if your Rate Limiting has successfully configured.

๐‡๐จ๐ฐ ๐ญ๐จ ๐š๐๐ ๐‘๐š๐ญ๐ž ๐‹๐ข๐ฆ๐ข๐ญ๐ข๐ง๐  ๐ข๐ง .๐๐„๐“ ๐€๐ฉ๐ฉ๐ฅ๐ข๐œ๐š๐ญ๐ข๐จ๐ง? We can apply rate limiting on Application Layer in our project using Asp Net Core Rate Limit NuGet Packageโฌ

17


Episode 18 : A basic visit to Response Caching along with its implementation in .NET

Response caching is a technique for storing the responses of an API or web application in a cache so that they can be served faster to subsequent requests. The responses are stored in a cache with a key that uniquely identifies them, and the cache has a limited size and a policy for removing items when it becomes full.

๐๐ž๐ง๐ž๐Ÿ๐ข๐ญ๐ฌ ๐จ๐Ÿ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐š๐œ๐ก๐ข๐ง๐ ?

  1. Improved performance by reducing the load on the server.

  2. Reduced server load, as it can serve the cached response instead of generating a new one.

  3. Reduced bandwidth usages it reduces the amount of data that needs to be transferred between the server and the client.

  4. Improved security as it can reduce the number of requests that reach the server, reducing the risk of certain types of attacks.

๐Ž๐ง ๐ฐ๐ก๐ข๐œ๐ก ๐ซ๐ž๐ช๐ฎ๐ž๐ฌ๐ญ๐ฌ ๐ฐ๐ž ๐œ๐š๐ง ๐š๐ฉ๐ฉ๐ฅ๐ฒ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐š๐œ๐ก๐ข๐ง๐ 

  1. Get
  2. Head

๐…๐ž๐ฐ ๐œ๐จ๐ง๐ฌ๐ญ๐ซ๐š๐ข๐ง๐ญ๐ฌ ๐Ÿ๐จ๐ซ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐š๐œ๐ก๐ข๐ง๐  1.The request must result in a server response with a 200 (OK) status code.

2.Response Caching Middleware must be placed before middleware that require caching. For more information, see ASP.NET Core Middleware.

3.The Authorization header must not be present.

  1. Cache-Control header parameters must be valid, and the response must be marked public and not marked private.

  2. The Content-Length header value (if set) must match the size of the response body.

๐’๐จ๐ฆ๐ž ๐ซ๐ž๐š๐ฅ-๐ฐ๐จ๐ซ๐ฅ๐ ๐ž๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž๐ฌ ๐จ๐Ÿ ๐‘๐ž๐ฌ๐ฉ๐จ๐ง๐ฌ๐ž ๐‚๐š๐œ๐ก๐ข๐ง๐ 

  1. News website
  2. E-commerce websites In your application you can apply response caching to those requests whose response is changed after a time and you are sure about it.

๐‡๐จ๐ฐ ๐œ๐š๐ง ๐ˆ ๐ฏ๐ž๐ซ๐ข๐Ÿ๐ฒ ๐ข๐ญ? Create an application apply caching and then request it from Postman and set time to 60 minutes, you will notice that only first request will reach the controller, after that even if you try request will not reach controller.

18


Episode 19 : Do you know how to initialize an Empty Enumerable in .NET ?

โœ”๏ธ .NET provides us the Empty() method to initialize an empty Enumerable of any type.

โœ”๏ธThis method is useful for passing an empty sequence to a user defined method that takes an IEnumerable โคต๏ธ

19


Episode 20 : Dependency Injection Explained in .NET

This post crossed 100K views on my LinkedIn

100K

Dependency injection involves providing a class with its required dependencies from an external source rather than having the class create them itself.

This helps to decouple the object creation process from the caller, leading to a more modular and flexible system.

In other words, it allows a class to focus on its core functionality rather than worrying about how to create and manage its dependencies.

๐–๐ก๐ฒ ๐๐จ ๐ฐ๐ž ๐ง๐ž๐ž๐ ๐๐ž๐ฉ๐ž๐ง๐๐ž๐ง๐œ๐ฒ ๐ข๐ง๐ฃ๐ž๐œ๐ญ๐ข๐จ๐ง? By using DI classes are decoupled from each other so you make changes at one place and it is reflected all over the places.

๐‡๐จ๐ฐ ๐ญ๐จ ๐ข๐ฆ๐ฉ๐ฅ๐ž๐ฆ๐ž๐ง๐ญ ๐๐ž๐ฉ๐ž๐ง๐๐ž๐ง๐œ๐ฒ ๐ข๐ง๐ฃ๐ž๐œ๐ญ๐ข๐จ๐ง?

โœ… In .NET 6 we implement DI in Program.cs class by using builder.Services

โœ… For previous versions of .NET, to implement DI we need to add the service in โ€œConfigureServicesโ€ method which is in Startup.cs file

๐ƒ๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐ญ ๐ฐ๐š๐ฒ๐ฌ ๐จ๐Ÿ ๐ข๐ฆ๐ฉ๐ฅ๐ž๐ฆ๐ž๐ง๐ญ๐ข๐ง๐  ๐ƒ๐ž๐ฉ๐ž๐ง๐๐ž๐ง๐œ๐ฒ ๐ˆ๐ง๐ฃ๐ž๐œ๐ญ๐ข๐จ๐ง There are three ways of doing DI:

1.Scoped โžก๏ธ It will create an instance per scope, if we are in same scope same instance would be used. Whenever we go out of scope new instance would be created.

  1. Transient โžก๏ธ It creates new instances every time its injected.

  2. Singleton โžก๏ธ It instantiates one global object for all requests coming to the server from any user.

๐๐ž๐ง๐ž๐Ÿ๐ข๐ญ๐ฌ ๐จ๐Ÿ ๐ƒ๐ž๐ฉ๐ž๐ง๐๐ž๐ง๐œ๐ฒ ๐ˆ๐ง๐ฃ๐ž๐œ๐ญ๐ข๐จ๐ง

1.Improved testability: Dependency injection makes it easier to write unit tests for your code, as you can easily substitute mock objects for the real dependencies.

  1. Enhanced flexibility: By injecting dependencies from the outside, you can easily change the implementation of a class's dependencies without having to modify the class itself. This makes it easier to adapt your application to changing requirements.

  2. Increased modularity: Dependency injection encourages the use of small, single-purpose classes that are easy to test and reuse in different contexts. This can lead to a more modular and maintainable codebase.

  3. Better separation of concerns: Dependency injection helps to separate the concerns of different parts of your application, making it easier to understand and maintain the code.

  4. Enhanced decoupling: Dependency injection promotes loose coupling between classes, which can make your application more resilient to change and easier to test.

20


Episode 21 : IEnumerable vs IQueryable in .NET

IEnumerable and IQueryable interfaces are both used to work with collections of data and both support LINQ (Language Integrated Query).

๐ˆ๐๐ฎ๐ž๐ซ๐ฒ๐š๐›๐ฅ๐ž

โœ… IQueryable executes queries on the server side.

โœ… It is designed specifically to work with LINQ

โœ… It extends IEnumerable, which means it includes all of the functionality of IEnumerable.

โœ… It can be more efficient when working with large data

โœ… It can be more helpful when you have to apply a lot of filtrations, you can apply all filters on Queryable and when you are done you can convert data to desired collection

๐ˆ๐„๐ง๐ฎ๐ฆ๐ž๐ซ๐š๐›๐ฅ๐ž โœ”๏ธ IEnumerable executes queries on the client side.

โœ”๏ธ It is generally used to work with in-memory data collections.

Explanation with code โคต๏ธ

21


Episode 22 : Why is it generally considered good practice to keep Dependency Injections in seperate class !

It is good practice to move all of your dependency injection work in a separate class instead of filling your Program.cs with all Injections. Here are some benefits of this approach

  1. By keeping dependency injections in separate classes, you can better adhere to the principle of separation of concerns. So, your Program.cs is just focusing on configuration and its DI class headache to manage dependencies.

  2. Keeping dependency injections in separate classes can make it easier to maintain the application over time. For any change in DIโ€™s, you would not be changing the Program.cs rather you would just change the desired dependency injection class

  3. For better understanding we can create different DI classes that would be dealing with similar dependencies.

How you prefer to keep your dependencies? ๐Ÿ“โคต๏ธ

22


Episode 23 : Difference b/w GetType() and typeOf() Methods in .NET

Both TypeOf and GetType help you to get the type with a little difference

โœ… typeof gets the type from a class while GetType gets type from an object.

โœ… The GetType method is used to retrieve the type of an object at runtime, while the typeof operator is used to retrieve the type of an object at compile-time.

I have described one example where typeof can prove helpful , what other situations could be where GetType and typeof can save us ๐Ÿ“โฌ

23


Episode 24 : Difference b/w VAR and DYNAMIC keyword in C#

๐•๐€๐‘ โœ… VAR is early binded (statically checked)

โœ… It looks at your right-hand side data and then during compile time it decides the left-hand side data type

โœ… Use of var makes your code more readable, simplified and reduces the typing.

๐ƒ๐˜๐๐€๐Œ๐ˆ๐‚ โœ”๏ธ Dynamic is late binded (dynamically evaluated).

โœ”๏ธ It is used to work on dynamic objects.

โœ”๏ธ It helps us when we are not sure about the data type of objects, saves us from a lot of data type-oriented checking (because the compiler ignores them).

๐Ÿ’ญ There is a debate over var or proper type. I would like to hear your opinion, many people say that we should use var instead of proper type, but some say that if you are familiar with type then why would you go for var. ๐Ÿ“โฌ

24


Episode 25 : StringBuilder vs string in C#

Difference b/w String and StringBuilder

๐’๐ญ๐ซ๐ข๐ง๐ ๐๐ฎ๐ข๐ฅ๐๐ž๐ซ

  1. StringBuilder is mutable

  2. StringBuilder will only create one object on heap and every time it would be updated with new value even if you append/insert 1 million values.

๐’๐ญ๐ซ๐ข๐ง๐ 

  1. String is immutable.

  2. Every time when we update data in string it creates a new instance of object. So, if you update value 1K times it will create 1K new instances.

๐“๐ข๐ฆ๐ž ๐๐ข๐Ÿ๐Ÿ๐ž๐ซ๐ž๐ง๐œ๐ž ๐จ๐ฏ๐ž๐ซ ๐Ÿ๐ŸŽ,๐ŸŽ๐ŸŽ๐ŸŽ ๐ข๐ญ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง๐ฌ A good rule of thumb is to use strings when you aren't going to perform operations like(Append/Remove) repetitively, use StringBuilder for vice versa.

I took 10,000 iterations and checked the difference for that see the difference in picture.โฌ

25


.nettipsbywaseem's People

Contributors

mwaseemzakir avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.