<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Salona Sinha on Medium]]></title>
        <description><![CDATA[Stories by Salona Sinha on Medium]]></description>
        <link>https://medium.com/@salonasinha?source=rss-255eb5deead5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*282JAy0En35MRPdf</url>
            <title>Stories by Salona Sinha on Medium</title>
            <link>https://medium.com/@salonasinha?source=rss-255eb5deead5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 17:39:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@salonasinha/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Builder Pattern in ASP .NET]]></title>
            <link>https://medium.com/@salonasinha/builder-pattern-in-asp-net-87a7ab270e9f?source=rss-255eb5deead5------2</link>
            <guid isPermaLink="false">https://medium.com/p/87a7ab270e9f</guid>
            <category><![CDATA[builder-pattern]]></category>
            <category><![CDATA[aspnet]]></category>
            <dc:creator><![CDATA[Salona Sinha]]></dc:creator>
            <pubDate>Wed, 03 Jul 2024 10:11:19 GMT</pubDate>
            <atom:updated>2024-07-03T10:11:19.736Z</atom:updated>
            <content:encoded><![CDATA[<h3>Builder Pattern in ASP .NET</h3><h3>Intent</h3><p>Separate the construction of a complex object from its representation, allowing the same construction process to create various representations.</p><h3>Motivation</h3><p>Consider a complex object that requires multiple steps to construct, such as a text document or a meal in a restaurant. The Builder pattern allows for step-by-step construction, providing flexibility in creating different representations of the object without altering the construction process.</p><h3>Applicability</h3><ul><li>When the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled.</li><li>When the construction process must allow different representations for the object being constructed.</li></ul><h3>Structure</h3><p>The Builder pattern involves four main components:</p><ol><li><strong>Builder</strong>: Specifies an abstract interface for creating parts of a Product object.</li><li><strong>ConcreteBuilder</strong>: Constructs and assembles parts of the Product by implementing the Builder interface. It defines and keeps track of the representation it creates and provides an interface to retrieve the product.</li><li><strong>Director</strong>: Constructs an object using the Builder interface.</li><li><strong>Product</strong>: Represents the complex object being built.</li></ol><h3>Collaborations</h3><ul><li>The client creates a Director object and configures it with the desired Builder object.</li><li>The Director then instructs the builder to construct the product by invoking its building steps sequentially.</li><li>The client retrieves the product from the builder.</li></ul><p><strong>Example in ASP.NET</strong></p><p>The `WebHostBuilder` in ASP.NET Core follows the Builder pattern, a common design pattern used to construct complex objects step by step. The Builder pattern allows you to produce different types and representations of an object using the same construction code. Here’s how the `WebHostBuilder` implements the Builder pattern in the context of setting up an ASP.NET Core web host.</p><p>In ASP.NET Core, the `<strong>WebHostBuilder</strong>` acts as the Builder, the `<strong>Startup</strong>` class provides the parts and configuration, and the `<strong>WebHost</strong>` is the final product.</p><p><strong>Components in ASP.NET Core WebHostBuilder</strong></p><p>1. <strong>IHostBuilder</strong>: Defines the methods for configuring the host.<br>2. <strong>WebHostBuilder</strong>: Implements `IHostBuilder` and provides specific methods for web host configuration.<br>3. <strong>Host</strong>: The final product that runs the web application.<br>4. <strong>Program.cs</strong>: Acts as the Director by coordinating the building process.</p><p><strong>Detailed Example with Explanation</strong></p><p>Here’s a step-by-step breakdown of how the Builder pattern is used with `WebHostBuilder` in ASP.NET Core.</p><p><strong>Step 1: Setting Up the Program.cs</strong></p><p>The `Program.cs` file uses the `WebHostBuilder` to configure and build the host. This is where the Director coordinates the construction process.</p><pre>using Microsoft.AspNetCore.Hosting;<br>using Microsoft.Extensions.Hosting;<br><br>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>        CreateHostBuilder(args).Build().Run();<br>    }<br><br>    public static IHostBuilder CreateHostBuilder(string[] args) =&gt;<br>        Host.CreateDefaultBuilder(args)<br>            .ConfigureWebHostDefaults(webBuilder =&gt;<br>            {<br>                webBuilder.UseStartup&lt;Startup&gt;();<br>            });<br>}</pre><p>In this example:<br>- `Host.CreateDefaultBuilder(args)`: Initializes a default host builder with common configurations.<br>- `ConfigureWebHostDefaults`: Applies default configurations specific to web hosting and specifies the `Startup` class.</p><p><strong>Step 2: Configuring the Startup Class</strong></p><p>The `Startup` class defines the parts of the web application, such as services and middleware.</p><pre>using Microsoft.AspNetCore.Builder;<br>using Microsoft.AspNetCore.Hosting;<br>using Microsoft.Extensions.DependencyInjection;<br>using Microsoft.Extensions.Hosting;<br><br>public class Startup<br>{<br>    public void ConfigureServices(IServiceCollection services)<br>    {<br>        services.AddControllersWithViews(); // Register MVC services<br>    }<br><br>    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)<br>    {<br>        if (env.IsDevelopment())<br>        {<br>            app.UseDeveloperExceptionPage();<br>        }<br>        else<br>        {<br>            app.UseExceptionHandler(&quot;/Home/Error&quot;);<br>            app.UseHsts();<br>        }<br><br>        app.UseHttpsRedirection();<br>        app.UseStaticFiles();<br><br>        app.UseRouting();<br><br>        app.UseAuthorization();<br><br>        app.UseEndpoints(endpoints =&gt;<br>        {<br>            endpoints.MapControllerRoute(<br>                name: &quot;default&quot;,<br>                pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);<br>        });<br>    }<br>}</pre><p>In this example:<br>- <strong>ConfigureServices </strong>registers services required by the application.<br>- <strong>Configure</strong> defines the middleware pipeline that processes incoming HTTP requests.</p><p><strong>Step 3: Customizing the WebHostBuilder</strong></p><p>The `WebHostBuilder` can be further customized to fine-tune the configuration.</p><pre>public static IHostBuilder CreateHostBuilder(string[] args) =&gt;<br>    Host.CreateDefaultBuilder(args)<br>        .ConfigureWebHostDefaults(webBuilder =&gt;<br>        {<br>            webBuilder.UseStartup&lt;Startup&gt;();<br>            webBuilder.UseKestrel(options =&gt;<br>            {<br>                options.ListenAnyIP(5000);<br>            });<br>            webBuilder.ConfigureAppConfiguration((hostingContext, config) =&gt;<br>            {<br>                config.AddJsonFile(&quot;customconfig.json&quot;, optional: true, reloadOnChange: true);<br>            });<br>            webBuilder.ConfigureLogging(logging =&gt;<br>            {<br>                logging.ClearProviders();<br>                logging.AddConsole();<br>            });<br>        });</pre><p>In this example:<br>- <strong>UseKestrel</strong>: Configures the Kestrel web server with specific settings.<br>- <strong>ConfigureAppConfiguration</strong>: Adds additional configuration sources.<br>- <strong>ConfigureLogging</strong>: Sets up logging providers and configurations.</p><p><strong>How It All Fits Together</strong></p><p>1. <strong>Program.cs (Director)</strong>:<br> — Coordinates the building process by calling methods on the `WebHostBuilder`.<br> — Uses `CreateDefaultBuilder` to initialize the builder with default settings.<br> — Uses `ConfigureWebHostDefaults` to apply common configurations and specify the `Startup` class.</p><p>2. <strong>WebHostBuilder (Builder)</strong>:<br> — Provides methods like `UseStartup`, `UseKestrel`, `ConfigureAppConfiguration`, and `ConfigureLogging` to configure various aspects of the web host.<br> — Implements the `Build` method to create the `WebHost`.</p><p>3. <strong>Startup.cs (ConcreteBuilder)</strong>:<br> — Provides specific configurations for services and the request pipeline.<br> — Contains methods `ConfigureServices` and `Configure` to set up the application.</p><p>4. <strong>WebHost (Product)</strong>:<br> — The final product that runs the web application.<br> — Manages the application lifecycle, handles HTTP requests, and serves responses.</p><p>### Conclusion</p><p>The `WebHostBuilder` in ASP.NET Core is a practical implementation of the Builder pattern, allowing for the flexible and incremental construction of a web host. By separating the configuration steps into distinct methods and components, ASP.NET Core enables a modular and maintainable approach to setting up web applications. The `Program.cs` acts as the Director, using the `WebHostBuilder` to apply various configurations and produce the final `WebHost` product that runs the application.</p><h3>Modern Alternatives to this Pattern</h3><ul><li><strong>Fluent Interfaces</strong>: Methods return the object itself, allowing for method chaining to achieve similar results.</li><li><strong>Constructors with Multiple Parameters</strong>: Can replace simpler uses of the Builder pattern, although they lack the flexibility for very complex objects.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87a7ab270e9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Be Careful Coding in Node js]]></title>
            <link>https://medium.com/@salonasinha/be-careful-coding-in-node-js-26577e238177?source=rss-255eb5deead5------2</link>
            <guid isPermaLink="false">https://medium.com/p/26577e238177</guid>
            <category><![CDATA[coding-in-node-js]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[callback]]></category>
            <dc:creator><![CDATA[Salona Sinha]]></dc:creator>
            <pubDate>Wed, 27 Feb 2019 10:29:48 GMT</pubDate>
            <atom:updated>2019-02-28T10:23:29.882Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Be Careful Coding in Node js</strong></p><p>Coming from a java or any other object oriented background, writing node js code may intimidate you in the first look. You might wonder am i going to miss object oriented design patterns, primitive dataTypes and so on.</p><p>There are concepts to learn before you start coding are as follows:</p><ul><li><a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076bec976">pure functions</a></li><li><a href="https://blog.risingstack.com/fundamental-node-js-design-patterns/">node js design patterns</a></li><li><a href="https://medium.com/@theflyingmantis/callbacks-vs-promises-and-basics-of-js-80d3d1515e81">callbacks vs promises</a></li><li><a href="https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/">blocking vs non blocking</a></li><li><a href="https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/">event loop</a></li></ul><p>Once you have understood some of the basics about node js usage, you will find node js easy and fast to code.</p><p>But very soon you come across multiple items that come back when you do a code change.</p><p>The problems that are quite common:</p><ul><li>Ide(some popular ide) does not help you much on refactoring. whenever file location change please change at all references manually</li><li>whenever you change a method signature find all usages manually and apply the expected change</li><li>Promises are another great threat. When a calling body expects a promise to be returned be careful all flows return reject or resolve for the returned promise.</li></ul><blockquote>The example given below will now work as expected:</blockquote><pre><strong>return</strong> Promise.each(messageSet, <strong>function</strong> (m) {</pre><pre>  func1(arg1,<strong>function</strong> (err,result) {</pre><pre><strong>    if</strong>(err){</pre><pre><strong>      return</strong> Promise.reject(“error while refreshing config “+err)</pre><pre>    }<strong>else</strong>{</pre><pre><strong>      return</strong> func2(result)<br>    }<br>})</pre><blockquote>should ideally convert func1 to a promise returning function.</blockquote><ul><li>Same applies for callbacks all flows in a callback based function should return callback.</li></ul><p>Node js may be easy to setup and and easy write code, but comes with its own set of problems. Most of these problems would never come back if there were strong compiler embedded with and ide.</p><p>Choose node js when you need single threaded process, with lots of non-cpu intensive, non-blocking operations. Don’t be fooled by its ease of set up and coding. Your initial days may be easy but your alertness will definitely be tested.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26577e238177" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>