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#
๐๐ก๐ข๐ฅ๐ ๐ฎ๐ฌ๐ข๐ง๐ ๐๐ฌ๐๐จ๐๐ซ๐๐๐ค๐ข๐ง๐ 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.
- In .NET, SingleAsync and FirstAsync are methods that can be used to retrieve a single element from a collection of elements.
- Both methods are similar in that they return the first element in a collection that satisfies a specified condition.
- 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.
- 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.
Lets see the difference b/w monolithic and microservices
๐๐จ๐ง๐จ๐ฅ๐ข๐ญ๐ก๐ข๐:
- A monolithic application is a single, self-contained program that consists of a single codebase.
- It is typically built and deployed as a single unit
- It tends to be more tightly coupled
- It can be more brittle, since a failure in one part of the application can affect the entire system
๐๐ข๐๐ซ๐จ๐ฌ๐๐ซ๐ฏ๐ข๐๐๐ฌ:
- A microservices application is a collection of small, independent services that communicate with each other to form a complete application.
- It is built as a set of independent services that are deployed and managed separately
- It is flexible and scalable as individual services can be updated and deployed without affecting the entire application.
- It tends to be loosely coupled.
- These applications can be more complex to build and manage, since they require coordination among multiple services.
- They can also be more expensive to run, since each service needs to be individually managed and scaled.
๐๐จ๐ฑ๐ข๐ง๐ 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 ๐๐ง๐๐จ๐ฑ๐ข๐ง๐ .
Here are some benefits of ๐๐ฌ๐๐๐๐๐๐ง๐ฅ๐ฒ Method of ๐๐ข๐ฌ๐ญ<๐>
- It gives you read only view of your collection.
- It allows you to prevent the collection from being modified, either accidentally or intentionally
- It can improve the performance of your code in some cases because the read-only wrapper provides a more restricted view of the collection.
- This can be particularly beneficial when working with large collections, where even small performance improvements can make a significant difference.
๐๐ง๐ฒ ๐ฏ๐ฌ ๐๐ฅ๐ฅ ๐๐ฑ๐ญ๐๐ง๐ฌ๐ข๐จ๐ง ๐๐๐ญ๐ก๐จ๐ ๐จ๐ ๐๐ข๐ฌ๐ญ<๐>
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.
๐๐๐ณ๐ฒ ๐๐จ๐๐๐ข๐ง๐ (๐๐)
-
Lazy Loading is a process where EF loads the related entities on demand.
-
It is the default behavior of EF
-
It delays the loading of related entities until you specifically request it.
-
You can go with it when you are sure that you are not using the related entities Instantly.
-
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.
-
It simply uses the SELECT Statement without any join.
-
If you are not interested in related entities or the related entities are not used instantly, then you can use it.
๐๐๐ ๐๐ซ ๐๐จ๐๐๐ข๐ง๐ (๐๐)
-
Eager loading is a Process where EF loads the related entities along with the main entity
-
EF will not execute separate SQL queries for loading the related entities.
-
All the entities are loaded from the database with a single query saving bandwidth and server CPU time.
-
You can go with EL when you are sure that you will be using the related entities with the main entity everywhere
-
It is a good practice to reduce the number of SQL queries to be sent to the database server to fetch the related entities
-
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.
-
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.
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
- Sum
- Average
- Minimum
- Maximum
- Count
.NET 6.0 has 12 overloads of these all methods for all numeric data types used in code โฌ
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 ๐๐ฎ๐ฅ๐ญ๐ข๐ฅ๐๐ฏ๐๐ฅ ๐๐๐ญ๐ซ๐ข๐๐ฏ๐๐ฅ.
ToQueryString is a custom extension method that converts IQueryable to SQL Query at the back-end side, especially helpful for debugging. โฌ
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.
- 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
- 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.
- 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?
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.
-
For .NET 3.1 register in ConfigureServices in ๐๐ญ๐ซ๐๐ญ๐ฎ๐ฉ.๐๐ฌ
-
For latest versions of .NET(6.0 ,7.0) register in ๐๐ซ๐จ๐ ๐ซ๐๐ฆ.๐๐ฌ
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.
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:
- Routing
- Exception handling
- Authentication and authorization
- CORS (Cross-Origin Resource Sharing)
- Response compression
- Request validation
- 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.
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.
- 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.
๐๐๐๐ ๐๐ฒ๐ฉ๐๐ฌ ๐๐ฒ ๐๐๐๐๐ฎ๐ฅ๐ญ ๐๐ฎ๐ฉ๐ฉ๐จ๐ซ๐ญ๐๐ ๐๐ฒ .๐๐๐ ๐๐ซ๐จ๐ฏ๐ข๐๐๐ซ๐ฌ
- text/plain
- text/css
- application/javascript
- text/html
- application/xml
- text/xml
- application/json
- text/json
- application/wasm
๐๐จ๐ฎ๐ง๐ญ()
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.
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.
๐๐๐ง๐๐๐ข๐ญ๐ฌ ๐จ๐ ๐๐๐ญ๐ ๐๐ข๐ฆ๐ข๐ญ๐ข๐ง๐ ?
- Protecting against denial-of-service attacks of specific types.
- Maintaining service availability.
- Reducing resource consumption.
- Detecting & blocking maliciousbehavior.
- 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.
๐๐ญ ๐ฐ๐ก๐ข๐๐ก ๐ฉ๐จ๐ข๐ง๐ญ ๐ฐ๐ ๐๐๐ง ๐๐ฉ๐ฉ๐ฅ๐ฒ ๐๐๐ญ๐ ๐๐ข๐ฆ๐ข๐ญ๐ข๐ง๐ ?
- Network Edge
- Application Layer
- Database Layer
- 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โฌ
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.
๐๐๐ง๐๐๐ข๐ญ๐ฌ ๐จ๐ ๐๐๐ฌ๐ฉ๐จ๐ง๐ฌ๐ ๐๐๐๐ก๐ข๐ง๐ ?
-
Improved performance by reducing the load on the server.
-
Reduced server load, as it can serve the cached response instead of generating a new one.
-
Reduced bandwidth usages it reduces the amount of data that needs to be transferred between the server and the client.
-
Improved security as it can reduce the number of requests that reach the server, reducing the risk of certain types of attacks.
๐๐ง ๐ฐ๐ก๐ข๐๐ก ๐ซ๐๐ช๐ฎ๐๐ฌ๐ญ๐ฌ ๐ฐ๐ ๐๐๐ง ๐๐ฉ๐ฉ๐ฅ๐ฒ ๐๐๐ฌ๐ฉ๐จ๐ง๐ฌ๐ ๐๐๐๐ก๐ข๐ง๐
- Get
- 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.
-
Cache-Control header parameters must be valid, and the response must be marked public and not marked private.
-
The Content-Length header value (if set) must match the size of the response body.
๐๐จ๐ฆ๐ ๐ซ๐๐๐ฅ-๐ฐ๐จ๐ซ๐ฅ๐ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐๐ฌ ๐จ๐ ๐๐๐ฌ๐ฉ๐จ๐ง๐ฌ๐ ๐๐๐๐ก๐ข๐ง๐
- News website
- 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.
โ๏ธ .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
This post crossed 100K views on my LinkedIn
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.
-
Transient โก๏ธ It creates new instances every time its injected.
-
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.
-
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.
-
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.
-
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.
-
Enhanced decoupling: Dependency injection promotes loose coupling between classes, which can make your application more resilient to change and easier to test.
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
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
-
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.
-
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
-
For better understanding we can create different DI classes that would be dealing with similar dependencies.
How you prefer to keep your dependencies? ๐
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 ๐โฌ
๐๐๐ โ 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. ๐โฌ
Difference b/w String and StringBuilder
๐๐ญ๐ซ๐ข๐ง๐ ๐๐ฎ๐ข๐ฅ๐๐๐ซ
-
StringBuilder is mutable
-
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.
๐๐ญ๐ซ๐ข๐ง๐
-
String is immutable.
-
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.โฌ