<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Weekly .NET Pulse]]></title><description><![CDATA[I share .NET content every week, that help you boost your .NET skill to next level.]]></description><link>https://noumanbaloch.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!wwGL!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe99a99aa-bb6e-40ea-92c1-e2b8a597b586_256x256.png</url><title>Weekly .NET Pulse</title><link>https://noumanbaloch.substack.com</link></image><generator>Substack</generator><lastBuildDate>Thu, 21 May 2026 22:12:23 GMT</lastBuildDate><atom:link href="https://noumanbaloch.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Nouman Baloch]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[noumanbaloch@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[noumanbaloch@substack.com]]></itunes:email><itunes:name><![CDATA[Nouman Baloch]]></itunes:name></itunes:owner><itunes:author><![CDATA[Nouman Baloch]]></itunes:author><googleplay:owner><![CDATA[noumanbaloch@substack.com]]></googleplay:owner><googleplay:email><![CDATA[noumanbaloch@substack.com]]></googleplay:email><googleplay:author><![CDATA[Nouman Baloch]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Partial Classes in C# - When and How to Use Them]]></title><description><![CDATA[When working on large-scale applications in C#, partial classes provide a convenient way to split a class across multiple files.]]></description><link>https://noumanbaloch.substack.com/p/partial-classes-in-c-when-and-how</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/partial-classes-in-c-when-and-how</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 21 Oct 2024 18:26:01 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ZaCo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZaCo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZaCo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZaCo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:430327,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZaCo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!ZaCo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F8222a867-6b2e-4eab-8c9c-1ab589307225_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When working on large-scale applications in C#, partial classes provide a convenient way to split a class across multiple files. This feature is especially useful in team-based projects or auto-generated code scenarios. In this article, we'll explore the benefits of partial classes, when to use them, and how to implement them effectively.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h3>What Are Partial Classes?</h3><p>A partial class in C# allows a single class, interface, or struct to be divided into multiple physical files. While the class appears as a single entity in memory during runtime, it is split into several files for better manageability during development.</p><div><hr></div><h3>When and Why to Use Partial Classes</h3><p>Partial classes are commonly used in scenarios where the code base is large, and maintaining a single file would make the code unwieldy. They offer several key benefits:</p><h4>1. Code Organization:</h4><p>When a class becomes too large or contains different sets of functionalities, partial classes enable the logical division of code into smaller files. This promotes clean separation of concerns.</p><h4>2. Team Collaboration:</h4><p>In large projects, partial classes allow multiple developers to work on the same class without causing conflicts. Each developer can focus on different aspects of the class, such as methods or properties, in separate files.</p><h4>3. Auto-Generated Code:</h4><p>Partial classes are extensively used in auto-generated code scenarios, such as in Windows Forms, WPF, or ASP.NET. The auto-generated code resides in one file, while custom logic resides in another, preventing the loss of custom code when regenerating the file.</p><div><hr></div><h3>How to Implement Partial Classes</h3><p>Using partial classes is simple. You just need to use the <code>partial</code> keyword when defining a class in multiple files. Here's an example:</p><p>File 1: Car_Part1.cs</p><pre><code>public partial class Car
{
    public string Make { get; set; }
    public string Model { get; set; }

    public void DisplayCarInfo()
    {
        Console.WriteLine($"Make: {Make}, Model: {Model}");
    }
}</code></pre><p>File 2: Car_Part2.cs</p><pre><code>public partial class Car
{
    public int Year { get; set; }
    public string Color { get; set; }

    public void DisplayColor()
    {
        Console.WriteLine($"Color: {Color}");
    }
}</code></pre><p>In this example, the <code>Car</code> class is split across two files. When compiled, both parts will be combined into a single class with all properties and methods accessible as if they were defined in one file.</p><div><hr></div><h3>When Not to Use Partial Classes</h3><p>While partial classes provide flexibility and improve code organization, there are scenarios where using them may not be ideal:</p><ul><li><p><strong>Excessive Use:</strong> Overusing partial classes can lead to scattered code, making it harder to track where specific functionality resides.</p></li><li><p><strong>Readability Issues:</strong> Splitting related functionality across multiple files can reduce readability, especially in smaller projects where the entire class can easily fit into a single file.</p></li></ul><div><hr></div><h3>Conclusion</h3><p>Partial classes offer a way to manage large codebases more effectively, streamline team collaboration, and keep auto-generated code separate from custom logic. However, they should be used with care to avoid reducing code clarity.</p><p>By understanding when and how to use partial classes, developers can maintain cleaner, more organized projects, especially as their applications scale.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[2 Common Myths About Async/Await in .NET]]></title><description><![CDATA[Async/await in .NET simplifies asynchronous programming, allowing developers to write non-blocking code.]]></description><link>https://noumanbaloch.substack.com/p/2-common-myths-about-asyncawait-in</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/2-common-myths-about-asyncawait-in</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 14 Oct 2024 06:11:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!doEZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!doEZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!doEZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!doEZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:436521,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!doEZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!doEZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4ff49218-15ad-454c-be56-f6054e24dcf3_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Async/await in .NET simplifies asynchronous programming, allowing developers to write non-blocking code. However, several misconceptions surround its use. In this article, we'll explore three common myths about async/await and clarify how it works with code examples.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h3>Myth 1: Async/await Creates New Threads</h3><p>Some believe that using async/await automatically spawns new threads for every operation.</p><p><strong>Reality</strong>: Async/await doesn&#8217;t create new threads directly. Instead, it reuses threads from the thread pool and runs tasks without blocking the main thread.</p><pre><code>public async Task PerformAsyncOperation()
{
    // This simulates an I/O-bound operation like an HTTP request
    await Task.Delay(2000); // Simulates waiting for a response

    Console.WriteLine("Operation complete.");
}</code></pre><p>In the above code, <code>Task.Delay(2000)</code> doesn&#8217;t block the calling thread for 2 seconds. Instead, it schedules the continuation after the delay, releasing the thread to handle other tasks.</p><p><strong>Key Insight</strong>: Async/await enables efficient use of threads by reusing existing ones from the thread pool, especially for I/O-bound operations.</p><div><hr></div><h3>Myth 2: Async/await Automatically Improves Performance</h3><p>There is a misconception that using async/await will automatically speed up the execution of code.</p><p><strong>Reality</strong>: While async/await improves responsiveness, particularly with I/O-bound tasks, it doesn&#8217;t necessarily increase performance for CPU-bound tasks.</p><pre><code>public async Task ProcessDataAsync()
{
    // Simulates an I/O-bound operation like reading from a file
    await Task.Delay(1000);

    // Simulates CPU-bound processing
    for (int i = 0; i &lt; 1000000; i++)
    {
        // Intensive computation
        var result = Math.Sqrt(i);
    }

    Console.WriteLine("Data processing complete.");
}</code></pre><p>In the example, the <code>Task.Delay(1000)</code> prevents the thread from being blocked while waiting for I/O. However, the CPU-bound loop that follows doesn't benefit from async/await and takes up processing power as usual.</p><p><strong>Key Insight</strong>: Async/await improves responsiveness during I/O-bound tasks but doesn't offer significant performance boosts for CPU-bound operations.</p><div><hr></div><h3>Conclusion</h3><p>By understanding and debunking these myths, you can use async/await effectively in your .NET projects:</p><ol><li><p>Async doesn&#8217;t create new threads or guarantee faster execution&#8212;it improves responsiveness for I/O-bound tasks.</p></li><li><p>Async methods are best suited for non-blocking I/O operations, not CPU-bound tasks.</p></li></ol><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Understanding How foreach Works Internally in C#]]></title><description><![CDATA[When working with collections in C#, the foreach statement provides an easy way to iterate over elements.]]></description><link>https://noumanbaloch.substack.com/p/understanding-how-foreach-works-internally</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/understanding-how-foreach-works-internally</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 07 Oct 2024 06:26:38 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!UWeZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UWeZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UWeZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UWeZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:427500,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!UWeZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!UWeZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7e74f364-0d7e-4d0b-b360-fc8830cadb03_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When working with collections in C#, the <code>foreach</code> statement provides an easy way to iterate over elements. To make sure a custom collection type works seamlessly with <code>foreach</code>, developers need to understand the mechanics behind it. In this article, we'll explore how the <code>foreach</code> loop functions, the rules it follows, and how to implement it in your custom types.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h3>foreach Internals</h3><p>In C#, the <code>foreach</code> loop can iterate over any collection or type that follows specific rules. These rules define how the loop retrieves each element one by one.</p><h3>How foreach Works Internally:</h3><ol><li><p><strong>GetEnumerator Method:</strong><br>The collection must have a method called <code>GetEnumerator</code> that returns an object responsible for the iteration. This object is called the enumerator.</p></li><li><p><strong>MoveNext Method</strong>:<br>The enumerator object needs to provide a <code>MoveNext</code> method. This method advances the enumerator to the next element in the collection and returns a <code>bool</code>:</p><ul><li><p><code>true</code> if there are more elements.</p></li><li><p><code>false</code> if the end of the collection has been reached.</p></li></ul></li><li><p><strong>Current Property</strong>:<br>The enumerator also needs a property named <code>Current</code>, which provides the current item in the collection. This property is read-only, meaning it allows access to the item but does not permit modification.</p></li></ol><p>Even though the .NET framework provides <code>IEnumerable</code> and <code>IEnumerable&lt;T&gt;</code> interfaces to formalize these rules, the compiler doesn't require the collection to implement them as long as the necessary methods and properties are in place.</p><div><hr></div><h3>How the Compiler Transforms foreach into Code</h3><p>When you write a <code>foreach</code> loop, the compiler translates it into a series of method calls under the hood. Here&#8217;s a simple example:</p><pre><code>foreach (string name in names)
{
    Console.WriteLine($"{name} has {name.Length} characters.");
}</code></pre><p>The above code gets transformed into something like this:</p><pre><code>IEnumerator e = names.GetEnumerator();
while (e.MoveNext())
{
    string name = (string)e.Current; // Current is read-only!
    Console.WriteLine($"{name} has {name.Length} characters.");
}</code></pre><p>As you can see, the compiler:</p><ul><li><p>Calls the <code>GetEnumerator</code> method on the collection.</p></li><li><p>Uses the <code>MoveNext</code> method to loop through each element.</p></li><li><p>Accesses the <code>Current</code> property to get the value of the current element in each iteration.</p></li></ul><p>This process ensures that the <code>foreach</code> statement works seamlessly with any collection that adheres to the rules of enumeration.</p><div><hr></div><h3>Why You Can&#8217;t Modify Current in foreach</h3><p>One important limitation of the <code>foreach</code> loop is that the variable representing the current item is read-only. This is because the <code>Current</code> property, which provides access to the current element, is designed to be immutable. The iterator pattern ensures that the collection remains unchanged while it's being traversed, maintaining the integrity of the data.</p><div><hr></div><h3>Implementing IEnumerable for Custom Types</h3><p>While the compiler doesn't enforce the use of <code>IEnumerable</code>, implementing it is a good practice. Doing so makes your custom collection more versatile and compatible with various LINQ methods and other .NET libraries. Here's a simple implementation of <code>IEnumerable&lt;T&gt;</code> in a custom collection:</p><pre><code>public class MyCollection&lt;T&gt; : IEnumerable&lt;T&gt;
{
    private T[] items;

    public MyCollection(T[] items)
    {
        this.items = items;
    }

    public IEnumerator&lt;T&gt; GetEnumerator()
    {
        for (int i = 0; i &lt; items.Length; i++)
        {
            yield return items[i];
        }
    }

    IEnumerator IEnumerable.GetEnumerator() =&gt; GetEnumerator();
}</code></pre><p>By implementing <code>IEnumerable&lt;T&gt;</code>, your collection can now be used in a <code>foreach</code> loop and benefit from the many tools in the .NET ecosystem.</p><div><hr></div><h3>Conclusion</h3><p>Understanding how <code>foreach</code> works under the hood helps you implement custom collection types that are compatible with C#'s iteration features. Whether you&#8217;re working with built-in types or building your own, adhering to the rules of the iterator pattern ensures smooth, efficient looping with the <code>foreach</code> statement.</p><p>If you're working on scalable, flexible systems or need type safety with iteration, consider implementing the <code>IEnumerable</code> interface in your custom collections to enhance compatibility and usability in your projects.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[JWT vs Session Cookies: Understanding the Difference in Authentication]]></title><description><![CDATA[Authentication is a fundamental part of any web application.]]></description><link>https://noumanbaloch.substack.com/p/jwt-vs-session-cookies-understanding</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/jwt-vs-session-cookies-understanding</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 16 Sep 2024 07:12:47 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Wqbc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Wqbc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Wqbc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Wqbc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:426120,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Wqbc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!Wqbc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe89a98be-78ce-464c-9244-e0af1a727ec0_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Authentication is a fundamental part of any web application. Two of the most common methods for handling user authentication are <strong>JWT (JSON Web Token)</strong> and <strong>Session Cookies</strong>. While both methods serve the same purpose, they operate in distinct ways, each with its own advantages and disadvantages. In this article, we'll explore how each of these mechanisms works, their pros and cons, and when to use one over the other.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>JWT Authentication</h4><p>In <strong>JWT-based authentication</strong>, the server generates a token, which the client stores and sends back with each request. Unlike session-based methods, JWT authentication is <strong>stateless</strong>. This means that once a JWT is issued, the server doesn&#8217;t need to maintain any record of it, making it highly scalable.</p><div><hr></div><h4>How JWT Authentication Works:</h4><ol><li><p><strong>Token Creation</strong>: Upon user authentication, the server generates a JWT containing the user's information.</p></li><li><p><strong>Token Storage</strong>: This JWT is then sent back to the client, typically stored in <strong>local storage</strong> or an <strong>HTTP-only cookie</strong> for security.</p></li><li><p><strong>Token Presentation</strong>: The client includes this JWT in the <strong>HTTP Authorization header</strong> for all subsequent requests to the server.</p></li><li><p><strong>Token Validation</strong>: The server validates the JWT on each request. If the token is valid, the user is granted access.</p><div><hr></div></li></ol><h4>Advantages of JWT:</h4><ul><li><p><strong>Scalability</strong>: Being stateless, JWTs are perfect for distributed systems where keeping track of session data across multiple servers is complex.</p></li><li><p><strong>Flexibility</strong>: JWTs can be used across different domains and applications, making them ideal for microservices architectures.</p></li><li><p><strong>Security</strong>: Proper implementation of JWT with features like encryption and signing can ensure secure handling of user authentication.</p><div><hr></div></li></ul><h4>Session-Based Authentication</h4><p>Unlike JWT, <strong>session-based authentication</strong> is <strong>stateful</strong>. In this method, the server generates and stores session data for the user on the server-side. The client holds only a <strong>session identifier</strong>, usually stored as a cookie.</p><div><hr></div><h4>How Session-Based Authentication Works:</h4><ol><li><p><strong>Session Creation</strong>: The server verifies the user's credentials and creates a <strong>session record</strong> with details such as the user ID, session start time, expiry, and possibly other metadata like IP address and user agent.</p></li><li><p><strong>Session Identifier</strong>: The server then sends a <strong>session identifier</strong> to the client, stored as a cookie.</p></li><li><p><strong>Session Validation</strong>: For each subsequent request, the client sends the session identifier. The server queries its stored session records to validate the session.</p></li><li><p><strong>Access Control</strong>: If the session is valid, the user is granted access to the requested resource.</p><div><hr></div></li></ol><h4>Advantages of Session-Based Authentication:</h4><ul><li><p><strong>Simplicity</strong>: With all session data stored on the server, it&#8217;s straightforward to manage user sessions centrally.</p></li><li><p><strong>Revocation Efficiency</strong>: Sessions can be revoked or invalidated on the server instantly, ensuring that the user&#8217;s session is up to date and secure.</p><div><hr></div></li></ul><h4>Choosing Between JWT and Session Cookies</h4><h4>When to Use JWT:</h4><ul><li><p><strong>Scalability Needs</strong>: JWTs are ideal for large-scale applications or <strong>distributed systems</strong> where maintaining session data on the server side becomes challenging.</p></li><li><p><strong>Cross-Domain Flexibility</strong>: If your application spans multiple domains or services, JWT offers flexibility without the complexity of sharing session state between servers.</p><div><hr></div></li></ul><h4>When to Use Session Cookies:</h4><ul><li><p><strong>Simplicity and Security</strong>: For applications where simplicity is paramount and a centralized control of user sessions is required, session cookies provide a reliable and <strong>easy-to-manage</strong> solution.</p></li><li><p><strong>Immediate Revocation</strong>: If you need to <strong>revoke access</strong> instantly (e.g., for sensitive data access), sessions stored on the server offer greater control.</p><div><hr></div></li></ul><h4>Conclusion</h4><p>Both JWT and session-based authentication provide effective ways to handle user authentication, but each has its specific use cases. <strong>JWT</strong> is preferred for stateless, scalable systems where flexibility across domains is required, while <strong>session-based authentication</strong> is simpler and allows for more immediate control over user sessions.</p><p>Choosing the right method depends on your application&#8217;s needs. Consider your system&#8217;s scalability requirements, security concerns, and the complexity of session management when making your decision.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Managing Concurrency in EF Core: Optimistic vs Pessimistic Locking]]></title><description><![CDATA[Concurrency control is a critical aspect of application development, especially when dealing with multi-user environments.]]></description><link>https://noumanbaloch.substack.com/p/managing-concurrency-in-ef-core-optimistic</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/managing-concurrency-in-ef-core-optimistic</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 09 Sep 2024 06:06:42 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!lpt8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!lpt8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!lpt8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!lpt8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:432298,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!lpt8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!lpt8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd32325c1-fa5b-4192-b982-47e4bbf94b49_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Concurrency control is a critical aspect of application development, especially when dealing with multi-user environments. In Entity Framework (EF) Core, managing concurrency becomes essential to ensure data consistency across transactions. In this article, we will dive into two primary concurrency management strategies&#8212;<strong>Optimistic Locking</strong> and <strong>Pessimistic Locking</strong>&#8212;explaining how each works, when to use them, and how they are implemented in EF Core.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>What is Optimistic Locking?</h4><p><strong>Optimistic Locking</strong> assumes that most operations will not conflict, so no locks are held on the data until it's time to save changes. This strategy works well in high-concurrency environments where conflicts are rare, allowing transactions to proceed without waiting on locks.</p><div><hr></div><h4>How Optimistic Locking Works</h4><p>In EF Core, optimistic locking is usually achieved by adding a concurrency token to the entity, such as a version or timestamp field. When an update is made, EF Core checks this token to ensure no other transaction has modified the data since it was last read. If a conflict is detected, a <code>DbUpdateConcurrencyException</code> is thrown, indicating that the update cannot proceed.</p><pre><code>public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    
    // Optimistic concurrency token
    [Timestamp]
    public byte[] RowVersion { get; set; }
}</code></pre><p>In this example, the <code>RowVersion</code> property acts as the concurrency token. When an update occurs, EF Core checks this property to detect changes made by other transactions.</p><p>Here's how you might handle optimistic locking in practice:</p><pre><code>try
{
    var product = dbContext.Products.Find(productId);
    product.Price = newPrice;
    
    // Save changes with optimistic concurrency check
    dbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
    // Handle the concurrency conflict
    Console.WriteLine("Concurrency conflict detected!");
}</code></pre><div><hr></div><h4>Ideal Scenarios for Optimistic Locking</h4><p>Optimistic locking is best suited for scenarios where:</p><ul><li><p>Conflicts are rare.</p></li><li><p>High concurrency is expected, and performance is a priority.</p></li><li><p>The application can tolerate occasional retry logic in case of conflicts.</p></li></ul><div><hr></div><h4>What is Pessimistic Locking?</h4><p><strong>Pessimistic Locking</strong> takes a more cautious approach, assuming that conflicts are likely. When data is read, it is locked immediately, preventing other transactions from modifying it until the lock is released. This approach ensures strict data consistency but can lead to performance bottlenecks in high-concurrency scenarios.</p><div><hr></div><h4>How Pessimistic Locking Works</h4><p>In EF Core, pessimistic locking is typically implemented using raw SQL queries with locking hints like <code>UPDLOCK</code> and <code>ROWLOCK</code>. This ensures that the data is locked for the duration of the transaction, preventing other operations from reading or writing to it.</p><p>using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))</p><pre><code>using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
{
    // Execute raw SQL with pessimistic locking
    var product = dbContext.Products
        .FromSqlRaw("SELECT * FROM Products WITH (UPDLOCK, ROWLOCK) WHERE Id = {0}", productId)
        .SingleOrDefault();
    
    // Modify the product
    product.Price = newPrice;
    
    // Save changes
    dbContext.SaveChanges();
    
    // Commit the transaction
    transaction.Commit();
}</code></pre><p>In this example, the <code>UPDLOCK</code> and <code>ROWLOCK</code> hints are used to lock the selected row until the transaction is complete.</p><div><hr></div><h4>Ideal Scenarios for Pessimistic Locking</h4><p>Pessimistic locking is most appropriate when:</p><ul><li><p>Data integrity is critical, and conflicts must be avoided at all costs.</p></li><li><p>You can afford the performance overhead that comes with locking resources.</p></li><li><p>Transactions are short-lived, reducing the risk of deadlocks.</p></li></ul><div><hr></div><h4>Understanding Which Strategy to Use</h4><p>The choice between optimistic and pessimistic locking depends on the specific requirements of your application. Optimistic locking is generally the preferred option when:</p><ul><li><p>You expect minimal conflicts between transactions.</p></li><li><p>High performance and scalability are crucial.</p></li></ul><p>On the other hand, pessimistic locking is better suited for situations where:</p><ul><li><p>Conflicts are frequent.</p></li><li><p>Strict data integrity is essential, and conflicts must be prevented proactively.</p></li></ul><div><hr></div><h4>Conclusion</h4><p>In EF Core, both optimistic and pessimistic locking offer distinct advantages depending on your application's concurrency needs. By understanding the trade-offs between performance and data integrity, you can select the right locking strategy to ensure smooth and reliable operation, even in complex, multi-user environments.</p><p>Make sure to choose the right strategy based on your application's requirements. Optimistic locking is usually a better fit for systems with high concurrency and rare conflicts, while pessimistic locking is ideal for environments where conflicts are more common and data integrity is paramount.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Understanding appsettings Json Files in ASP.NET Core]]></title><description><![CDATA[In the world of C# development, especially when working with ASP.NET Core, managing configuration settings is a vital aspect of building robust and scalable applications.]]></description><link>https://noumanbaloch.substack.com/p/understanding-appsettings-json-files</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/understanding-appsettings-json-files</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 02 Sep 2024 07:11:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!eK5Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!eK5Y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!eK5Y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!eK5Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:445459,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!eK5Y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!eK5Y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb78fd90c-f9a9-4018-8072-68d6364a971a_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the world of C# development, especially when working with ASP.NET Core, managing configuration settings is a vital aspect of building robust and scalable applications. One of the key tools for handling these configurations is the <code>appsettings.json</code> file. This article will walk you through what <code>appsettings.json</code> is, how it works, and how to use it effectively across different environments.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developer&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developer</span></a></p><div><hr></div><h4>What is <code>appsettings.json</code>?</h4><p><code>appsettings.json</code> is the primary configuration file used in ASP.NET Core applications. It serves as a centralized location for storing application settings such as connection strings, API keys, logging configurations, and more. By keeping these settings in a JSON file, you can easily manage and modify them without the need to recompile your application.</p><p>Here&#8217;s a basic example of what an <code>appsettings.json</code> file might look like:</p><pre><code>{
  "ConnectionStrings": {
    "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}</code></pre><div><hr></div><h4>Understanding Environment-Specific Configuration</h4><p>ASP.NET Core allows you to create environment-specific versions of the <code>appsettings.json</code> file, such as <code>appsettings.Development.json</code>, <code>appsettings.Staging.json</code>, or <code>appsettings.Production.json</code>. These environment-specific files override the settings in the base <code>appsettings.json</code> file, allowing you to tailor the configuration for different stages of deployment.</p><p>For example, during development, you might connect to a local database with different logging levels. Your <code>appsettings.Development.json</code> might look like this:</p><pre><code>{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=devDb;User Id=devUser;Password=devPassword;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "Microsoft": "Information"
    }
  }
}</code></pre><p>In contrast, your <code>appsettings.Production.json</code> could have a more secure connection string and less verbose logging:</p><pre><code>{
  "ConnectionStrings": {
    "DefaultConnection": "Server=prodServer;Database=prodDb;User Id=prodUser;Password=prodPassword;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning",
      "Microsoft": "Error"
    }
  }

}</code></pre><div><hr></div><h4>How Configuration is Loaded</h4><p>When your ASP.NET Core application starts, it first loads the <code>appsettings.json</code> file. Afterward, it loads the environment-specific file corresponding to the current environment, such as <code>appsettings.Development.json</code>. This process ensures that the application uses the appropriate settings for the environment in which it&#8217;s running.</p><div><hr></div><h4>Setting the Environment</h4><p>The environment is determined by the <code>ASPNETCORE_ENVIRONMENT</code> variable, which can be set in various ways:</p><ol><li><p><strong>Environment Variables:</strong> Set this variable on your server or development machine to <code>Development</code>, <code>Staging</code>, or <code>Production</code>.</p></li><li><p><strong>launchSettings.json:</strong> For local development, you can specify the environment in the <code>launchSettings.json</code> file under the <code>profiles</code> section:</p></li></ol><pre><code>{
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}</code></pre><div><hr></div><h4>Conclusion</h4><p>Effective management of configuration settings is crucial for developing applications that are both flexible and maintainable. By leveraging the <code>appsettings.json</code> file and its environment-specific variants, you can seamlessly transition your application through various stages of development, testing, and production. This approach not only simplifies configuration management but also reduces the risk of errors when deploying to different environments.</p><p>Including environment-specific settings ensures that your app behaves as expected, whether you&#8217;re debugging locally or running in a production environment. Make sure to take full advantage of this powerful feature to keep your applications well-organized and easily configurable.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Class vs Struct vs Record: Definitions and Differences]]></title><description><![CDATA[When designing data structures in C#, understanding the differences between class, struct, and record is essential to making informed decisions.]]></description><link>https://noumanbaloch.substack.com/p/class-vs-struct-vs-record-definitions</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/class-vs-struct-vs-record-definitions</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 26 Aug 2024 07:26:54 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vkds!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vkds!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vkds!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!vkds!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!vkds!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!vkds!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vkds!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:454945,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vkds!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!vkds!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!vkds!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!vkds!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a94ab15-7ecb-47ec-9bba-62708dcbea76_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When designing data structures in C#, understanding the differences between class, struct, and record is essential to making informed decisions. So in this article we will compare these data types and see the differences and use cases of classes, struct and record.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Class</h4><p><strong>Reference Type:</strong> Classes are reference types, meaning they store a reference to the data rather than the data itself.<br><br><strong>Heap Allocation:</strong> Objects are allocated on the heap, and garbage collection is responsible for memory management.<br><br><strong>Use When:</strong> You generally use classes for complex data structures, long-lived objects, or when the size exceeds 16 bytes.</p><p><strong>Example:</strong></p><pre><code>class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}</code></pre><p>Use the keyword class<em> </em>to create a class in C#. You can specify an access modifier (private, public, protected, internal), if required, for the class.</p><div><hr></div><h4>Struct</h4><p><strong>Value Type:</strong> Structs are value types, meaning they store the actual data directly.<br><br><strong>Stack Allocation:</strong> Structs are typically allocated on the stack, which can lead to better performance for small, short-lived instances.<br><br><strong>Use When:</strong> Opt for structs when the data structure is small, immutable, and logically represents a single value. Avoid structs if the data structure is large or mutable.</p><p><strong>Example:</strong></p><pre><code>struct Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}</code></pre><p>Use the struct keyword to declare the struct (structure) in C#. You can use access modifiers, if required.</p><div><hr></div><h4>Records</h4><p><strong>Reference or Value Type:</strong> Records can be either reference types (record class) or value types (record struct).<br><br><strong>Value Equality:</strong> Unlike classes, records emphasize value equality, meaning two records with the same data are considered equal.<br><br><strong>Immutability:</strong> Records are immutable by default, making them ideal for scenarios where the data should not change after creation.<br><br><strong>Use When:</strong> Use records for immutable data structures, especially when you want concise syntax, like in request and response DTOs.</p><p><strong>Example:</strong></p><pre><code>public record Person( string Name, int Age );</code></pre><div><hr></div><h4>When to Use Which</h4><p><strong>Use Class:</strong> For most scenarios, especially when dealing with complex objects or when the instance size exceeds 16 bytes.<br><br><strong>Use Struct:</strong> For small, simple, and immutable data structures, particularly when performance is a concern.<br><br><strong>Use Record:</strong> For immutable data with value equality, such as DTOs, or when you want the convenience of built-in methods for deconstruction and printing.</p><div><hr></div><h4>Conclusion:</h4><p>In conclusion, choosing between classes, structs, and records in C# depends on your specific use case. Classes are best for complex and long-lived objects, structs for small, simple, and performance-critical data, and records for immutable data structures with value equality. Understanding these distinctions allows you to optimize your code for performance, maintainability, and clarity.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Understanding [FromForm], [FromBody], [FromQuery], [FromRoute], and [FromHeader] in ASP.NET Core]]></title><description><![CDATA[When building APIs in ASP.NET Core, understanding how to bind incoming data to your action parameters is crucial.]]></description><link>https://noumanbaloch.substack.com/p/understanding-fromform-frombody-fromquery</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/understanding-fromform-frombody-fromquery</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 19 Aug 2024 04:01:31 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!fC5t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fC5t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fC5t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fC5t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:456053,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fC5t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!fC5t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fba170025-fb75-4e13-b878-536cf0494c95_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>When building APIs in <a href="http://asp.net/">ASP.NET</a> Core, understanding how to bind incoming data to your action parameters is crucial.  So in this article we will see the difference between all these attributes.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/HcCb9rdJKuk7cVUpIUTSAs"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>[FromForm]</h4><p>Use [FromForm] when you want to bind data submitted as part of a form, typically via `multipart/form-data` or `application/x-www-form-urlencoded`. This is often used when handling form submissions in web applications.</p><pre><code><code>public IActionResult SubmitData([FromForm] MyFormModel model)</code></code></pre><p><strong>Example:</strong> Uploading a file with additional form data.</p><div><hr></div><h4>[FromBody]</h4><p>[FromBody] is used to bind data from the body of the request, typically in JSON or XML format. This is common in API endpoints that accept complex objects or raw data.</p><pre><code><code>public IActionResult CreateItem([FromBody] MyItemModel item)</code></code></pre><p><strong>Example:</strong> Posting JSON data to create a new resource.</p><div><hr></div><h4>[FromQuery]</h4><p>[FromQuery] binds data from query string parameters in the URL. This is useful for filtering, sorting, or searching operations where data is passed via the query string.</p><pre><code>public IActionResult GetItems([FromQuery] string filter)</code></pre><p><strong>Example:</strong> `GET /items?filter=active`</p><div><hr></div><h4>[FromRoute]</h4><p>Use [FromRoute] when you want to bind data directly from the route parameters in the URL. This is often used for identifying resources.</p><pre><code>public IActionResult GetItem([FromRoute] int id)</code></pre><p><strong>Example:</strong> `GET /items/123` where `123` is the ID of the item.</p><div><hr></div><h4>[FromHeader]</h4><p>[FromHeader] allows you to bind data from HTTP headers. This is useful for scenarios where you need to pass metadata, such as authentication tokens or custom headers. </p><pre><code>public IActionResult GetData([FromHeader(Name = "X-Custom-Header")] string headerValue)</code></pre><p><strong>Example:</strong> Passing an API key via a custom header like `X-API-Key`.</p><div><hr></div><h4>Why Does This Matter? &#129300;</h4><p>Choosing the right attribute helps make your API clear, predictable, and easy to use. It ensures that your application correctly interprets where data is coming from, making your code more maintainable and reducing the risk of bugs.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Dapper Repository Implementation for Store Procedure Calls in .NET 8]]></title><description><![CDATA[Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications.]]></description><link>https://noumanbaloch.substack.com/p/dapper-repository-implementation</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/dapper-repository-implementation</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 04 Mar 2024 09:30:33 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Aa69!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Aa69!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Aa69!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Aa69!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:377827,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Aa69!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!Aa69!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe949b700-8905-4d82-a3d3-a6295062e04b_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications. The library allows developers quickly and easily access data from databases without the need to write tedious code. Dapper allows you to execute raw SQL queries, map the results to objects, and execute stored procedures. In this article we will see the implementation of generic dapper repository to call the store procedures.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Dapper Repository Interface</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!nfAY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!nfAY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 424w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 848w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 1272w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!nfAY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png" width="1456" height="387" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/af5db90b-675f-41c8-a794-67f33a78e629_2560x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:387,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:222072,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!nfAY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 424w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 848w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 1272w, https://substackcdn.com/image/fetch/$s_!nfAY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faf5db90b-675f-41c8-a794-67f33a78e629_2560x680.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Place this interface inside domain layer or in application layer.</p><div><hr></div><h4>Connection Factory Interface</h4><p> Place this interface inside domain layer or in application layer beside your dapper repository interface. This connection factory is responsible for creating the connection with database</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!900y!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!900y!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 424w, https://substackcdn.com/image/fetch/$s_!900y!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 848w, https://substackcdn.com/image/fetch/$s_!900y!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 1272w, https://substackcdn.com/image/fetch/$s_!900y!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!900y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png" width="902" height="362" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:362,&quot;width&quot;:902,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:33426,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!900y!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 424w, https://substackcdn.com/image/fetch/$s_!900y!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 848w, https://substackcdn.com/image/fetch/$s_!900y!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 1272w, https://substackcdn.com/image/fetch/$s_!900y!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fac591bee-4a8c-4e62-9100-188d3d81bba6_902x362.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Implementation of Connection Factory Interface</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qRxL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qRxL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 424w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 848w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 1272w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qRxL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png" width="1444" height="1050" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1050,&quot;width&quot;:1444,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:141817,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qRxL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 424w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 848w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 1272w, https://substackcdn.com/image/fetch/$s_!qRxL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F555da2f7-c1b1-4ca1-8780-3c2898aa4ba6_1444x1050.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Place this implementation inside your infrastructure project or maybe create a separate persistence project inside infrastructure and put this there.</p><div><hr></div><h4>Implementation of Dapper Repository</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ScRc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ScRc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 424w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 848w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 1272w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ScRc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png" width="1456" height="2214" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:2214,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:967311,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ScRc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 424w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 848w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 1272w, https://substackcdn.com/image/fetch/$s_!ScRc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a2c56c-3de2-4f79-a4ef-d9108182700e_2560x3892.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Place this implementation inside your infrastructure project or maybe create a separate persistence project inside infrastructure and put this there.</p><div><hr></div><h4>Dependency Injection</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!sTnW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!sTnW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 424w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 848w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!sTnW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png" width="1456" height="765" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:765,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:209292,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!sTnW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 424w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 848w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 1272w, https://substackcdn.com/image/fetch/$s_!sTnW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa66773f1-64ff-4226-bae7-ae1cd0d95508_2102x1104.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Difference Between IOptions, IOptionsSnapshot, IOptinosMonitor]]></title><description><![CDATA[The Options pattern uses classes to provide strongly typed access to groups of related settings and helps to satisfy Encapsulation and Separation of concerns principals .options also provide a mechanism to validate configuration data.]]></description><link>https://noumanbaloch.substack.com/p/difference-between-ioptions-ioptionssnapshot</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/difference-between-ioptions-ioptionssnapshot</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 26 Feb 2024 20:13:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!uuRD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uuRD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uuRD!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uuRD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:391314,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uuRD!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!uuRD!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c4e5feb-11b6-489f-b0fc-897e5deabcb6_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The Options pattern uses classes to provide strongly typed access to groups of related settings and helps to satisfy Encapsulation and Separation of concerns principals .options also provide a mechanism to validate configuration data. So when working with options pattern it provide three interfaces to inject the configuration into your services. IOptions, IOptionsSnapshot, IOptinosMonitor, so in this article we will talk about the difference between these interfaces.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><p>&#119816;&#119822;&#119849;&#119853;&#119842;&#119848;&#119847;&#119852; &#119842;&#119847;&#119853;&#119838;&#119851;&#119839;&#119834;&#119836;&#119838;<br>The lifetime of the IOptions interface is Singleton, and you can register it in any service lifetime. It has one limitation: when you start your app, and after starting, you change something in your configuration, it will not pick up the new configuration.</p><div><hr></div><p>&#119816;&#119822;&#119849;&#119853;&#119842;&#119848;&#119847;&#119852;&#119826;&#119847;&#119834;&#119849;&#119852;&#119841;&#119848;&#119853; &#119842;&#119847;&#119853;&#119838;&#119851;&#119839;&#119834;&#119836;&#119838;<br>The lifetime of the IOptionsSnapshot interface is Scoped because it is scoped; therefore, you cannot use this in your Singleton services. It is useful in scenarios where options should be recomputed on every request.</p><div><hr></div><p>&#119816;&#119822;&#119849;&#119853;&#119842;&#119848;&#119847;&#119852;&#119820;&#119848;&#119847;&#119842;&#119853;&#119848;&#119851; &#119842;&#119847;&#119853;&#119838;&#119851;&#119839;&#119834;&#119836;&#119838;<br>The lifetime of the IOptionsMonitor interface is Singleton, and you can register it in any service lifetime, just like the IOptions interface.<br>However, IOptionsMonitor can read the latest configuration from your app settings even after you start your app; this is the major difference between the IOptions and IOptionsMonitor interfaces. The name of the property that stores the config value is CurrentValue instead of Value.</p><div><hr></div><h4>Quick look at IOptions, IOptionsSnapshot, IOptinosMonitor</h4><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dWaN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dWaN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dWaN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg" width="898" height="382" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:382,&quot;width&quot;:898,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:36046,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!dWaN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 424w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 848w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!dWaN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fae035ece-af37-424b-91e1-edf5a95c8978_898x382.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Immediate Vs Deferred Query Execution In LINQ]]></title><description><![CDATA[When working with data in C#, understanding how your queries are executed is crucial for writing efficient and predictable code.]]></description><link>https://noumanbaloch.substack.com/p/immediate-vs-deferred-query-execution</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/immediate-vs-deferred-query-execution</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 19 Feb 2024 09:20:20 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!v7Ug!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!v7Ug!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!v7Ug!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!v7Ug!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:362137,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!v7Ug!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!v7Ug!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25c95bd5-d711-41ea-b8cc-7dd817f68e38_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>When working with data in C#, understanding how your queries are executed is crucial for writing efficient and predictable code. LINQ (Language Integrated Query) introduces two key concepts in this regard: deferred execution and immediate execution. So in this article we will talk about how deferred query execution and Immediate Query Execution works in LINQ, and the difference between the two.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Deferred Execution</h4><p>In case of differed execution, a query is not executed at the point of its declaration. It is executed when the Query variable is iterated by using loop for, foreach etc.</p><p>Think of deferred execution like making a shopping list. You write down everything you need, but you don't physically grab anything from the shelves until you reach the checkout. Similarly, in LINQ, deferred execution defines the operations needed on your data but doesn't actually retrieve it until required. This is especially useful when working with large datasets or performing multiple operations on the same data source.</p><div><hr></div><h4>Coding Example</h4><p><code>IEnumerable&lt;int&gt; numbers = Enumerable.Range(1, 100);</code></p><p><code>var filteredNumbers = numbers.Where(n =&gt; n % 2 == 0);</code></p><p><code>// Nothing is executed yet!</code></p><p><code>Console.WriteLine(filteredNumbers.Count()); // Now the query executes</code></p><div><hr></div><h4>Immediate Execution</h4><p>In case of immediate execution, a query is executed at the point of its declaration. The query which returns a singleton value (single value or a set of values) like Average, Sum, Count, List etc. caused Immediate Execution.</p><p>You can force a query to execute immediately of by calling ToList, ToArray methods.</p><p>Think of immediate execution like going straight to the shelves and fetching all items on your list, even if you don't need them immediately. In LINQ, immediate execution forces the query to be evaluated right away, returning the result as a data structure like a list or array.</p><div><hr></div><h4>Coding Example</h4><p><code>List&lt;int&gt; evenNumbers = numbers.Where(n =&gt; n % 2 == 0).ToList();</code></p><p><code>// The query and filtering are executed immediately</code></p><p><code>Console.WriteLine(evenNumbers[0]); // Accessing the first even number</code></p><div><hr></div><h4>Choosing the Right Approach: When to Go Which Way</h4><p>Deciding between deferred and immediate execution depends on your specific use case:</p><ul><li><p><strong>Use deferred execution for:</strong></p><ul><li><p>Large datasets where you only need parts of the data.</p></li><li><p>Performing multiple operations on the same data source.</p></li><li><p>Avoiding unnecessary processing overhead.</p></li></ul></li><li><p><strong>Use immediate execution for:</strong></p><ul><li><p>When you need all the data upfront for further processing.</p></li><li><p>Modifying the query results with additional LINQ operations.</p></li><li><p>Debugging purposes to inspect the actual data returned.</p></li></ul></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[SOLID Engineering Principles]]></title><description><![CDATA[SOLID is an acronym for the five principles made popular by Robert C.]]></description><link>https://noumanbaloch.substack.com/p/solid-engineering-principles</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/solid-engineering-principles</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 12 Feb 2024 09:30:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!hYIQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hYIQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hYIQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hYIQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:387939,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hYIQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!hYIQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F44027406-430c-4389-b89f-c969059fb0f6_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>SOLID is an acronym for the five principles made popular by Robert C. Martin (also known as Uncle Bob).</p><p>SOLID stands for:</p><ul><li><p><strong>S</strong> - Single-responsibility Principle</p></li><li><p><strong>O</strong> - Open-closed Principle</p></li><li><p><strong>L</strong> - Liskov Substitution Principle</p></li><li><p><strong>I</strong> - Interface Segregation Principle</p></li><li><p><strong>D</strong> - Dependency Inversion Principle</p></li></ul><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Single-Responsibility Principle</h4><p>A class should have one and only one reason to change, meaning that a class should have only one job.</p><p>This principle aims to separate behaviours so that if bugs arise as a result of your change, it won&#8217;t affect other unrelated behaviours.</p><div><hr></div><h4>Open-Closed Principle</h4><p>Objects or entities should be open for extension but closed for modification.</p><p>This principle aims to extend a Class&#8217;s behaviour without changing the existing behaviour of that Class. This is to avoid causing bugs wherever the Class is being used.</p><div><hr></div><h4>Liskov Substitution Principle</h4><p>If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program.</p><p>This principle aims to enforce consistency so that the parent Class or its child Class can be used in the same way without any errors.</p><div><hr></div><h4>Interface Segregation</h4><p>A client should never be forced to implement an interface that it doesn&#8217;t use, or clients shouldn&#8217;t be forced to depend on methods they do not use.</p><p>This principle aims at splitting a set of actions into smaller sets so that a Class executes ONLY the set of actions it requires.</p><div><hr></div><h4>Dependency Inversion</h4><p>High-level modules should not depend on low-level modules. Both should depend on the abstraction. Abstractions should not depend on details. Details should depend on abstractions.</p><p><strong>High-level Module(or Class)</strong>: Class that executes an action with a tool.</p><p><strong>Low-level Module (or Class)</strong>: The tool that is needed to execute the action</p><p><strong>Abstraction</strong>: Represents an interface that connects the two Classes.</p><p>This principle aims at reducing the dependency of a high-level Class on the low-level Class by introducing an interface.</p><div><hr></div><h4>Conclusion</h4><p>To sum it up, SOLID principles are like a rulebook for writing good code. They help make software easier to understand, modify, and fix. By sticking to these principles, developers can create programs that are less likely to have problems and are simpler to work with in the long run. It's all about making code that just makes sense and works well.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[DRY, YAGNI, KISS Engineering Principles]]></title><description><![CDATA[DRY, YAGNI, KISS are nothing but set of guidelines that help you write clean, readable and more maintainable code.]]></description><link>https://noumanbaloch.substack.com/p/dry-yagni-kiss-engineering-principles</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/dry-yagni-kiss-engineering-principles</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 05 Feb 2024 09:31:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!AYlu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!AYlu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!AYlu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!AYlu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:371559,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!AYlu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!AYlu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2e5af109-0360-4e2d-8719-70550e99c788_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>DRY, YAGNI, KISS are nothing but set of guidelines that help you write clean, readable and more maintainable code. These set of guidelines proven to work over the years. So in this article we will understand these principles but even before that we will first discuss why you should learn this principles at the first place.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Why you should learn these principles?</h4><p>These principles are a set of guidelines that help you write better, robust, and clean code. Once, you start applying them, you&#8217;ll start seeing the results on the overall process of developing products. Here are some advantages which will be visible in your code -</p><ul><li><p>Robustness</p></li><li><p>Efficiency</p></li><li><p>Delivering high-quality software</p></li><li><p>Flexible, Easy to refactor</p></li></ul><div><hr></div><h4>DRY ( Don&#8217;t Repeat Yourself)</h4><p>As the name suggest <strong>Don&#8217;t Repeat Yourself, </strong>This principle focuses on applying reusability and reducing repetition in code. If you are reusing your code you are reducing the repetition and you are producing less code, less code is better then more code.</p><p>The most common example of DRY is functions! A general rule is if you are writing the same logic twice, it&#8217;s time to refactor the logic to a function and reuse it! </p><div><hr></div><h4><strong>YAGNI (You Aren't Gonna Need It)</strong></h4><p>This principle focuses that always implement things when you actually need them, never when you just foresee that you need them.</p><p>Even if you're super sure that you'll need a feature, later on, don't implement it now. Usually, it'll turn out either:</p><ul><li><p>you don't need it after all, or</p></li><li><p>what you actually need is quite different from what you foresaw needing earlier.</p></li></ul><p>This doesn't mean you should avoid building flexibility into your code. It means you shouldn't over engineer something based on what you think you might need later on.</p><p><strong>Premature Optimization is the Root of All Evil</strong></p><p>Programmers waste enormous amounts of time thinking about or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.</p><div><hr></div><h4>KISS (Keep It Simple Stupid)</h4><p>The KISS principle states that most systems work best if they are kept simple rather than made complicated; therefore, simplicity should be a key goal in design, and unnecessary complexity should be avoided. </p><p>This principle literally means to keep your code as simple code as possible. The simpler the code, the easier it is to understand and maintain it.</p><p>Simple code has the following benefits:</p><ul><li><p>Less time to write</p></li><li><p>Less chances of bugs</p></li><li><p>easier to understand, debug and modify</p></li></ul><div><hr></div><h4>Conclusion</h4><p>In conclusion, implementing the engineering principles of DRY, YAGNI, and KISS in your applications enhances code quality and maintainability. These guidelines promote reusability,  and simplicity, leading to efficient, high-quality software. By following these principles, developers can create cleaner, more resilient code and streamline the development process.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Dictionary Vs Concurrent Dictionary in .NET 8]]></title><description><![CDATA[Well we all know that a dictionary in C# is a generic collection that can be used to store key/value pairs, where each key is unique within the dictionary, and it maps to a specific value.]]></description><link>https://noumanbaloch.substack.com/p/dictionary-vs-concurrentdictionary</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/dictionary-vs-concurrentdictionary</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 29 Jan 2024 06:13:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!uZTM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uZTM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uZTM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uZTM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9499763-615a-426d-a53e-e233b859c672_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:406507,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uZTM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!uZTM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc9499763-615a-426d-a53e-e233b859c672_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Well we all know that a dictionary in C# is a generic collection that can be used to store key/value pairs, where each key<strong> </strong>is unique<strong> </strong>within the dictionary, and it maps to a specific value. But when we come to know that concurrent dictionary is also a thing then most of us got confused between dictionary and concurrent dictionary. So in this article we will understand the difference between dictionary and concurrent dictionary, also we will talk about that in which scenario you should use standard dictionary and when you should use concurrent dictionary.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>What is Dictionary?</h4><p>A dictionary is essentially a generic collection of key-value pairs, where each key corresponds to a unique value. You can draw an analogy between a dictionary in programming and a real-world dictionary, where you look up a word (key) to find its definition (value).</p><p>The `Dictionary&lt;TKey, TValue&gt;` class in .NET allows you to define both the data type for keys (`TKey`) and the data type for values (`TValue`). For instance, you can create a dictionary with integers as keys and strings as values, denoted as `Dictionary&lt;int, string&gt;`. It provides a structured way to organize and retrieve data.</p><p>It's important to note that in a `Dictionary`, keys must be unique, ensuring that each key is associated with a single value. Additionally, keys should not be null for reference types. On the other hand, values can be null, especially if they are of a reference type.</p><div><hr></div><h4>What is Concurrent Dictionary?</h4><p>A `ConcurrentDictionary` is a thread-safe collection of key-value pairs, providing a mechanism for multiple threads to access and modify the data concurrently while ensuring data integrity. This class is specifically designed to handle scenarios where multiple threads might simultaneously access, add, or modify entries in the dictionary.</p><p>The primary goal of the `ConcurrentDictionary&lt;TKey, TValue&gt;` class is to prevent race conditions, a common issue that arises when two or more threads attempt to access and modify shared data simultaneously. Race conditions can lead to unpredictable behavior, including data corruption, incorrect results, or exceptions.</p><div><hr></div><h4>What you should use Dictionary and when Concurrent Dictionary?</h4><p>ConcurrentDictionary is primarily for use in an environment where you're updating the dictionary from multiple threads (or async tasks). You can use a standard Dictionary from as much code as you like if it's from a single thread.</p><p>If you look at the methods on a ConcurrentDictionary, you'll spot some interesting methods like TryAdd, TryGetValue, TryUpdate, and TryRemove.</p><p>For example, consider a typical pattern you might see for working with a normal Dictionary class.</p><pre><code><code>if (!dictionary.ContainsKey(id))
    dictionary.Add(id, value);</code></code></pre><p>This has an issue in that between the check for whether it contains a key and calling Add a different thread could call Add with that same id. When this thread calls Add, it'll throw an exception. The method TryAdd handles that for you and will return a true/false telling you whether it added it (or whether that key was already in the dictionary).</p><p>So unless you're working in a multi-threaded section of code, you probably can just use the standard Dictionary class. That being said, you could theoretically have locks to prevent concurrent access to a dictionary.</p><div><hr></div><h4>Real-World Use Case of a Dictionary: Based on My Experience</h4><p>In a project I worked on, I stored unique device IDs from mobile devices in a standard dictionary. All API calls were asynchronous, and upon receiving a request, I checked whether the device ID existed in the standard dictionary. If it did, I approved the request; otherwise, I fetched the device ID from the database and stored it in the dictionary.</p><p>However, I encountered a wired  issue where, at times, the mobile client would throw an <code>ArgumentException</code> stating "Item with the same key has already been added." To address this issue, I replaced the standard dictionary with a concurrent dictionary. This change resolved the problem, ensuring that concurrent access to the dictionary by multiple threads did not result in key conflicts or exceptions. The concurrent dictionary provided the necessary thread safety, preventing simultaneous additions or modifications to the dictionary from causing errors.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Concurrency VS Parallelism in .NET 8]]></title><description><![CDATA[Concurrency and Parallelism are two concepts that are often used interchangeably but have distinct meanings and implications.]]></description><link>https://noumanbaloch.substack.com/p/concurrency-vs-parallelism-in-net</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/concurrency-vs-parallelism-in-net</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 22 Jan 2024 05:00:18 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!CShF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!CShF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!CShF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!CShF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!CShF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!CShF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!CShF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:408299,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!CShF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!CShF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!CShF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!CShF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b3a46bb-0daf-4ad2-a0ee-35960a8d7f8c_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Concurrency and Parallelism are two concepts that are often used interchangeably but have distinct meanings and implications. So in this article we will understand the core of these concepts, also we will talk about advantages and key differences between concurrency and parallelism.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Understanding the Core Differences:</h4><p><strong>Concurrency: </strong>is a condition that exists when at least two tasks can start, run, and complete in overlapping time periods. Concurrency is the illusion of parallelism, it feels like parallel but actually it is not parallel. For example, multitasking on a single-core machine is a form of concurrency, where the processor switches between tasks at a very fast rate, creating the illusion of parallelism. Async/Await in C# is an example of concurrency. </p><p>Imagine a juggling act. You can only hold one ball at a time, but you can rapidly switch between them, creating the illusion of juggling multiple balls simultaneously. This is concurrency in action.</p><p><strong>Parallelism:</strong> is a condition that arises when at least two tasks are executing simultaneously. It means that they are literally running at the same time.Task Parallel Library (TPL) in C# is an example of parallelism where you can pass your Tasks to this WhenAll method and those tasks run at the same time.</p><p>Imagine a real-life juggling act with multiple jugglers. Each juggler can handle multiple balls independently and simultaneously. This is parallelism in action.</p><div><hr></div><h4>Advantages:</h4><p><strong>Concurrency: </strong></p><ul><li><p>In the context of WEB API&#8217;s concurrency increase the throughput of your API, which mean you can handle more requests. Like for example if one request come to you and now you hit the DB and your CPU is waiting for response from DB during that time you can handle other requests.</p></li><li><p>In the context of UI Based Apps concurrency increase the responsiveness &amp; interactivity of your application by allowing tasks to run in the background without blocking the user interface.</p></li></ul><p><strong>Parallelism:</strong></p><ul><li><p>Improving the performance and efficiency of the application by utilizing the full potential of the hardware resources. Because now you are actually doing multiple things at the same time.</p></li><li><p>It also reduce the latency in the response, for example if there are two tasks each take 5 seconds total 10 seconds to complete but now because they are running in parallel then instead of taking 10 seconds to complete both task will complete in total 5 seconds.</p></li></ul><div><hr></div><h4>Key difference between concurrency and Parallelism:</h4><p><strong>Concurrency:</strong> mean utilizing your resources efficiently where two tasks can start run and complete at overlapping time. It is like one juggler juggling between balls.</p><p><strong>Parallelism:</strong> mean actually doing multiple things at the same time where two task can start run and complete at same time. It is like two jugglers juggling the balls.</p><p><strong>Concurrency</strong> aims to improve the responsiveness and interactivity of the application, </p><p><strong>Parallelism: </strong> aims to improve the speed and efficiency of the application.</p><div><hr></div><h4><strong>C# Examples of concurrency and parallelism:</strong></h4><p><strong>Example of concurrency:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!7goY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!7goY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 424w, https://substackcdn.com/image/fetch/$s_!7goY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 848w, https://substackcdn.com/image/fetch/$s_!7goY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 1272w, https://substackcdn.com/image/fetch/$s_!7goY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!7goY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png" width="1456" height="404" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:404,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:115159,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!7goY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 424w, https://substackcdn.com/image/fetch/$s_!7goY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 848w, https://substackcdn.com/image/fetch/$s_!7goY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 1272w, https://substackcdn.com/image/fetch/$s_!7goY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89237e30-ec31-4a04-b491-2c8a8d7dcdcc_2070x574.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong>Example of parallelism:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vv7D!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vv7D!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 424w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 848w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 1272w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vv7D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png" width="1456" height="461" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:461,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:94582,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!vv7D!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 424w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 848w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 1272w, https://substackcdn.com/image/fetch/$s_!vv7D!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffb1f8533-de81-437e-8a40-ebf1fa779621_1650x522.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Conclusion:</h4><p>In conclusion, while concurrency and parallelism are often used interchangeably, they represent distinct concepts. Concurrency creates the illusion of simultaneous execution, beneficial for responsive user interfaces and multitasking. On the other hand, parallelism achieves true simultaneous execution, leveraging multiple cores or distributed systems for enhanced speed and efficiency. In C#, asynchronous programming exemplifies concurrency, while the Task Parallel Library showcases parallelism. The choice between these approaches depends on the specific goals, with concurrency enhancing user experience and parallelism maximizing computational power. Balancing these concepts ensures the development of robust and high-performing software systems.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[7 Best Practices to Handle Exceptions in .NET 8 ]]></title><description><![CDATA[A well designed application handle exceptions and errors gracefully and prevent app to crash. So in this article we will talk about 7 best practices to handle exceptions in proper way. 1) Use a try/catch/finally block: Use try/catch/finally to handle exceptions gracefully. The try block contains the code that might throw an exception, the catch block catches and handles the exception, and the finally block ensures that essential cleanup code is executed regardless of whether an exception occurred.]]></description><link>https://noumanbaloch.substack.com/p/7-best-practices-to-handle-exceptions</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/7-best-practices-to-handle-exceptions</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Mon, 15 Jan 2024 04:00:27 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/29a3695d-f120-4412-ae90-d201388a2192_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5MMT!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5MMT!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5MMT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png" width="1456" height="1048" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:377485,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5MMT!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!5MMT!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdc1958e1-c7ed-4310-990c-34722af33d83_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A well designed application handle exceptions and errors gracefully and prevent application to crash. So in this article we will talk about 7 best practices to handle exceptions in a proper way.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>1) Use a try/catch/finally block:</h4><p>Use try/catch/finally to handle exceptions gracefully. The try block contains the code that might throw an exception, the catch block catches and handles the exception, and the finally block ensures that essential cleanup code is executed regardless of whether an exception occurred.</p><h4>2) Handle common conditions without throwing exception:</h4><p>Avoid using exceptions for flow control. Instead, handle common conditions with if statements or other appropriate checks. This improves performance and makes the code more readable, as exceptions should be reserved for exceptional and unexpected situations.</p><h4>3) Design classes in a way that avoids exceptions:</h4><p>Design classes with preventative methods or properties to mitigate the need for exceptions. For instance, a class can offer methods or properties, such as those found in the FileStream class, that allow users to check conditions before making a call that might lead to an exception. By incorporating such checks, classes can provide a more robust and error-resistant interface, minimizing reliance on exceptions for routine error handling scenarios.</p><h4>4) Throw exceptions and log the details:</h4><p>When throwing exceptions, log the details to aid in debugging. Store logs in a database, blog storage, or a file server for future analysis. This ensures that developers can investigate issues effectively and make informed improvements to the code.</p><h4>5) Do not return sensitive information from exceptions to clients:</h4><p>Avoid returning sensitive information in exception messages, as this can pose security risks. Instead, log detailed information for internal use, while providing generalized and user-friendly error messages to clients.</p><h4>6) Implement global exception handling middleware:</h4><p>Implement a global exception handling middleware in your .NET application to capture unhandled exceptions at a centralized level. This allows you to log the exceptions, notify administrators, and gracefully handle errors to provide a better user experience.</p><h4><strong>7) Throw Specific Exceptions:</strong> </h4><p>Be specific when throwing exceptions. Instead of using generic exceptions, create custom exception classes that convey the nature of the problem. This aids in more precise error handling and makes it easier for developers to identify and address issues in the code.</p><div><hr></div><h4>Conclusion:</h4><p>In conclusion, effective exception handling in .NET involves a combination of proactive design and thoughtful practices. Utilizing try/catch/finally blocks, handling common conditions without resorting to exceptions, and designing classes with preventative methods exemplify the importance of minimizing the reliance on exceptions for routine error scenarios. Additionally, logging exception details, implementing global exception handling middleware, and being specific in exception types contribute to a comprehensive approach for managing errors. By adhering to these principles, developers can enhance code robustness, maintainability, and user experience in .NET applications.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts right into your mailbox.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[How to Implement a Generic HttpClient Wrapper in .NET 8]]></title><description><![CDATA[Why we need generic HttpClient Wrapper? In .NET, making HTTP calls to 3rd-party APIs and services is an easy task with the help of HttpClient. However, when it comes to production, careful consideration is necessary before incorporating HttpClient directly into the codebase. Injecting HttpClient everywhere and making external API calls without proper management can be problematic. Therefore, it is advisable to use a generic wrapper to handle external API calls.]]></description><link>https://noumanbaloch.substack.com/p/how-to-implement-a-generic-httpclient</link><guid isPermaLink="false">https://noumanbaloch.substack.com/p/how-to-implement-a-generic-httpclient</guid><dc:creator><![CDATA[Nouman Baloch]]></dc:creator><pubDate>Sun, 07 Jan 2024 12:04:54 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/62270bf7-5c62-45da-9d20-b7bbf7f82efa_1456x1048.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DMvG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DMvG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DMvG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png" width="728" height="524" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:1048,&quot;width&quot;:1456,&quot;resizeWidth&quot;:728,&quot;bytes&quot;:357531,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DMvG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 424w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 848w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 1272w, https://substackcdn.com/image/fetch/$s_!DMvG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81091219-0f9d-49aa-be8b-0c265d684bc8_1456x1048.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h4>Why we need generic HttpClient Wrapper?</h4><p>In .NET, making HTTP calls to 3rd-party APIs and services is an easy task with the help of HttpClient. However, when it comes to production, careful consideration is necessary before incorporating HttpClient directly into the codebase. Injecting HttpClient everywhere and making external API calls without proper management can be problematic. Therefore, it is advisable to use a generic wrapper to handle external API calls.</p><p>The Generic HttpClient Wrapper serves as a solution in such scenarios. This wrapper encapsulates all HttpClient logic, providing a clean and organized approach. By using this wrapper in our services, we can enhance the maintainability and readability of the codebase.</p><div><hr></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H&quot;,&quot;text&quot;:&quot;Join WhatsApp Group of .NET Developers&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://chat.whatsapp.com/CEsiHV4opt3FKxHVM71e1H"><span>Join WhatsApp Group of .NET Developers</span></a></p><div><hr></div><h4>Let's dive into the code of HttpClient Wrapper and explore its usage:</h4><p><strong>Interface:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!wxoK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!wxoK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 424w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 848w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 1272w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!wxoK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png" width="1456" height="266" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:84296,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!wxoK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 424w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 848w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 1272w, https://substackcdn.com/image/fetch/$s_!wxoK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F61d42d21-b5ee-417e-b0a2-c14b262231fd_2280x416.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><strong>Class:</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_nYG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_nYG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 424w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 848w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 1272w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_nYG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png" width="1456" height="1320" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1320,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:504425,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!_nYG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 424w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 848w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 1272w, https://substackcdn.com/image/fetch/$s_!_nYG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa384be47-2f60-473f-92aa-951dabcaa02a_2560x2320.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><h4>Let&#8217;s understand this code step by step:</h4><p><strong>Constructor</strong>: The class has a constructor that takes an instance of the HttpClient class. This is important for dependency injection, allowing the class to use an existing HttpClient instance.</p><p><strong>GetAsync Method</strong>: This method performs an HTTP GET request. It takes a generic type for the expected response (TResponse) and an optional request object (TRequest). It constructs the URL with an optional query string based on the provided request. If the HTTP response indicates an error, it throws an exception. Otherwise, it reads the response body and deserializes it into the specified response type using Newtonsoft.Json.</p><p><strong>PostAsync Method</strong>: Similar to GetAsync, this method performs an HTTP POST request. It takes a generic type for the expected response (TResponse), a request object (TRequest), and a content type. It serializes the request object into JSON and sends it as the request content. If the HTTP response indicates an error, it throws an exception. Otherwise, it reads the response body and deserializes it into the specified response type using Newtonsoft.Json.</p><div><hr></div><h4>Register your HttpClient Wrapper into your DI container.</h4><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ulzR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ulzR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 424w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 848w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 1272w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ulzR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png" width="1446" height="204" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:204,&quot;width&quot;:1446,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:25213,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ulzR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 424w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 848w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 1272w, https://substackcdn.com/image/fetch/$s_!ulzR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdea264-e7a3-45f2-90fd-c0773c905996_1446x204.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div><hr></div><h4>Where to put your HttpClient Wrapper in your Clean Architecture?</h4><p>HttpClient is external concern so put the Wrapper class in your infrastructure project &amp; your Interface of HttpClient Wrapper will go into your application or domain layer.</p><div><hr></div><h4>Benefits:</h4><ul><li><p><strong>Reduced Code Duplication:</strong> Encapsulate common HTTP request logic in one place.</p></li><li><p><strong>Improved Maintainability:</strong> Easily modify and extend functionalities in a centralised class.</p></li><li><p><strong>Type Safety and Clarity:</strong> Generic methods ensure correct return types and promote consistent usage.</p></li><li><p><strong>Reusable Building Block:</strong> Leverage the wrapper across different parts of your application.</p></li></ul><div><hr></div><h4>Additional Considerations:</h4><ul><li><p><strong>Logging:</strong> Implement logging for request/response details and improved debugging.</p></li><li><p><strong>Retries and Timeouts:</strong> Consider retrying failed requests and setting timeouts for better control.</p></li><li><p><strong>Authentication:</strong> Integrate authentication strategies for secure API access.</p></li><li><p><strong>Further Customisation:</strong> Expand functionalities like header manipulation, custom serialisation, and response validation.</p></li></ul><div><hr></div><h4>Conclusion:</h4><p>In summary, the Generic HttpClient Wrapper in .NET offers a systematic approach to manage HTTP calls, ensuring code cleanliness and maintainability, especially when interacting with external APIs. By encapsulating HttpClient logic, it minimises code duplication, promotes consistency, and facilitates easier modifications. Placing the wrapper in the infrastructure layer aligns with Clean Architecture principles. The wrapper's benefits, including reduced redundancy and enhanced re-usability, make it a valuable tool for developers, though attention to aspects like logging and customisation is crucial for optimal functionality. Overall, it serves as a key component for efficient and organised external API integration in applications.</p><div><hr></div><h4>Visit this github repo if you want to get the code</h4><p><a href="https://github.com/noumanbaloch/HttpClientWrapperDemo">https://github.com/noumanbaloch/HttpClientWrapperDemo</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://noumanbaloch.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Weekly .NET Pulse! Subscribe for free to receive new posts right into your mailbox.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item></channel></rss>