<?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: Jozef Chmelar</title>
    <description>The latest articles on DEV Community by Jozef Chmelar (@jozefchmelar).</description>
    <link>https://dev.to/jozefchmelar</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%2F243303%2F39afb492-e33f-442f-8048-a52fb2e99b80.jpg</url>
      <title>DEV Community: Jozef Chmelar</title>
      <link>https://dev.to/jozefchmelar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jozefchmelar"/>
    <language>en</language>
    <item>
      <title>Microsoft.NET.Sdk.Common.targets was not found</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Thu, 30 Sep 2021 07:54:41 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/microsoft-net-sdk-common-targets-was-not-found-16dm</link>
      <guid>https://dev.to/jozefchmelar/microsoft-net-sdk-common-targets-was-not-found-16dm</guid>
      <description>&lt;p&gt;After update of Visual Studio and .NET I got an error that seems to repeat pretty often. Unfortunately I forget how to deal with it every single time. &lt;/p&gt;

&lt;p&gt;The issue looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;msbuild //works
dotnet build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Results in following error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C:\Program Files\dotnet\sdk\5.0.41\Current\SolutionFile\ImportAfter\Microsoft .NET.Sdk.Solution.targets(14,3): error MSB4619: The imported
project "C:\Program Files\dotnet\sdk\5.6.100\Sdks\Microsoft .NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" was not found. Confirm that
the expression in the Import declaration "C:\Program Files\dotnet\sdk\5.0.41\Sdks\Microsoft.NET.Sdk\targets\Microsoft .NET.Sdk.Common.targets" is correct, and that the file exists on disk.

Build FAILED.

C:\Program Files\dotnet\sdk\5.0.41\Current\SolutionFile\ImportAfter\Microsoft .NET.Sdk.Solution.targets(14,3): error MSB4619: The imported
project "C:\Program Files\dotnet\sdk\5.6.100\Sdks\Microsoft .NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets" was not found. Confirm that
the expression in the Import declaration "C:\Program Files\dotnet\sdk\5.0.41\Sdks\Microsoft.NET.Sdk\targets\Microsoft .NET.Sdk.Common.targets" is correct, and that the file exists on disk.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you try to print the file &lt;code&gt;Microsoft.NET.Sdk.Solution.targets&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat "C:\Program Files\dotnet\sdk\5.0.401\Current\SolutionFile\ImportAfter\Microsoft.NET.Sdk.Solution.targets"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice this line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;Import&lt;/span&gt; &lt;span class="na"&gt;Project=&lt;/span&gt;&lt;span class="s"&gt;"$(MSBuildSDKsPath)\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.Common.targets"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;MSBuildSDKsPath&lt;/code&gt; variable is important here. It looks like it's looking at the wrong folder.&lt;/p&gt;

&lt;p&gt;When I created an enviroment variable with the name &lt;code&gt;MSBuildSDKsPath&lt;/code&gt; and value &lt;code&gt;C:\Program Files\dotnet\sdk\5.0.401\Sdks&lt;/code&gt; it works! &lt;/p&gt;

&lt;p&gt;Don't forget to restart your terminal.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Covariance and contravariance by example.</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Sat, 23 Jan 2021 12:42:12 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/covariance-and-contravariance-by-example-289g</link>
      <guid>https://dev.to/jozefchmelar/covariance-and-contravariance-by-example-289g</guid>
      <description>&lt;p&gt;Covariance and contravariance are very spooky words.&lt;/p&gt;

&lt;p&gt;I use these words if I want to sound very smart - and it usually works. No one really knows what the hell it is. &lt;/p&gt;

&lt;p&gt;As I want to explain this topic as simple as possible (but not simpler) I'll try to avoid these smart-sounding words.&lt;/p&gt;

&lt;p&gt;I'm going to implement a Repository pattern with the use of generics to explain this topic. &lt;/p&gt;

&lt;p&gt;I'll use C# for my examples, it's pretty similar in languages like Java or Kotlin.  &lt;/p&gt;

&lt;h2&gt;
  
  
  You already know half of it!
&lt;/h2&gt;

&lt;p&gt;Let's define a few simple classes.&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;record&lt;/span&gt; &lt;span class="nc"&gt;Person&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;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Employee&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;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;RemoteEmployee&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;Name&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;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a few employees and put them inside a collection.&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="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Andrew"&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Karen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"USA"&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;Since an &lt;code&gt;Employee&lt;/code&gt; is also a &lt;code&gt;Person&lt;/code&gt; I should be able to do this.&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="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// It's not &amp;lt;Employee&amp;gt; anymore.&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;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Andrew"&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Karen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"USA"&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;So if an &lt;code&gt;Employee&lt;/code&gt; is also a &lt;code&gt;Person&lt;/code&gt; I can use a more generic type on the left side of the assignment. AKA Covariance!&lt;/p&gt;

&lt;p&gt;Simple right?&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6h7k9kgejkzwplqj8v0h.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6h7k9kgejkzwplqj8v0h.gif" alt="told you"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since I mentioned a &lt;em&gt;generic type&lt;/em&gt; let's make something where we can make  use of generics. Let's save the data in a database.&lt;/p&gt;

&lt;p&gt;| Fun fact : Generics were introduced in C# 2.0, released in 2005, along with Visual Studio 200.&lt;/p&gt;

&lt;p&gt;Should I use MySQL, Excel, SQL Server, SQLite or Postgres? The answer is - we shouldn't have to care. All I want to do is insert some data and get it back later. Here a repository pattern comes to rescue!&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;interface&lt;/span&gt; &lt;span class="nc"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;     &lt;span class="c1"&gt;// "T" stands for "data of some type"&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;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// I want to insert some data.&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;Get&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;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;        &lt;span class="c1"&gt;// and get it &lt;/span&gt;
    &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//            back later&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I implemented a very simple repository which stores the object in a Json file on a disk. You can see it &lt;a href="//FileRepository.cs"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you implement a repository like this you encounter a problem while implementing the &lt;code&gt;Insert&lt;/code&gt; method. &lt;br&gt;
How to insert the data in such a way that I can retrieve it later? &lt;br&gt;
There's an &lt;code&gt;id&lt;/code&gt; in the &lt;code&gt;Get&lt;/code&gt; method but under what &lt;code&gt;id&lt;/code&gt; would you Insert the data?&lt;/p&gt;

&lt;p&gt;You have multiple options here &lt;br&gt;
    1. Let a database handle it - The database could generate some Guid (like Mongo) or a relational database could generate a primary key.&lt;br&gt;
    2. Use a hash code or a ToString of an object - But you may never get the object back, so never do that.&lt;br&gt;
    3. Enforce an Id before it is added to a database.&lt;br&gt;
    4. Some other idea.&lt;/p&gt;

&lt;p&gt;You're in charge, not a database or a hash function so I'm going with the option #3.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8unqh1xmf8g4uu89gt5o.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8unqh1xmf8g4uu89gt5o.gif" alt="you are the king"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I want all my persistent objects to implement an &lt;code&gt;IEntity&lt;/code&gt; interface.&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;interface&lt;/span&gt; &lt;span class="nc"&gt;IEntity&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;Id&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="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;record&lt;/span&gt; &lt;span class="nc"&gt;Person&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;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IEntity&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;Id&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Name is not the best Id of course.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I also have to enforce the rule on the interface itself.&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;interface&lt;/span&gt; &lt;span class="nc"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IEntity&lt;/span&gt;  &lt;span class="c1"&gt;// "T" stands for "data of some type" where the data also has an Id.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I can store my list of employees in a database!&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;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&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="n"&gt;args&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;employeesRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FileRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;AddEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeesRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Add a list of employees to a repository&lt;/span&gt;
    &lt;span class="nf"&gt;ReadAndPrintRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeesRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;     &lt;span class="c1"&gt;// Read the repository and print users.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Karen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Usa"&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;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Karen"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadAndPrintRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&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;repository&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This all works, which is great. But an Employee is also a Person so I'd like to change make a small change.&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;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadAndPrintRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suddenly you get an error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cannot convert from 'FileRepository&amp;lt;Employee&amp;gt;' to 'IRepository&amp;lt;Person&amp;gt;'csharp(CS1503)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa5q4elnne1etqpk6goti.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa5q4elnne1etqpk6goti.gif" alt="but why meme"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But why? It works on the example above!&lt;/p&gt;

&lt;p&gt;To quote me  "So if an &lt;code&gt;Employee&lt;/code&gt; is also a &lt;code&gt;Person&lt;/code&gt; I can use a more abstract type on the left side of the assignment. AKA Covariance!&lt;/p&gt;

&lt;p&gt;What's the difference? Well if you take a look on the IEnumerable (or Collection in Java) interface it's defined like &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1?view=net-5.0" rel="noopener noreferrer"&gt;this.&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;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;out&lt;/code&gt; keyword is crucial here. It says to the compiler - use the type &lt;code&gt;T&lt;/code&gt; or a more abstract/derived type. The type &lt;code&gt;T&lt;/code&gt; is a &lt;em&gt;covariant&lt;/em&gt; parameter. It allows some change, a variance.&lt;/p&gt;

&lt;p&gt;But why the &lt;code&gt;out&lt;/code&gt; keyword? Why not &lt;code&gt;in&lt;/code&gt; or &lt;code&gt;covariant&lt;/code&gt; or &lt;code&gt;leave me alone and work plz&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Let's add an &lt;code&gt;out&lt;/code&gt; keyword to our interface and let's see.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invalid variance: The type parameter 'T' must be contravariantly valid on 'IRepository&amp;lt;T&amp;gt;.Insert(T)'. 'T' is covariant. csharp(CS1961)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(╯°□°）╯︵ ┻━┻&lt;/p&gt;

&lt;p&gt;Whaaat?  &lt;/p&gt;

&lt;p&gt;Well. When you specify the &lt;code&gt;out&lt;/code&gt; keyword it means that the interface can only &lt;strong&gt;OUTPUT&lt;/strong&gt; the  type &lt;code&gt;T&lt;/code&gt;. It cannot take an input of type &lt;code&gt;T&lt;/code&gt;, like it does in the Insert method. &lt;/p&gt;

&lt;p&gt;I'll remove the &lt;code&gt;Insert&lt;/code&gt; method from the &lt;code&gt;IRepository&lt;/code&gt; interface and move it to &lt;code&gt;IWriteOnlyRepository&lt;/code&gt;. To have some symmetry in the codebase I'll introduce an &lt;code&gt;IReadOnlyRepository&lt;/code&gt; and merge them in the &lt;code&gt;IRepository&lt;/code&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;interface&lt;/span&gt; &lt;span class="nc"&gt;IWriteOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IReadOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;Get&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;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IWriteOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;IReadOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Entity&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;Now when you use the &lt;code&gt;IReadOnlyRepository&lt;/code&gt; interface as a parameter in the &lt;code&gt;ReadAndPrintRepository&lt;/code&gt; method you won't get an error anymore! &lt;/p&gt;

&lt;p&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;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&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="n"&gt;args&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;employeesRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;FileRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;AddEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeesRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;ReadAndPrintRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employeesRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReadAndPrintRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IReadOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&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;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bjork"&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Karen"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Usa"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything works as expected. Life's good. But what if I add a special method which will only handle RemoteEmployees?&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;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddRemoteEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;//some operations only valid for RemoteEmployee&lt;/span&gt;
        &lt;span class="c1"&gt;//OnlyRemoteEmployeeMethod(remoteEmployee)&lt;/span&gt;
        &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Insert&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Andrew"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Canada"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Insert&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Carol"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"UK"&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;Unfortunately you'll get this error &lt;br&gt;
&lt;code&gt;cannot convert from 'FileRepository&amp;lt;Employee&amp;gt;' to 'IRepository&amp;lt;RemoteEmployee&amp;gt;'&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FileRepository&lt;/code&gt; implements the interface &lt;code&gt;IRepository&amp;lt;Employee&amp;gt;&lt;/code&gt; so only an Employee can be added into the repository. You can say that is very rigid. It won't allow any variance, therefore it is &lt;strong&gt;Invariant&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;IWriteOnlyRepository&lt;/code&gt; has currently the same problem as &lt;code&gt;IRepository&lt;/code&gt; it does not allow any variance. Since we have an &lt;code&gt;out&lt;/code&gt; modifier on the &lt;code&gt;IReadOnlyRepository&lt;/code&gt;, don't we have an &lt;code&gt;in&lt;/code&gt; modifier we can use? &lt;/p&gt;

&lt;p&gt;Yes, we do! When you specify the &lt;code&gt;in&lt;/code&gt; keyword it means that the interface can only accept an &lt;strong&gt;INPUT&lt;/strong&gt; of the type &lt;code&gt;T&lt;/code&gt;.  Since we are only using the type &lt;code&gt;T&lt;/code&gt; as an input and we never return in we can mark the input type as contravariant using the &lt;code&gt;in&lt;/code&gt; keyword.&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;interface&lt;/span&gt; &lt;span class="nc"&gt;IWriteOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;item&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 last thing to do is to change the interface in the method. Since &lt;code&gt;IRepository&lt;/code&gt; doesn't allow any variance, we have to use a more flexible interface which is the &lt;code&gt;IWriteOnlyRepository&lt;/code&gt;.  &lt;code&gt;RemoteEmployee&lt;/code&gt; is more specific than &lt;code&gt;Employee&lt;/code&gt;, but since our &lt;code&gt;IWriteOnlyRepository&lt;/code&gt; is marked with an &lt;code&gt;in&lt;/code&gt; keyword it doesn't mind more specific types. AKA contravariance.&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;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AddRemoteEmployees&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IWriteOnlyRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Andrew"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"Canada"&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;RemoteEmployee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Carol"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"UK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works! &lt;br&gt;
(☞ﾟヮﾟ)☞   &lt;/p&gt;

&lt;p&gt;GitHub repo with full code = &lt;a href="https://github.com/jozefchmelar/in_contra_co_variance/blob/master/Program.cs" rel="noopener noreferrer"&gt;link&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;When your interface allows you to use a single type, it offers no flexibility, no variance, therefore it's &lt;strong&gt;invariant&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can go through a list of employees and treat them as &lt;code&gt;Person&lt;/code&gt;`s, because they have a &lt;strong&gt;co&lt;/strong&gt;mmon trait - &lt;strong&gt;covariance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can't treat the list of employees as a list of managers, but you should be able to add a manager to a list of employees. This way you're approaching the problem from the opposite (contra) side - &lt;strong&gt;contravariance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope I made this weird topic a bit more clear! &lt;/p&gt;

</description>
      <category>generics</category>
      <category>dotnet</category>
      <category>covariance</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Strong named assemblies — To sign or not to sign?</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Mon, 28 Dec 2020 20:16:00 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/strong-named-assemblies-to-sign-or-not-to-sign-1nhc</link>
      <guid>https://dev.to/jozefchmelar/strong-named-assemblies-to-sign-or-not-to-sign-1nhc</guid>
      <description>&lt;p&gt;Should you sign your .NET assemblies or not? That is the question.&lt;/p&gt;

&lt;p&gt;According to the Betteridge’s law of headlines the answer should be no. Unfortunately you’re going to have to decide for yourself.&lt;/p&gt;

&lt;p&gt;I gathered some info and made a decision accordingly. Time has passed and I do not regret my decision.&lt;/p&gt;

&lt;p&gt;Here’s the data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Do not rely on strong names for security. They provide a unique identity only.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can use different versions of the same assembly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tampering with strongly signed assembly is possible. Since .NET 3.5 can be bypassed, because it was slow. &lt;a href="https://stackoverflow.com/questions/53116713/do-strong-named-assemblies-in-net-ensure-the-code-wasnt-tampered-with"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strong naming is probably causing the issue we had with app.config &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/strong-naming"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Strong named assembly can only reference other strong named assemblies &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/assembly/strong-named"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vladimir Khorikov doesn't sign his libs, since no one is doing it anymore &lt;a href="https://github.com/vkhorikov/CSharpFunctionalExtensions/issues/48#issuecomment-576410858"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;According to Microsoft, you should strong sign your dlls &lt;a href="https://github.com/dotnet/docs/issues/13228#issuecomment-522776614"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This thing exists: strong namer – sign dlls which are not signed &lt;a href="https://github.com/dsplaisted/strongnamer#strong-namer"&gt;[link]&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NewtonSoft.Json, Serilog, TwinCAT.Ads - signed&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interesting blog posts&lt;a href="https://jbp.dev/blog/signing-net.html"&gt;https://jbp.dev/blog/signing-net.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;This guy changed his mind&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;March 2016 &lt;a href="https://www.pedrolamas.com/2016/03/01/still-strong-naming-your-assemblies-you-do-know-its-2016-right/"&gt;Still Strong-Naming your Assemblies? You do know 's 2016, right?&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;September 2018 &lt;a href="https://www.pedrolamas.com/2018/09/11/start-strong-naming-your-assemblies/"&gt;Start Strong-Naming your Assemblies!&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;To those still skeptical of strong-naming their assemblies, I propose a compromise solution: do strong-name your assemblies, but only increment the assembly version for major releases! Json.NET has been using this approach to avoid binding redirects since 2014, and as far as I can tell, with relative success!&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This git diff &lt;a href="https://github.com/dotnet/corefx/pull/33784/files#"&gt;&lt;/a&gt;&lt;a href="https://github.com/dotnet/corefx/pull/33784/files#"&gt;https://github.com/dotnet/corefx/pull/33784/files#&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Microsoft recommends to sign assemblies; critical libraries are signed as well. We want to ensure that specific versions will always work with each other. We should also able to sign dlls which are not signed by default. Therefore, I recommend to &lt;strong&gt;use strong naming.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Simple tip for your product meetings.</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Mon, 12 Oct 2020 17:22:39 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/simple-tip-for-your-product-meetings-g05</link>
      <guid>https://dev.to/jozefchmelar/simple-tip-for-your-product-meetings-g05</guid>
      <description>&lt;h1&gt;
  
  
  I'm busy
&lt;/h1&gt;

&lt;p&gt;Print "KEEP IT LEAN" on a piece of paper and put the paper on a visible place during your meeting.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.google.com/document/d/1NiWGtBV-Uawk6bTISbb65JCgMO8ijHBVSixdSKe8qxw/edit?usp=sharing"&gt;Like this&lt;/a&gt; &lt;/p&gt;




&lt;h2&gt;
  
  
  I have a minute
&lt;/h2&gt;

&lt;p&gt;Marketing, SEO, BizDev, Product owner and WebDev people. &lt;/p&gt;

&lt;p&gt;We spent two days locked up in a meeting room with a simple goal.  &lt;/p&gt;

&lt;p&gt;Design a website which would enable our clients to buy and manage their licenses.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2 days is way too long.&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I was so upset, angry and tired after the first day. It felt like we have done a big amount of nothing. &lt;/p&gt;

&lt;p&gt;Next morning, I printed out "KEEP IT LEAN" on a piece of paper with the biggest font that fit. &lt;a href="https://docs.google.com/document/d/1NiWGtBV-Uawk6bTISbb65JCgMO8ijHBVSixdSKe8qxw/edit?usp=sharing"&gt;Here I prepared it for you&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now when we heard a feature like: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Merge private accounts with company accounts so they can (whatever was the argument)"&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Someone would just grab that piece of paper and remind everyone that that's not the problem we're solving. &lt;/p&gt;

&lt;p&gt;We were done by lunch-time. &lt;/p&gt;

</description>
      <category>productivity</category>
      <category>management</category>
      <category>startup</category>
    </item>
    <item>
      <title>Soft skills - communication tips</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Wed, 27 Nov 2019 20:42:25 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/soft-skills-communication-tips-4dh3</link>
      <guid>https://dev.to/jozefchmelar/soft-skills-communication-tips-4dh3</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--azklMLJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AoHf0kbmGECw94pFw" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--azklMLJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AoHf0kbmGECw94pFw" alt="unsplash.com/@akson"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@akson


&lt;h1&gt;
  
  
  Your next promotion
&lt;/h1&gt;

&lt;p&gt;What do you think will land you your next promotion? Next framework you're going to learn? Haskell programming? Great architecture on that app you developed?&lt;/p&gt;

&lt;p&gt;Yeah. Maybe.&lt;/p&gt;

&lt;p&gt;You might be the best programmer around your office. However, in real world you don't have to be THE BEST to be promoted. If you can't talk to a client or no one in the office wants to talk to you, do you think you'll be promoted? &lt;/p&gt;

&lt;p&gt;Yeah. No. &lt;/p&gt;
&lt;h1&gt;
  
  
  Introduce yourself with a memorable association
&lt;/h1&gt;

&lt;p&gt;I’m Jozef and I like wakeboarding🏄‍♀️!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jacob 😊&lt;/li&gt;
&lt;li&gt;Emma👩&lt;/li&gt;
&lt;li&gt;Lucas 🧑🏿&lt;/li&gt;
&lt;li&gt;Sarah👧🏼&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They have one thing in common. You already forgot their names…and they forgot yours.&lt;/p&gt;

&lt;p&gt;People forget names way too often. My name is the sweetest, best sounding song in the world. And I love it! And you love yours. It’s the sound you hear even when no one said it…&lt;/p&gt;

&lt;p&gt;What was the name of that guy with wake-board 🤔? &lt;strong&gt;Jozef&lt;/strong&gt; !&lt;/p&gt;

&lt;p&gt;Make it easy to remember your name :) It makes it easier to talk to you.&lt;/p&gt;
&lt;h1&gt;
  
  
  You are worthy
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mleOj9Di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AH-aYY6Z_VoKNdohP" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mleOj9Di--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AH-aYY6Z_VoKNdohP" alt="unsplash.com/@andrewtneel"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@andrewtneel



&lt;p&gt;Once you put someone on a pedestal there’s only way they can look at you. You’ll always be bellow them. Don’t allow this to happen. Your time has value, you have value.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I'm sorry for the code I wrote  → Yay! Good that you noticed. &lt;/li&gt;
&lt;li&gt;I’m sorry that I’m late → Thank you for waiting for me&lt;/li&gt;
&lt;li&gt;I’m sorry about the delay → Thanks for your patience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Constant apologizing may appear as you are not confident enough to appreciate yourself.&lt;/p&gt;

&lt;p&gt;Next time you’re going to have job interview, you can refer to it as a meeting about future cooperation. Remember that the company &lt;strong&gt;wants you&lt;/strong&gt; and you want to work there.&lt;/p&gt;
&lt;h1&gt;
  
  
  Wallet is a very sensitive part of the body
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9aohc82Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AzrEgHOKXpEBOgjEq" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9aohc82Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AzrEgHOKXpEBOgjEq" alt="unsplash.com/@andrea_natali"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@andrea_natali



&lt;p&gt;I found myself in a situation when I was the "sales guy" even though I'm a developer. It might happen to you! In this case, these are my notes.&lt;br&gt;
I would like to offer you a presentation of our latest product…Might be the most NO-NO sentence in the universe.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Product&lt;/li&gt;
&lt;li&gt;Sale&lt;/li&gt;
&lt;li&gt;Presentation&lt;/li&gt;
&lt;li&gt;Offer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Especially these words make me feel like you want to get into my wallet. I don’t like that. Don’t make it so obvious, silly.&lt;/p&gt;

&lt;p&gt;I could show you the result of our work, I believe it may be beneficial for you. — Sounds a bit better, doesn’t it?&lt;/p&gt;
&lt;h1&gt;
  
  
  But, why?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qbkuu31f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AQdBjK-cwgQfw2NAP" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qbkuu31f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AQdBjK-cwgQfw2NAP" alt="unsplash.com/@jannerboy62"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@jannerboy62



&lt;ul&gt;
&lt;li&gt;I want to give you a raise, but bla bla bla bla…&lt;/li&gt;
&lt;li&gt;Your job performance is great, but blah blah bla blah…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This translates into :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I don’t want give you a raise.&lt;/li&gt;
&lt;li&gt;Your job performance sucks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice the “.” at the end of translated sentences. They end right there. Whatever follows “but” is just irrelevant. You already know this, but…&lt;/p&gt;

&lt;p&gt;I recommend to avoid this word. Nonetheless there’s use for it, when you turn your sentence around.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Why did you put this code…&lt;/li&gt;
&lt;li&gt;Why is it written like this…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Starting a sentence with why feels very accusingly or threatening. Just use different words&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What’s the reason for …&lt;/li&gt;
&lt;li&gt;What was the thinking behind this …&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Be positive
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yEBTlQXi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AioC8CJfgNtoTBbYR" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yEBTlQXi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AioC8CJfgNtoTBbYR" alt="unsplash.com/@adamjang"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@adamjang



&lt;p&gt;Next time you’re going to write an email try to sound friendly, positive. It’s very important to be perceived as a positive person — not a trouble maker.&lt;/p&gt;

&lt;p&gt;It’s not easy to be positive. You have to change your thinking a bit.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I have to go for a run.&lt;/li&gt;
&lt;li&gt;I have to go to the gym.&lt;/li&gt;
&lt;li&gt;I have to go to school.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They all sound very bad on their own. But once you attach a goal or something bigger to your activity…it’s not that bad 😊&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I want to be thin and healthy so I’m going for a run!&lt;/li&gt;
&lt;li&gt;I want to have that beach body so I have to lift some weights!&lt;/li&gt;
&lt;li&gt;I want to be educated and well perceived in the future so I want to study
Remember that you and the person on the other side of the line, may have more in common than you think :).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nLdwJEbx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AIxHBvyDZWOjmqUwF" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nLdwJEbx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1000/0%2AIxHBvyDZWOjmqUwF" alt="unsplash.com/@alex_tsl"&gt;&lt;/a&gt;&lt;/p&gt;
unsplash.com/@alex_tsl



&lt;p&gt;You may have heard a lot of tips like this. It’s easy to forget about them unless you apply them in your everyday life.&lt;/p&gt;

&lt;p&gt;Communication is something you will train for the rest of your life. Just pick a few from this tips…one at a time and you’ll be amazed by the results.&lt;/p&gt;

&lt;p&gt;Your customers will prefer you before that trouble maker. You will be the problem solver.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/jozefchmelar"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kgM3r2o4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/213/1%2A9vg3-OY14aZN1UpKwIxxZg.png" alt="I'm just really curious if there are ppl like this"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>communication</category>
      <category>softskills</category>
      <category>management</category>
    </item>
    <item>
      <title>How I built a simple compiler — Markdown to HTML</title>
      <dc:creator>Jozef Chmelar</dc:creator>
      <pubDate>Fri, 22 Nov 2019 12:12:41 +0000</pubDate>
      <link>https://dev.to/jozefchmelar/how-i-built-a-simple-compiler-markdown-to-html-9g1</link>
      <guid>https://dev.to/jozefchmelar/how-i-built-a-simple-compiler-markdown-to-html-9g1</guid>
      <description>&lt;p&gt;A colleague of mine, who happens to be my ̶ b̶o̶s̶s̶  leader wrote a really cool compiler. It’s not a cool CRUD app or complicated animation.&lt;/p&gt;

&lt;p&gt;IT’S A COMPILER. Used in the production environment. And it’s not backed up by a community or a big company.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Um3ljJl8jrnHy/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Um3ljJl8jrnHy/giphy.gif" alt="MINDBLOW"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think that’s pretty cool…I would like to code cool stuff...So I decided I’m going to write a compiler!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So I did&lt;/em&gt;…because I could do following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn more about the magic of compilers&lt;/li&gt;
&lt;li&gt;Learn something else than a new JavaScript framework&lt;/li&gt;
&lt;li&gt;Achieve one of my goals.&lt;/li&gt;
&lt;li&gt;Code something different.&lt;/li&gt;
&lt;li&gt;Understand the codebase more in-depth&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How?
&lt;/h2&gt;

&lt;p&gt;Foundation of my compiler is built on top of &lt;a href="https://github.com/IronyProject/Irony" rel="noopener noreferrer"&gt;Irony&lt;/a&gt;. It’s a .NET Language Implementation Kit. Unfortunately, there’s not a lot of info or tutorials online. Even with the little amount of online documentation, I was able to define language grammar, so I can parse the input.&lt;/p&gt;

&lt;p&gt;I’m using Irony’s NonTerminal and Terminal classes to define my markdown.&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Grammar
&lt;/h2&gt;

&lt;p&gt;Now it’s time to define Grammar rules. Grammar is defined using &lt;a href="https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_form" rel="noopener noreferrer"&gt;Backus-Naur form&lt;/a&gt;. Irony overloads C# operators and makes it really easy to define grammar.&lt;/p&gt;


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


&lt;p&gt;It’s so easy to read. Let’s dive in.&lt;/p&gt;

&lt;p&gt;Bold text has to start with a &lt;em&gt;star *&lt;/em&gt; followed by a &lt;em&gt;Text&lt;/em&gt; ending with a &lt;em&gt;star *&lt;/em&gt;. &lt;em&gt;Text&lt;/em&gt; can be &lt;em&gt;StyledText&lt;/em&gt; or &lt;em&gt;PlainText&lt;/em&gt;. But &lt;em&gt;StyledText&lt;/em&gt; can &lt;em&gt;Bold, Italics&lt;/em&gt; or U͟n͟d͟e͟r͟s͟c͟o͟r͟e͟. Do you see the little hack? Now &lt;em&gt;Bold&lt;/em&gt; text can also be underscore and italics! Same thing with Header. Underscore header? No problem, since it fits our grammar rules.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F1280%2F0%2AaYgkI4mqH6eoqreU.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%2Fmiro.medium.com%2Fmax%2F1280%2F0%2AaYgkI4mqH6eoqreU.jpg" alt="It's not THAT complicated"&gt;&lt;/a&gt;&lt;br&gt;It's not THAT complicated
&lt;/p&gt;

&lt;p&gt;If you notice the definition of NonTerminals and Terminals you will see the second parameter is a type of Ast node. This way I could map my parse tree to AST. It was really a pain to achieve this. I searched StackOverflow, second page of google results…no luck. My solution might not be the best in the world but it works. It’s a function which creates an instance of the type you provide it with.&lt;/p&gt;


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


&lt;p&gt;It’s much easier to work with AST than parse tree :) That’s why you do it.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F936%2F1%2AhgUs6W3ldETMAP5J9JLe1Q.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%2Fmiro.medium.com%2Fmax%2F936%2F1%2AhgUs6W3ldETMAP5J9JLe1Q.png" alt="Parse vs AST tree"&gt;&lt;/a&gt;&lt;br&gt;Parse vs AST tree
&lt;/p&gt;
&lt;h2&gt;
  
  
  From Abstract Syntax Tree to HTML
&lt;/h2&gt;

&lt;p&gt;Grammar is defined, AST looks good. Now I had to do a thing which I’ve been successfully avoiding for like 5 years. &lt;strong&gt;I had to learn the &lt;a href="https://en.wikipedia.org/wiki/Visitor_pattern" rel="noopener noreferrer"&gt;visitor pattern&lt;/a&gt;&lt;/strong&gt;. It makes much more sense now, than in a university few years ago.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/11gC4odpiRKuha/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/11gC4odpiRKuha/giphy.gif" alt="Accurate description of visitor pattern"&gt;&lt;/a&gt;&lt;br&gt;Accurate description of visitor pattern
&lt;/p&gt;

&lt;p&gt;This is how Irony accepts Visitor when traversing the tree.&lt;/p&gt;


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


&lt;p&gt;And this is how I wrote my HTML visitor. Before you visit a node write you tag, after that he will visit every child.&lt;/p&gt;


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


&lt;p&gt;When you encounter PlainTextAst, output the text we parsed&lt;/p&gt;


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


&lt;p&gt;And now just visit the root&lt;/p&gt;


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


&lt;h2&gt;
  
  
  Voilà!
&lt;/h2&gt;

&lt;p&gt;From this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#"H1 Heading"
#_"H1 undescore heading"_
##"H2 Heading"
*"This is bold text"* "and this is regular text"
"Following three items should be in a list, this is plaintext"
- "Plaintext list item"
- *"Bold List item"*
- _*"Underscore bold list item"*_
- _"Underscore  list item"_
#"This is heading again"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To this&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;h1&amp;gt;H1 Heading&amp;lt;/h1&amp;gt;
  &amp;lt;h1&amp;gt;
    &amp;lt;u&amp;gt;H1 undescore heading&amp;lt;/u&amp;gt;
  &amp;lt;/h1&amp;gt;
  &amp;lt;h2&amp;gt;H2 Heading&amp;lt;/h2&amp;gt;
  &amp;lt;b&amp;gt;This is bold text&amp;lt;/b&amp;gt;
  &amp;lt;p&amp;gt;and this is regular text&amp;lt;/p&amp;gt;
  &amp;lt;p&amp;gt;Following three items should be in a list, this is plaintext&amp;lt;/p&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;li&amp;gt;Plaintext list item&amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;b&amp;gt;Bold List item&amp;lt;/b&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;u&amp;gt;
        &amp;lt;b&amp;gt;Underscore bold list item&amp;lt;/b&amp;gt;
      &amp;lt;/u&amp;gt;
    &amp;lt;/li&amp;gt;
    &amp;lt;li&amp;gt;
      &amp;lt;u&amp;gt;Underscore  list item&amp;lt;/u&amp;gt;
    &amp;lt;/li&amp;gt;
  &amp;lt;/ul&amp;gt;
  &amp;lt;h1&amp;gt;This is heading again&amp;lt;/h1&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  It works!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/a0h7sAqON67nO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/a0h7sAqON67nO/giphy.gif" alt="Great success"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I wanted to write a compiler from my markdown to HTML. I picked up great available tools to achieve it and &lt;strong&gt;did it&lt;/strong&gt;. You don’t have to start in assembly to write your own parser, compiler, language, operating system…&lt;/p&gt;

&lt;p&gt;Try it. Do it. Solve it.&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/jozefchmelar" rel="noopener noreferrer"&gt;
        jozefchmelar
      &lt;/a&gt; / &lt;a href="https://github.com/jozefchmelar/MarkdownCompiler" rel="noopener noreferrer"&gt;
        MarkdownCompiler
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      My tiny compiler :)  https://jozefchmelar.com/MarkdownCompiler/
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/jozefchmelar" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F213%2F1%2A9vg3-OY14aZN1UpKwIxxZg.png" alt="I'm just really curious if there are ppl like this"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>compiler</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
