<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick Schadler</title>
    <description>The latest articles on DEV Community by Patrick Schadler (@patzistar).</description>
    <link>https://dev.to/patzistar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F98318%2F039f4c56-f466-490f-82cd-f5615710329f.jpg</url>
      <title>DEV Community: Patrick Schadler</title>
      <link>https://dev.to/patzistar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/patzistar"/>
    <language>en</language>
    <item>
      <title>Builder Design Pattern</title>
      <dc:creator>Patrick Schadler</dc:creator>
      <pubDate>Thu, 27 Feb 2020 11:43:05 +0000</pubDate>
      <link>https://dev.to/patzistar/builder-design-pattern-1po2</link>
      <guid>https://dev.to/patzistar/builder-design-pattern-1po2</guid>
      <description>&lt;h1&gt;
  
  
  Builder Design Pattern
&lt;/h1&gt;

&lt;p&gt;This post was originally published &lt;a href="https://www.patrickschadler.com/builder-design-pattern/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In today’s episode of &lt;strong&gt;Software Design Patterns&lt;/strong&gt; you will learn everything about the &lt;strong&gt;Builder Design Pattern&lt;/strong&gt;. The Builder Pattern is also pretty common and widely used. Different from the already covered &lt;strong&gt;behavioral&lt;/strong&gt; pattern &lt;a href="https://www.patrickschadler.com/observer-design-pattern/"&gt;Observer&lt;/a&gt;, the Builder Pattern is a creational design patterns like &lt;a href="https://www.patrickschadler.com/singleton-design-pattern/"&gt;Singleton&lt;/a&gt; and &lt;a href="https://www.patrickschadler.com/factory-method-design-pattern/"&gt;Factory Method&lt;/a&gt; pattern.&lt;/p&gt;

&lt;p&gt;All the examples, source code and unit tests are in this &lt;a href="https://github.com/DonkeyKongJr/BuilderPattern"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qF2jUiUG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/github-logo-6a5bca60a4ebf959a6df7f08217acd07ac2bc285164fae041eacb8a148b1bab9.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DonkeyKongJr"&gt;
        DonkeyKongJr
      &lt;/a&gt; / &lt;a href="https://github.com/DonkeyKongJr/BuilderPattern"&gt;
        BuilderPattern
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This Repository hosts how a Builder Design Pattern could be used.
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  What’s the purpose of the Builder Design Pattern?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Gang-of-Four&lt;/strong&gt; [1] reference book ("&lt;em&gt;Design Patterns. Elements of Reusable Object-Oriented Software&lt;/em&gt;") describes the intent of the Builder Pattern as the following.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Separate the construction of a complex object from its representation so that the same construction process can create different representations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically, you have a &lt;strong&gt;builder&lt;/strong&gt; and a &lt;strong&gt;director&lt;/strong&gt;. In my example on &lt;a href="https://github.com/DonkeyKongJr/BuilderPattern"&gt;GitHub&lt;/a&gt; I use different types of houses. There is a builder for flat and hipped houses. Those builders are created and passed to the director. The director itself constructs the different houses. To retrieve the built house the actual builder has a method for this (&lt;em&gt;GetHouse()&lt;/em&gt;). Via this you can use the concrete house implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the Builder look like?
&lt;/h2&gt;

&lt;p&gt;In the picture below you find the Unified Modeling Language (UML) diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y9exhIn2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.patrickschadler.com/wp-content/uploads/2019/10/Builder-Pattern-UML.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y9exhIn2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.patrickschadler.com/wp-content/uploads/2019/10/Builder-Pattern-UML.png" alt="Builder Pattern UML "&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In the linked &lt;a href="https://github.com/DonkeyKongJr/BuilderPattern"&gt;GitHub repository&lt;/a&gt; you will find interfaces for the &lt;strong&gt;IHouseBuilder&lt;/strong&gt; and the &lt;strong&gt;IDirector&lt;/strong&gt;. Many resources advise you to use abstract base classes for the director and the builder - which is totally fine. I simply like interfaces more, so I went this route. The IHousebuilder interface contains the definition for building the house and retrieving the build instance. The &lt;strong&gt;IDirector&lt;/strong&gt; simply has a construct method. In this method all steps should be done to build a specific thing - in our case a given type of house. The &lt;strong&gt;House&lt;/strong&gt; class stores the model of a house. To make it simple I minimized the house to four property with getter only. To set value, you have to use the constructor when creating a house.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IHouseBuilder&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BuildHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;House&lt;/span&gt; &lt;span class="nf"&gt;GetHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IDirector&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;House&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;PaintingColor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;RoofType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;NumbersOfFloors&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;HasCellar&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;House&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;paintingColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;roofType&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;numbersOfFloors&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasCellar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;PaintingColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paintingColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;RoofType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;roofType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;NumbersOfFloors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbersOfFloors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;HasCellar&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hasCellar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The concrete house builders sets the correspondending values when creating a new instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FlatRoofHouseBuilder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IHouseBuilder&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;House&lt;/span&gt; &lt;span class="n"&gt;_house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;House&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BuildHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;House&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"white"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"flat"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;House&lt;/span&gt; &lt;span class="nf"&gt;GetHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_house&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HippedRoofHouseBuilder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IHouseBuilder&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;House&lt;/span&gt; &lt;span class="n"&gt;_house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;House&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;BuildHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;House&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"gray"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hipped"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;House&lt;/span&gt; &lt;span class="nf"&gt;GetHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_house&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The construct method on the director initiates the build process for the given house builder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HouseBuildDirector&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IDirector&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;IHouseBuilder&lt;/span&gt; &lt;span class="n"&gt;_houseBuilder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HouseBuildDirector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IHouseBuilder&lt;/span&gt; &lt;span class="n"&gt;houseBuilder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_houseBuilder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;houseBuilder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_houseBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;BuildHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the unit tests you see the whole process of how to use the builder pattern. In this case it seems pretty odd to use such a pattern, but imagine that the complete build/construct process takes many complex steps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BuilderPatternTest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShouldBuildFlatHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;flatHouseBuilder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;FlatRoofHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;houseBuildDirector&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HouseBuildDirector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flatHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;houseBuildDirector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;flatHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HasCellar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;BeFalse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PaintingColor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"white"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RoofType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"flat"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NumbersOfFloors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShouldBuildHippedHouse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;hippedRoofHouseBuilder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HippedRoofHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;houseBuildDirector&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HouseBuildDirector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hippedRoofHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;houseBuildDirector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Construct&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;house&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hippedRoofHouseBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HasCellar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;BeTrue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PaintingColor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"gray"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RoofType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hipped"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NumbersOfFloors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Appendix: There are many ways of describing the builder pattern. Often it's mistaken by a fluent library where you chain different methods and eventually build an object from it. Let me know in the comments below how you find the builder or how you learned it.&lt;/p&gt;

&lt;p&gt;If you have any questions or comments feel free to leave them below.&lt;/p&gt;

&lt;p&gt;That’s it. Happy coding as always :).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;[1] –&lt;a href="https://en.wikipedia.org/wiki/Design_pattern"&gt;Design patterns&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Software_engineering"&gt;software engineering&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming"&gt;object-oriented programming&lt;/a&gt;&lt;/p&gt;

</description>
      <category>softwaredesignpattern</category>
      <category>softwarecraftmanship</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Factory Method Design Pattern</title>
      <dc:creator>Patrick Schadler</dc:creator>
      <pubDate>Thu, 30 Jan 2020 14:43:34 +0000</pubDate>
      <link>https://dev.to/patzistar/factory-method-design-pattern-25bf</link>
      <guid>https://dev.to/patzistar/factory-method-design-pattern-25bf</guid>
      <description>&lt;p&gt;This post was originally published &lt;a href="https://www.patrickschadler.com/factory-method-design-pattern/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Factory Method Design Pattern
&lt;/h1&gt;

&lt;p&gt;In today's episode of &lt;strong&gt;Software Design Patterns&lt;/strong&gt; you will learn everything about the &lt;strong&gt;Factory Method Design Pattern&lt;/strong&gt;. The Factory Method is a creational pattern. That means that the pattern has something to do with object creation. There are different types of design patterns. The other types are &lt;strong&gt;structural&lt;/strong&gt; and &lt;strong&gt;behavioral&lt;/strong&gt;. Those types will have a dedicated blog post in the future. For reference - the last article covered the &lt;a href="https://www.patrickschadler.com/singleton-design-pattern/" rel="noopener noreferrer"&gt;Singleton Design Pattern&lt;/a&gt; - which is also a creational pattern. All the examples, source code and unit tests are in this &lt;a href="https://github.com/DonkeyKongJr/FactoryMethodPattern" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;. &lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DonkeyKongJr" rel="noopener noreferrer"&gt;
        DonkeyKongJr
      &lt;/a&gt; / &lt;a href="https://github.com/DonkeyKongJr/FactoryMethodPattern" rel="noopener noreferrer"&gt;
        FactoryMethodPattern
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This Repository hosts how a Factory Method Design Pattern could be used.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;FactoryMethodPattern&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;&lt;a href="https://www.codacy.com/manual/patrick.schadler/FactoryMethodPattern?utm_source=github.com&amp;amp;utm_medium=referral&amp;amp;utm_content=DonkeyKongJr/FactoryMethodPattern&amp;amp;utm_campaign=Badge_Grade" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/985a43dff924fe2f4e1f5357253ba9bc4967985e6c473eb1e9ba8233bb74eeef/68747470733a2f2f6170692e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3962356330623032333362643432643039646631623265393835373031306663" alt="Codacy Badge"&gt;&lt;/a&gt;
&lt;a href="https://circleci.com/gh/DonkeyKongJr/FactoryMethodPattern" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/23e1803894741f9afbf13ebf196cefaff6d13bc2f9326fb506fadbce6ef2e3b9/68747470733a2f2f636972636c6563692e636f6d2f67682f446f6e6b65794b6f6e674a722f466163746f72794d6574686f645061747465726e2e7376673f7374796c653d737667" alt="CircleCI"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This Repository hosts how a Factory Method Design Pattern could be used.&lt;/p&gt;
&lt;p&gt;You find an up-to-date description in the related blog post here. (Yet to come)&lt;/p&gt;
&lt;p&gt;For any changes or updates please file a Pull Request.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DonkeyKongJr/FactoryMethodPattern" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  What’s the purpose of the Factory Method Design Pattern?
&lt;/h2&gt;

&lt;p&gt;The Factory Method is a very common pattern, but strongly misused. I have seen many examples where methods were named factory, but did something completely different. The quote below - &lt;em&gt;taken from the Gang of Four&lt;/em&gt; - is very precise about the Factory Method. The Factory Method Design Pattern defines an interface for creating an object, but let sub classes decide which class to instantiate. Factory Method lets a class defer instantiating to sub classes. [1]&lt;/p&gt;

&lt;p&gt;Don't fall into the trap and use it for everything. If you are not sure about when to use, you probably don't need it anyway. &lt;/p&gt;

&lt;h2&gt;
  
  
  How does the Factory Method looks like?
&lt;/h2&gt;

&lt;p&gt;In the picture below you find the Unified Modeling Language (UML) diagram. &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.patrickschadler.com%2Fwp-content%2Fuploads%2F2019%2F10%2FFactory-Method-UML.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.patrickschadler.com%2Fwp-content%2Fuploads%2F2019%2F10%2FFactory-Method-UML.jpg" alt="Factory"&gt;&lt;/a&gt;  As mentioned at the beginning you find all the code examples in a working project on &lt;a href="https://github.com/DonkeyKongJr/FactoryMethodPattern" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ICar&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Accelerate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Break&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ElectricCar&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ICar&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Electric"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Accelerate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Break&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Speed&lt;/span&gt;&lt;span class="p"&gt;--;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PetrolCar&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ICar&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Petrol"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Accelerate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Speed&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Break&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Speed&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Speed&lt;/span&gt;&lt;span class="p"&gt;--;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example we have an interface &lt;strong&gt;ICar.cs&lt;/strong&gt;, which provides basic car operations like accelerate and break. In addition to that we have an &lt;strong&gt;GetName()&lt;/strong&gt; method, were the concrete implementation should return the actual name of the car.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CarFactory&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ICar&lt;/span&gt; &lt;span class="nf"&gt;CreateCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Petrol"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StringComparison&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PetrolCar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Electric"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;StringComparison&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrdinalIgnoreCase&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;NotSupportedException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="n"&gt;supported&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our Factory, called &lt;strong&gt;CarFactory.cs&lt;/strong&gt;, is responsible for creating instances of concrete cars. We provide a dedicated &lt;strong&gt;CreateCar(string type)&lt;/strong&gt; method, where the desired type can be passed as an argument. The method maps the type to a concrete implementation. This can be done via multiple &lt;strong&gt;if&lt;/strong&gt; clauses or a &lt;strong&gt;switch statement&lt;/strong&gt;. Anyway, if there is no matching type we throw an &lt;strong&gt;NotSupportedException&lt;/strong&gt; to make it clear that the factory does not support the desired type. To see the proper use of the &lt;strong&gt;CarFactory&lt;/strong&gt; look into the test project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CarFactoryTest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Theory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Electric"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"electric"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"eLeCtriC"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShouldCreateElectricCarInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CarFactory&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;BeOfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Electric"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Theory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Petrol"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"petrol"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"pEtRoL"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShouldCreatePetrolCarInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CarFactory&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;BeOfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;PetrolCar&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetName&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Petrol"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Theory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hans"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"car"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"electricpetrol"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ShouldThrowNotSupportedExceptionWhenTypeIsInvalid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;CarFactory&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Action&lt;/span&gt; &lt;span class="n"&gt;getCar&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;carFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;getCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;ThrowExactly&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;NotSupportedException&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have any questions or comments feel free to leave them below. That’s it. Happy coding as always. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;References&lt;/em&gt;&lt;/strong&gt;[1] -&lt;a href="https://en.wikipedia.org/wiki/Design_pattern" rel="noopener noreferrer"&gt;Design patterns&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Software_engineering" rel="noopener noreferrer"&gt;software engineering&lt;/a&gt;, &lt;a href="https://en.wikipedia.org/wiki/Object-oriented_programming" rel="noopener noreferrer"&gt;object-oriented programming&lt;/a&gt;&lt;/p&gt;

</description>
      <category>softwaredesignpattern</category>
      <category>softwarecraftmanship</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Creating A OpenAPI (Swagger) Documentation For RESTful Services</title>
      <dc:creator>Patrick Schadler</dc:creator>
      <pubDate>Wed, 29 Jan 2020 12:17:11 +0000</pubDate>
      <link>https://dev.to/patzistar/creating-a-swagger-documentation-for-restful-services-bp9</link>
      <guid>https://dev.to/patzistar/creating-a-swagger-documentation-for-restful-services-bp9</guid>
      <description>&lt;p&gt;This post was originally published &lt;a href="https://www.patrickschadler.com/creating-swagger-documentation/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this Blog post we discover creating a OpenAPI (Swagger) documentation for WebAPI (&lt;a href="https://www.asp.net/"&gt;ASP.NET&lt;/a&gt;) and why you should do it too. If you have any questions on creating a RESTful Service with C# and Visual Studio just look at this tutorial &lt;a href="https://www.patrickschadler.com/creating-a-rest-webservice-with-c-sharp/"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;First - what is Swagger? &lt;a href="http://swagger.io/"&gt;Swagger&lt;/a&gt; lets you create a beautiful and rich documentation for your RESTful web services. The creators itself calls Swagger THE WORLD'S MOST POPULAR API TOOLING. Furthermore, you can provide an easy way to test your web service with the in-built functionality from swagger. And the best part of it? It's free. And what makes it even better? &lt;br&gt;
You just have to add a nuget package to your WebApi project to automatically create an interactive documentation. The nuget package you want to use is called &lt;a href="https://www.nuget.org/packages/Swashbuckle"&gt;Swashbuckle&lt;/a&gt;. Double check if there is a new version - if so, you just can update the command to match the latest version number. By running the following command you add all needed files to your current project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PM&amp;gt; Install-Package Swashbuckle -Version 5.6.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After the successful install process you will find a new file within the App_Start folder called SwaggerConfig.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App_Start -&amp;gt; SwaggerConfig.cs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you don't need anything special you don't really want to modify this file - just leave it as it is for the moment. To see how your Swagger documentation looks like just run the Application by pressing F5 or the green start button. Make sure that you have chosen a Browser to run with. After the build of your project succeeds the browser window with your Home Controller will open. To navigate to your Swagger Documentation just enter /swagger to your given URL. The URL should look like the following example. &lt;/p&gt;

&lt;p&gt;If you use a dotnet core project you can add swagger via the application builder and configure it via the service collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSwagger();
    app.UseSwaggerUI(c =&amp;gt;
    {
        c.SwaggerEndpoint($"/swagger/v{ApiInfo.CurrentVersion}/swagger.json", $"{ApiInfo.Name} v1");
        c.DocExpansion(DocExpansion.None);
    });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(options =&amp;gt;
        {
            options.SwaggerDoc(ApiVersion, new Info
            {
                Title = $"{ApiInfo.Name} {ApiVersion}",
                Description = ApiInfo.Description,
                Version = ApiVersion,
                Contact = new Contact
                {
                    Name = ApiInfo.ContactName,
                    Email = ApiInfo.ContactEmail,
                    Url = ApiInfo.ContactUrl
                }
            });
            options.DescribeAllEnumsAsStrings();
            options.DescribeAllParametersInCamelCase();
            options.OperationFilter&amp;lt;FileUploadOperation&amp;gt;();
        });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:49442/swagger
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you have entered the URL you should be navigated to the documentation and test page. It will look similar to the following screenshot of mine. &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jd9Kftl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.patrickschadler.com/wp-content/uploads/2018/01/swagger.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jd9Kftl1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.patrickschadler.com/wp-content/uploads/2018/01/swagger.png" alt="Swagger"&gt;&lt;/a&gt; Swagger Documentation Page[/caption] On this page you can easily open every single controller you have defined in your WebApi Project. Within every controller section you will find all the available HTTP Methods (for example get, put, post, delete etc.) which you can test and execute immediately by using the Try it out! Button from this swagger page. That's everything you need to do to add a stunning swagger documentation to your WebApi Project. Happy Swaggering.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>swagger</category>
      <category>openapi</category>
      <category>c</category>
    </item>
    <item>
      <title>C# Unit Tests with Mocks</title>
      <dc:creator>Patrick Schadler</dc:creator>
      <pubDate>Wed, 22 Jan 2020 15:38:25 +0000</pubDate>
      <link>https://dev.to/patzistar/c-unit-tests-with-mocks-8i7</link>
      <guid>https://dev.to/patzistar/c-unit-tests-with-mocks-8i7</guid>
      <description>&lt;p&gt;This post was originally published &lt;a href="https://www.patrickschadler.com/c-unit-tests-mocks/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;C# Unit Tests with Mocks provide an easy way of reducing unwanted dependencies when writing unit tests. One of the best and shortest definition for mocking is &lt;em&gt;mocking is creating objects that simulate the behavior of real objects. - &lt;a href="https://stackoverflow.com/questions/2665812/what-is-mocking"&gt;StackOverflow&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prologue
&lt;/h2&gt;

&lt;p&gt;However, when talking about writing unit tests I would strongly advise to not change the implementation itself. Rather, think about how you can test it easily before start writing the implementation. The very best approach for unit-testing would be &lt;a href="http://agiledata.org/essays/tdd.html"&gt;Test-Driven-Development&lt;/a&gt; (TDD) anyway, but I understand that many struggles with this methodology. Especially when starting with tests in general. Always Keep in mind that the &lt;a href="http://www.patrickschadler.com/solid-principles-part-i/"&gt;S.O.L.I.D&lt;/a&gt; Framework helps you a lot when writing unit tests. &lt;/p&gt;

&lt;h2&gt;
  
  
  Moq Framework
&lt;/h2&gt;

&lt;p&gt;Moq is an open-source framework which can be found on &lt;a href="https://github.com/moq/moq4"&gt;GitHub&lt;/a&gt;. When start using Moq make sure you always install the very latest version. Moq is usable with any test runner like &lt;a href="https://github.com/Microsoft/vstest"&gt;vstest&lt;/a&gt;, &lt;a href="https://xunit.github.io/"&gt;xunit&lt;/a&gt;, &lt;a href="https://nunit.org/"&gt;nunit&lt;/a&gt; etc. When using Moq or any other mocking framework keep following restrictions in mind. You can not mock: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Static class/methods.&lt;/li&gt;
&lt;li&gt;Extensions Methods.&lt;/li&gt;
&lt;li&gt;Non-virtual methods (except there is an interface).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For this blog post, I have used my &lt;a href="https://github.com/DonkeyKongJr/link-reader"&gt;link-reader repository&lt;/a&gt;. This project is written with dotnet core 2.1. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;However, you can easily use a newer version of dotnet core if you want. You can see that in this particular case it is necessary to nest multiple mocks within each other. You can configure that one mock mocks a method and return another mock object. To generate a mock you just have to create a new Mock object and set the type. As you see Moq uses generics very heavily.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var moq = new Mock&amp;lt;IWebRequest&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When you look at the IWebRequest definition you'll see that the interface has a Create method that all classes have to implement. Therefore, we can easily mock this method in our case by using the Setup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;moq.Setup(_ =&amp;gt; _.Create(It.IsAny&amp;lt;string&amp;gt;())).Returns(moqHttpWebRequest.Object);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When you want to mock a method with parameters you can work with the &lt;em&gt;It.IsAny()&lt;/em&gt; option. In the example above all string values are allowed. Of course, you can strict this up and allow only specific values to work with. To work with mocks you have to use the &lt;em&gt;mock&lt;/em&gt;.Object property. This property is the actual mock implementation of the source.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var webRequest = moq.Object.Create(test);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The line above returns the &lt;em&gt;moqHttpWebRequest.Object&lt;/em&gt;, because that's the way we have set up the mock behavior. &lt;/p&gt;

&lt;h2&gt;
  
  
  Customizing Mock Behavior
&lt;/h2&gt;

&lt;p&gt;Moq provides different ways to set up the mock - &lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior"&gt;MockBehavior.Strict&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior"&gt;MockBehavior.Loose&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior"&gt;CallBase = true&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior"&gt;{ DefaultValue = DefaultValue.Mock }&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior"&gt;MockBehavior.Strict and { DefaultValue = DefaultValue.Mock }&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Verification
&lt;/h2&gt;

&lt;p&gt;When you are using the &lt;a href="https://github.com/Moq/moq4/wiki/Quickstart#verification"&gt;verification&lt;/a&gt; from a mock instance by calling &lt;em&gt;moq.Verify()&lt;/em&gt; it does check if all set up methods are called. You can check all Moq Setups without any Verifiable declaration by using the &lt;em&gt;moq.VerifyAll()&lt;/em&gt; method on the mocked object. Happy mocking and testing.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>unittest</category>
      <category>qa</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Singleton Design Pattern in c#</title>
      <dc:creator>Patrick Schadler</dc:creator>
      <pubDate>Mon, 20 Jan 2020 14:40:37 +0000</pubDate>
      <link>https://dev.to/patzistar/singleton-design-pattern-in-c-17co</link>
      <guid>https://dev.to/patzistar/singleton-design-pattern-in-c-17co</guid>
      <description>&lt;h1&gt;
  
  
  Singleton Design Pattern
&lt;/h1&gt;

&lt;p&gt;This post was originally published at my personal blog &lt;a href="https://www.patrickschadler.com/singleton-design-pattern/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With this post a brand-new series about Software Design Patterns starts. You will learn everything about the most used Design Patterns. The first is probably the most known. It is the Singleton Design Pattern. It is one of the twenty-three well-known - and industry standard - &lt;a href="https://www.amazon.de/Patterns-Elements-Reusable-Object-Oriented-Software/dp/0201633612" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Gang of Four&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; Design Pattern Collection.&lt;/p&gt;

&lt;p&gt;All the examples, source code and unit tests are in this &lt;a href="https://github.com/DonkeyKongJr/SingletonDesignPattern" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/DonkeyKongJr" rel="noopener noreferrer"&gt;
        DonkeyKongJr
      &lt;/a&gt; / &lt;a href="https://github.com/DonkeyKongJr/SingletonDesignPattern" rel="noopener noreferrer"&gt;
        SingletonDesignPattern
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      This Repository hosts how a Singleton Design Pattern could be used.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;SingletonDesignPattern&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;This Repository hosts how a Singleton Design Pattern could be used.&lt;/p&gt;
&lt;p&gt;You find an up-to-date description in the related blog post &lt;a href="https://www.patrickschadler.com/singleton-design-pattern/" rel="nofollow noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For any changes or updates please file a Pull Request.&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/DonkeyKongJr/SingletonDesignPattern" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;h2&gt;
  
  
  What's the purpose of the Singleton Design Pattern?
&lt;/h2&gt;

&lt;p&gt;In software development we often instantiate a class many times. This could be problematic. As a result you have to deal with many instances of the very same class. This introduces complexity and increases memory usage of the application.&lt;/p&gt;

&lt;p&gt;The Singleton Design Pattern solves this specific issue. It ensures that only one instance of this class exists. Beyond that it standardizes and eases the access to that instance. To access the instance, a Singleton provides per design a static &lt;code&gt;GetInstance()&lt;/code&gt; method. To prevent any instantiating via the new keyword you have to hide the constructor.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the Singleton Design Pattern looks like?
&lt;/h2&gt;

&lt;p&gt;In the picture below you find the Unified Modeling Language (UML) diagram.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.patrickschadler.com%2Fwp-content%2Fuploads%2F2019%2F08%2F2000px-Singleton_UML_class_diagram-e1565094559716-1024x532.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.patrickschadler.com%2Fwp-content%2Fuploads%2F2019%2F08%2F2000px-Singleton_UML_class_diagram-e1565094559716-1024x532.png" alt="Singleton"&gt;&lt;/a&gt; Singleton UML Diagram&lt;/p&gt;

&lt;p&gt;The very basic implementation doesn't care about thread-safety. It's called &lt;a href="https://github.com/DonkeyKongJr/SingletonDesignPattern/blob/master/src/BasicSingleton.cs" rel="noopener noreferrer"&gt;BasicSingleton&lt;/a&gt; within the GitHub Repository.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BasicSingleton&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Singleton&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;BasicSingleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;BasicSingleton&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;BasicSingleton&lt;/span&gt; &lt;span class="nf"&gt;GetInstance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BasicSingleton&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
                &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BasicSingleton&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How about Thread-Safety?
&lt;/h2&gt;

&lt;p&gt;This implementation could lead to problems when many threads calls the &lt;em&gt;GetInstance()&lt;/em&gt; simultaneously. For example the second thread enters the if condition while the first isn't finished yet. Thus, a second instance will be created. This behavior violates the Singleton Design Pattern definition. The &lt;a href="https://github.com/DonkeyKongJr/SingletonDesignPattern/blob/master/src/ThreadSafeSingleton.cs" rel="noopener noreferrer"&gt;ThreadSafeSingleton&lt;/a&gt; prevents those race conditions with locking the access to a thread at a time. This guarantees that only one instance will ever be created. Keep in mind that every time we access the Singleton we perform a lock which uses a bit more resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ThreadSafeSingleton&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Singleton&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;ThreadSafeSingleton&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;singletonlock&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;ThreadSafeSingleton&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;ThreadSafeSingleton&lt;/span&gt; &lt;span class="nf"&gt;GetInstance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;lock&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;singletonlock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="p"&gt;{&lt;/span&gt;  
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ThreadSafeSingleton&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;instance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ThreadSafeSingleton&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;          
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it. Happy coding as always :).&lt;/p&gt;

</description>
      <category>softwaredesignpattern</category>
      <category>softwarecraftmanship</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
