DotNetTips
400 subscribers
88 photos
74 links
Our channel is dedicated to providing quick, practical tips and insights for developers working with C# and the .NET ecosystem.

Admin:@DTN_World

YouTube: https://www.youtube.com/@mnegah
Download Telegram
βœ… Microsoft announces a new free plan for GitHub Copilot, available for everyone today in VS Code. All you need is a GitHub account. No trial. No subscription. No credit card required.

πŸ’‘With GitHub Copilot Free you get 2000 code completions/month. That's about 80 per working day - which is a lot. You also get 50 chat requests/month, as well as access to both GPT-4o and Claude 3.5 Sonnet models.

✨With this announcement, GitHub Copilot becomes a core part of the VS Code experience. The team has been hard at work, as always, improving that experience with brand new AI features and capabilities.

Let’s take a look at some of the newer additions to GitHub Copilot that dropped in just the past few months. This is your editor, redefined with AI.

more videos: youtube.com/@mnegah
more tips: t.me/DotNetTipsChl
πŸ‘2
Powerful features in EF Core.

AsNoTracking
πŸ’‘It tells EF Core not to track the changes for the entities retrieved by a query. This makes the query read-only and faster because EF Core skips creating tracking objects.

CompiledQuery
πŸ’‘It precompiles the query, so EF Core skips the translation step on repeated executions.

DisableLazyLoading
πŸ’‘If you fetch a list of orders and EF Core fetches each customer separately, you’ll end up with multiple small queries hitting the database, slowing down your application.
Disable lazy loading and take full control of the data you fetch.

✨By using these features:
AsNoTracking: You speed up read-only queries by skipping change tracking.
DisableLazyLoading: You avoid unwanted database queries and take control of related data loading.
CompiledQuery: You optimize frequently used queries by precompiling them.

πŸ‘‰ If you want to discover more advanced features in EF Core, like batching, query optimization, and eager loading, check out this video: https://youtu.be/rqGzFQeD-3Q
πŸ‘6πŸ‘1
βœ… Using multiple DbContext instances in .NET is useful when you need to work with different databases or separate read and write operations for better performance or scalability.

Explanation
1️⃣ WriteDbContext:
Configured to handle data modification operations (e.g., Insert, Update, Delete).

2️⃣ ReadDbContext:
Optimized for read operations, possibly with AsNoTracking by default to improve query performance.

3️⃣ Separation of Concerns:
Each DbContext has its own connection string, allowing connection to separate databases (or the same database if you want to separate operations logically).
Scenarios:

πŸ’‘Useful for CQRS (Command Query Responsibility Segregation).
πŸ’‘Ideal for scaling with read replicas or partitioned databases.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘2
What is Refit?
βœ… Refit is a library for .NET that simplifies API integration by turning REST APIs into strongly-typed interfaces. It automatically generates the boilerplate code required for making HTTP requests, reducing the need to manually handle HttpClient and JSON serialization/deserialization.

How Refit Works
You define an interface with HTTP method attributes, and Refit generates a concrete implementation of the interface at runtime.

Advantages of Using Refit
1️⃣ Less Boilerplate: Eliminates the need for repetitive HttpClient setup code.
2️⃣ Strongly-Typed Interfaces: Errors are caught at compile time rather than runtime.
3️⃣ Customizable: You can add headers, authentication, and other configurations easily.

Use Case
πŸ’‘Refit is ideal for applications that frequently consume external REST APIs and need clean, maintainable code with minimal boilerplate.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ”₯2
βœ… .NET has a built-in Inversion of Control (IoC) container provided through Microsoft.Extensions.DependencyInjection. It supports three main lifetimes for dependency injection

1. Transient
1️⃣Instance Lifetime: A new instance of the service is created every time it is requested
2️⃣Usage: Suitable for lightweight and stateless services
3️⃣Scope Independence: Does not depend on the scope of a request. Each request (even within the same HTTP request) gets a fresh instance

2. Scoped
1️⃣Instance Lifetime: A single instance is created per HTTP request and shared within that request
2️⃣Usage: Ideal for services needing state isolation per request
3️⃣Scope Dependence: Same instance is reused in a request; a new instance is created for each new request

3. Singleton
1️⃣Instance Lifetime: One instance for the entire application lifetime
2️⃣Usage: Best for globally shared data or state, like configuration or caching
3️⃣Scope Independence: Same instance is used across all requests and threads

t.me/DotNetTipsChl
πŸ‘6
βœ… In C#, ConcurrentDictionary and ConcurrentQueue are part of the System.Collections.Concurrent namespace. They are designed for use in multi-threaded scenarios where multiple threads can safely read and write without explicit locking.

ConcurrentDictionary
πŸ’‘A ConcurrentDictionary is a thread-safe collection for key-value pairs. It provides better performance compared to manually managing locks on a Dictionary in a multi-threaded environment.

ConcurrentQueue
πŸ’‘A ConcurrentQueue is a thread-safe, FIFO (First In, First Out) collection designed for scenarios where multiple threads need to add or remove items concurrently.

When to Use
✨Use ConcurrentDictionary when you need a shared dictionary with frequent reads and writes by multiple threads.
✨Use ConcurrentQueue when implementing a producer-consumer pattern or when items need to be processed in order.

πŸ”— more tutorial videos: youtube.com/@mnegah
πŸ”— more tutorial tips: t.me/DotNetTipsChl
πŸ‘4
πŸš€Advanced Search Integration in .NET9 with Elasticsearch and SQL Server

βœ… In this comprehensive guide, I explore the evolution of search technology, focusing on Elasticsearch, a powerful search engine that originated from the creators of Apache Lucene.

1️⃣I'll walk through configuring Elasticsearch using Docker, ensuring a seamless setup for your development environment.

2️⃣Additionally, I'll cover the installation of essential packages in a .NET 9 application, enabling efficient integration with Elasticsearch.

3️⃣To validate our implementation, I'll utilize Postman for testing, ensuring my search functionality operates as intended.

πŸ’‘By the end of this tutorial, you'll have a solid understanding of implementing advanced search capabilities in ASP.NET Core applications, leveraging the combined power of Elasticsearch and SQL Server.

πŸ”— Watch the full tutorial now: https://lnkd.in/eKVu6Vbf
πŸ”— project from my GitHub: https://lnkd.in/eTJ_67gG
πŸ”₯4
🌟Explore .NET 8 & 9 with My Latest Tutorials!🌟

Over the past few months, I've been working hard to create high-quality tutorials on some of the most exciting and advanced topics in .NET 8 and .NET 9. If you're looking to take your skills to the next level, here is some of my tutorials that you’ll find on my channel:

βœ…
Implementing API Gateway with Ocelot

βœ…
How to use and implement gRPC services in .NET9

βœ…
What is .NET Aspire

βœ…
Hybrid Caching with Redis and In-Memory cache in .NET9

βœ…
Advanced Search with Elastic

βœ…
How to Use Keyed Services in .NET 8

πŸ‘‰If you want more, check out this link: YouTube Channel

πŸ”₯ What’s next?
I’m committed to helping you stay ahead of the curve by creating content that is easy to understand yet comprehensive. Whether you're a beginner or a seasoned developer, there’s something for everyone!

πŸ”— more videos: https://lnkd.in/gzEJjumb
πŸ”— more tips: t.me/DotNetTipsChl
πŸ”— Github account: github.com/mnegah
πŸ”₯5πŸ‘2
What is Connection Pooling?
Connection pooling is a mechanism that allows the reuse of established database connections. Instead of creating and destroying connections for each database request (which is resource-intensive), a pool of connections is maintained, and these connections are shared among requests.

Benefits of Using Connection Pooling
βœ…Performance:
Reuses existing connections to avoid the overhead of frequently opening and closing them.

βœ…Scalability:
Manages multiple simultaneous requests efficiently with limits like Min Pool Size and Max Pool Size.

βœ…Resource Management:
Max Pool Size: Prevents overloading the database.
Min Pool Size: Ensures connections are available during high traffic.

βœ…Reduced Latency:
Pre-created connections allow quicker database access, improving response times.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘3πŸ”₯2
βœ… The Circuit Breaker pattern prevents repeated calls to failing services in distributed systems, handling failures gracefully and avoiding cascading issues.

Example Scenario
Imagine an e-commerce system where the payment gateway might become unavailable. A circuit breaker prevents the system from continuously sending payment requests, which could worsen the problem.

πŸ’‘Above is a simplified implementation using a Policy library like Polly, which is commonly used for implementing the Circuit Breaker pattern in .NET applications.

How It Works
1️⃣ The Polly Circuit Breaker policy monitors exceptions.

2️⃣ After 3 consecutive failures, the circuit breaker transitions to the Open State.

3️⃣ In the Open State, requests are immediately blocked for 10 seconds.

4️⃣ After 10 seconds, it transitions to the Half-Open State, allowing a few test requests.

5️⃣ If a test request succeeds, the circuit breaker resets to the Closed State.

πŸ”— more videos: https://lnkd.in/gzEJjumb
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘5
βœ…In-Memory Cache
Cache stored within the memory of the application (e.g., server's RAM).Limited to the application instance. If the app restarts, the cache is lost. Faster since it's local to the application. Limited by the memory of the single application server. Only supports a single application instance.

βœ…Redis Cache
External cache server that stores data in memory and can be accessed by multiple applications. Persistent across application restarts, accessible from multiple servers. Slightly slower because it's accessed over the network. Highly scalable across multiple servers. Allows multiple applications/instances to share the same cache.

πŸ’‘Key Considerations
1.Use in-memory cache for simple, single-server applications or temporary caching.
2.Use Redis cache for distributed systems or when multiple servers need to share the same cached data.

πŸ”₯Watch this if you want to learn more: https://lnkd.in/dEiqvVDV

πŸ”— more videos: https://lnkd.in/gzEJjumb
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘2
Pooling Pattern
βœ… The pooling pattern is a design technique where a finite set of reusable objects (or resources) is maintained in a pool. Instead of creating and destroying objects repeatedly, the system reuses objects from the pool, improving performance and resource management.

How It Works
πŸ’‘Initialization: A pool is created with a predefined number of objects (e.g., database connections, threads, or sockets).
πŸ’‘Checkout: When a client needs an object, it "borrows" one from the pool.
πŸ’‘Usage: The client uses the borrowed object for its operation.
πŸ’‘Check-in: After use, the object is returned to the pool, making it available for others.

✨Key Pros and Cons
Pros

Boosts performance by reusing objects.
Efficiently manages limited resources.
Reduces object creation and garbage collection.

Cons
Adds complexity to implementation.
Risk of delays if the pool size is insufficient.
Memory can be wasted on idle objects

πŸ”— more videos: https://lnkd.in/gzEJjumb
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘3
Setting a Timeout in HttpClient
βœ… The timeout feature of HttpClient is crucial for ensuring your application remains responsive, especially when dealing with external web services that might be slow or unavailable. Always set a reasonable timeout to prevent your app from hanging and offer a better user experience.

Why Set a Timeout?
A timeout ensures that if a request takes too long, your app doesn’t get stuck waiting forever. It's essential for maintaining responsiveness in your app.

πŸ’‘Key Points:
Timeout: Set the time limit for how long a request should wait for a response.
Exception: If the request exceeds the timeout, a TaskCanceledException is thrown.
Adjust as Needed: Increase or decrease timeout duration depending on your app’s needs.

✨ Why It’s Important:
Prevents unresponsive apps.
Improves user experience.
Keeps control over external requests.

πŸ”— more videos: https://lnkd.in/gzEJjumb
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘Œ3πŸ‘2
βœ… RestSharp is a powerful library that simplifies HTTP requests in .NET. It automatically handles serialization & deserialization, making API calls much easier than HttpClient.

πŸ’‘Why use RestSharp?
βœ”οΈ Easy-to-use API
βœ”οΈ Auto JSON serialization/deserialization
βœ”οΈ Supports authentication, retries, and custom headers
βœ”οΈ More readable & concise than HttpClient
βœ”οΈ Handles API authentication easily
βœ”οΈ Built-in JSON handling

πŸ’‘ Key Features of RestSharp
▫️Easy API Calls: Makes GET, POST, PUT, DELETE requests simple.
▫️Serialization & Deserialization: Supports automatic JSON/XML serialization.
▫️Authentication Support: Handles OAuth, Basic, Bearer Token, etc.
▫️Automatic Parameter Handling: Easily adds URL/query/body parameters.
▫️Asynchronous Requests: Supports async API calls with await.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘3
String vs. StringBuilder in C#

βœ…In C#, both string and StringBuilder are used to handle text, but they behave differently in terms of performance and mutability. Let’s explore the differences with a simple example.

πŸ”Ή Immutable string (Slow for Modifications)
A string is immutable, meaning that every modification creates a new string in memory.

πŸ“Œ Explanation:
Each time we append " World", a new string is created in memory.
The old string remains in memory until garbage collection removes it, which slows down performance when dealing with many modifications.

πŸ”Ή Mutable StringBuilder (Faster for Modifications)
StringBuilder is mutable, meaning it modifies the existing object instead of creating a new one.

πŸ“Œ Explanation:
StringBuilder does not create a new object on each modification.
It modifies the same memory location, making it much faster and memory-efficient for large-scale string operations.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘3πŸ”₯1
βœ…In C#, == and .Equals() are used to compare objects, but they work differently depending on the type being compared.

πŸ”Ή== Operator:
Compares references by default (checks if two objects are the same in memory).
For value types (like int, float, struct), compares values directly.
Can be overridden for custom class comparisons.

πŸ”Ή.Equals() Method:
Inherits from Object; compares references by default, like ==.
Often overridden (e.g., string, int) to compare values, not references.

πŸ’‘When to Use:
==: For reference comparison or value types.
.Equals(): For comparing object contents/values, especially in reference types.

✨Key Takeaways:
==: Reference comparison by default; can be overridden for custom types.
.Equals(): Value comparison for value types and can be overridden for reference types to compare actual content.


πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ”₯2
βœ…Optimizing HttpClient Management in .NET
Socket exhaustion occurs when too many open sockets exceed system limits, leading to connection failures. This happens if you create and dispose of HttpClient instances in a loop (e.g., inside a using block), preventing socket reuse and causing port exhaustion.

πŸ’‘Solutions to Prevent Socket Exhaustion:

1️⃣ Using IHttpClientFactory:
IHttpClientFactory is an interface that is used to automatically manage the lifecycle and configuration of HttpClient instances. It prevents socket exhaustion and properly handles DNS changes.

2️⃣ Using SocketsHttpHandler for Fine-Grained Configuration:
When using HttpClient as Singleton or Static, you can use SocketsHttpHandler along with settings like PooledConnectionLifetime to optimize connections and network resources.
SocketsHttpHandler gives you more control over network resources, helping resolve issues like socket exhaustion and DNS changes management.
πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘4πŸ™1
Retry pattern
βœ… The RetryAsync method retries an operation (in this case, DoUnstableOperation) up to a specified number of times (maxRetryAttempts) with a delay (pauseBetweenFailures) between each attempt. You can modify the pauseBetweenFailures to increase after each retry for exponential backoff If the operation succeeds within the retry limit, it prints a success message; otherwise, it reports the failure.

βœ… Useful for handling transient faults such as network failures, API timeouts, or database connection drops.
Avoids failing immediately on the first failure, improving resilience of the system.

πŸ’‘Also, you can use Polly to implement the Retry pattern.
πŸ‘3
βœ…Polly is a powerful library for handling errors and implementing resilient patterns like retries, circuit breakers, and timeouts in .NET. It's most effective when used with HttpClientFactory for managing HttpClient instances efficiently.

πŸ“ŒExplanation:
HttpClientFactory: It's the best practice to use HttpClientFactory when working with Polly and HttpClient. This ensures that HttpClient instances are managed efficiently, preventing socket exhaustion and improving performance.
Retry Policy: Retries the request up to 3 times if it fails.
Circuit Breaker: Prevents further requests if 3 failures occur within a minute.
Timeout Policy: Cancels the request if it takes longer than 10 seconds.
Fallback Policy: Returns a default response if all retries and circuit breakers fail.

By combining these policies and using HttpClientFactory, you can create a robust and efficient error-handling strategy for your HTTP requests in .NET.
πŸ‘3
βœ…Keyed Services in .NET 8 – A Solution for Multiple Implementations of an Interface!
Have you ever needed an interface with multiple implementations? For example, a notification system that sends messages via both SMS and Email. The challenge is:

πŸ”Ή How do we specify which implementation to use in Dependency Injection (DI)?
πŸ”Ή Why were old methods like Factory Pattern or Named Services problematic?
πŸ”Ή What smart solution does .NET 8 offer?

πŸš€ The answer: Keyed Services! This new feature in .NET 8 allows us to register multiple implementations of the same interface using unique keys, making it easy to retrieve the desired one wherever needed.

πŸ“Œ Conclusion
πŸ”Ή Keyed Services in .NET 8 make it easy to manage multiple implementations of an interface.
πŸ”Ή This approach improves flexibility and code readability while reducing complexity.

πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ”₯4πŸ‘2
βœ…As our applications grow, their complexity grows as well. Without clear structure, we face code that is hard to test, maintain, or extend.
SOLID principles offer proven guidelines to design flexible, modular, and maintainable software systems.

🧩 S β€” Single Responsibility Principle (SRP)
A class should have only one reason to change.
Each class should be responsible for one specific task.
When a class handles multiple concerns, it becomes harder to test, understand, and modify.

βœ… Real-World Example

A poor design would do all(Fetch,Format,Export,Send Data) of that in one class.
A better design would follow SRP:

Easier Testing: You can now test EmailSender independently without worrying about the entire report generation.
Flexibility: If you need to export to Excel instead of PDF, you only need to change the PdfExporter class.
Clearer Code: Anyone reading the code can immediately understand the responsibilities of each class.


πŸ”— more videos: youtube.com/@mnegah
πŸ”— more tips: t.me/DotNetTipsChl
πŸ‘2