<?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: Dane Balia</title>
    <description>The latest articles on DEV Community by Dane Balia (@daneb).</description>
    <link>https://dev.to/daneb</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%2F172549%2F85352b55-585a-4da8-8b81-6ac93075f170.jpeg</url>
      <title>DEV Community: Dane Balia</title>
      <link>https://dev.to/daneb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/daneb"/>
    <language>en</language>
    <item>
      <title>.NET Core: Deploy exe as Service</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 04 Sep 2020 15:14:13 +0000</pubDate>
      <link>https://dev.to/daneb/net-core-deploy-exe-as-service-5eh3</link>
      <guid>https://dev.to/daneb/net-core-deploy-exe-as-service-5eh3</guid>
      <description>&lt;p&gt;So you've built a .NET core executable, it's long running (&lt;strong&gt;while(true)&lt;/strong&gt;) and is ready for deploy to your server. Your initial process involves a manual network copy and some wiring on the server to ensure it runs as a service. Maybe a service wrapper? 🐱‍🚀 After a few refactors and bug fixes with multiple deploys, the process starts to feel a bit like smuggling sand across the Kalahari Desert.&lt;/p&gt;

&lt;p&gt;We need a hero.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2DZN6Nf1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/72j56xsff1asxy0nei3v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2DZN6Nf1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/72j56xsff1asxy0nei3v.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's get to it. The hero of choice for this adventure movie is &lt;a href="https://github.com/ansible/ansible"&gt;Ansible&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Requirements:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;WSL2 (running Ubuntu)&lt;/li&gt;
&lt;li&gt;Visual Studio Code / Studio&lt;/li&gt;
&lt;li&gt;A Linux server  / localhost &lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html#action-check-your-ssh-connections"&gt;SSH access via authorized key&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What is Ansible&lt;/li&gt;
&lt;li&gt;Install Ansible&lt;/li&gt;
&lt;li&gt;Setup our configuration structure&lt;/li&gt;
&lt;li&gt;Define our Recipe&lt;/li&gt;
&lt;li&gt;Deploy 🎇&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 1: What is Ansible?
&lt;/h2&gt;

&lt;p&gt;--snip snip--&lt;br&gt;
Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems&lt;/p&gt;

&lt;p&gt;In order words, it's our deploy tool.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Installing Ansible
&lt;/h2&gt;

&lt;p&gt;Installation on your flavor of Linux is nothing more than,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# &amp;lt;your package manager&amp;gt; install ansible
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For Ubuntu the following would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The installation is not a service but nothing more than a set of executables that you will use to feed a basic recipe to configure the deployment process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Setup our configuration structure
&lt;/h2&gt;

&lt;p&gt;For those who would like to follow on without writing any code I've included a &lt;a href="https://github.com/daneb/InfiniteHelloWorlds"&gt;Github repository&lt;/a&gt; with the sample.&lt;/p&gt;

&lt;p&gt;In my project I usually create a deploy folder as a form of separating concerns. Thereafter, I create my &lt;a href="https://wiki.debian.org/systemd/Services"&gt;SystemD&lt;/a&gt; service file that we will setup on our remote host so we can stop and start our service. You can find a sample service file &lt;a href="https://github.com/daneb/InfiniteHelloWorlds/blob/master/InfiniteHelloWorlds/Deploys/infinitehelloworlds.service"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OuFH6xCn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ee7f21bsv22dv2vg4yug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OuFH6xCn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ee7f21bsv22dv2vg4yug.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next we will create a playbook YAML file that will be our configuration, and a hosts YAML file that will represent the host/s we want to deploy too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mJyev-Wq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ayf3bfnmrlbm5xcz3kla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mJyev-Wq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ayf3bfnmrlbm5xcz3kla.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay so now we have all we need, let's do the configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Define Our Recipe
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Host configuration
&lt;/h3&gt;

&lt;p&gt;Edit your &lt;a href="https://github.com/daneb/InfiniteHelloWorlds/blob/master/InfiniteHelloWorlds/Deploys/hosts.yml"&gt;hosts.yml&lt;/a&gt; in the Deploys folder to match the following. This basically supports a configuration of one or more hosts that we could deploy our service too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2lNS4za4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ed53a84fp0ty1ftvs96p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2lNS4za4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ed53a84fp0ty1ftvs96p.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Line 4 is probably the most important as it's the key cross reference (custom name) we leverage in our play book or recipe for the deployment. For more information on configuring your inventory, check out the &lt;a href="https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html"&gt;ansible documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;** Be advised you can do this cleanly with an Ansible task, but I haven't, so you will need to create the remote directory or production folder on the remote host.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deployment playbook
&lt;/h3&gt;

&lt;p&gt;Edit your &lt;a href="https://github.com/daneb/InfiniteHelloWorlds/blob/master/InfiniteHelloWorlds/infinitehelloworlds.yml"&gt;playbook file&lt;/a&gt; and lets add the following code. Anything related to "InfiniteHelloWorlds" can be replaced with your executable name. Just remember, Linux is case sensitive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--03ws3YO0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4i4l7kcjs9gtsawwk017.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--03ws3YO0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4i4l7kcjs9gtsawwk017.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;hosts&lt;/em&gt; - is that cross reference we mentioned earlier from the hosts.yml.&lt;br&gt;
&lt;em&gt;vars&lt;/em&gt; - represent our variables that will use within the configuration itself, the tasks. The production folder is the destination folder of the host to which you will deploy. The linux_release_path is the location of the executable after a dotnet publish as per below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ dotnet publish --framework netcoreapp3.1 --runtime linux-x64 -c Release
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next aspect are the actual &lt;em&gt;tasks&lt;/em&gt;. This configuration will drive the work of deployment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NlDBU_BP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wcabz0mj4evhbp3tnht2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NlDBU_BP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wcabz0mj4evhbp3tnht2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many kinds of tasks in ansible that apply to deploys for  Windows and Linux, you can check that out &lt;a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#tasks-list"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/daneb/InfiniteHelloWorlds/blob/master/InfiniteHelloWorlds/infinitehelloworlds.yml"&gt;tasks are broken&lt;/a&gt; down into the following functionality,&lt;/p&gt;

&lt;h4&gt;
  
  
  Task Steps by Name
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Compress directory before send&lt;/em&gt;: Compress published directory found in working directory of .NET Core project.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Stop service&lt;/em&gt;: &lt;strong&gt;If the first deploy, you may want to comment this out as the service has not been setup on the host.&lt;/strong&gt; This is there for ongoing deployments. This can also be done via an Ansible task.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Copy release files to host&lt;/em&gt;: will then copy the archive/zip to the remove host to the production folder specified in the &lt;strong&gt;vars&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Unarchive the code&lt;/em&gt;: involves un-compressing the archive onto the local disk of the host.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Copy systemd file&lt;/em&gt;: will then copy from the local unarchived file contents the service file and configure the service by placing the .service file in the &lt;strong&gt;SystemD&lt;/strong&gt; configuration folder.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Restart the service&lt;/em&gt;: lastly, call restart on the service.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And there you have it. All configured and ready to deploy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Deploy
&lt;/h2&gt;

&lt;p&gt;Let's deploy in the project folder (not solution).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ansible-playbook infinitehelloworlds.yml -i Deploys/hosts.yml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That should lead to a fully deployed service, and this is what success will look like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cjx0c4Y3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zi9tj8cic7ced5tmyii7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cjx0c4Y3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zi9tj8cic7ced5tmyii7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have just really touched on the basics today, and this is still pretty rudimentary and not resilient enough. But it's sufficient for a good start and a foundation for more advanced deploys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging
&lt;/h3&gt;

&lt;p&gt;To troubleshoot the running of your service, you can stop and start the service separately; run the deploy with verbose output, execute a specific step or even run test commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ansible-playbook --help
$ ansible-playbook InfiniteHelloWorlds/Deploys/infinitehelloworlds.yml -v
$ systemctl stop infinitehelloworlds
$ systemctl start infinitehelloworlds
$ journalctl --lines 100 
$ ansible all -m ping
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;HTH!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>ansible</category>
      <category>dotnet</category>
      <category>deployment</category>
    </item>
    <item>
      <title>Research Methods At A Glance</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Mon, 30 Dec 2019 08:54:44 +0000</pubDate>
      <link>https://dev.to/daneb/research-methods-distilled-hjm</link>
      <guid>https://dev.to/daneb/research-methods-distilled-hjm</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2cnS1WDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/ResearchMethods.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2cnS1WDb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/ResearchMethods.jpg" alt="Research Methods "&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>career</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Light bulbs of the week (Week 51 2019)</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 20 Dec 2019 09:33:33 +0000</pubDate>
      <link>https://dev.to/daneb/light-bulbs-of-the-week-week-51-2019-ee5</link>
      <guid>https://dev.to/daneb/light-bulbs-of-the-week-week-51-2019-ee5</guid>
      <description>&lt;h2&gt;
  
  
  What did I learn this week?
&lt;/h2&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Monolith vs Micro-kernel&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ebGBKGHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/MonolithvsMicro.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ebGBKGHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/MonolithvsMicro.jpg" alt="Comparison"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Outcomes over Output&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=NfrxSOtUWqI&amp;amp;feature=youtu.be"&gt;How to reason about value&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3yqS9KaS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/Outcome.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3yqS9KaS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/Outcome.jpg" alt="Outcome"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>agile</category>
      <category>kernel</category>
      <category>linux</category>
      <category>windows</category>
    </item>
    <item>
      <title>Quick Tour of C# Generic Type Constraints </title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 02 Aug 2019 14:41:10 +0000</pubDate>
      <link>https://dev.to/daneb/quick-tour-of-c-generic-type-constraints-3l5h</link>
      <guid>https://dev.to/daneb/quick-tour-of-c-generic-type-constraints-3l5h</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.to/daneb/quick-tour-of-generics-in-c-2fea"&gt;Last time&lt;/a&gt; we looked at some fundamentals to Generics. Let's peel a little more of the onion regarding Generics and consider Type Constraints.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;em&gt;Type Constraint&lt;/em&gt; is the restriction imposed on a type used by a generic.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;AGenericClass&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;IComparable&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="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;class&lt;/span&gt; &lt;span class="nc"&gt;UsingEnum&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;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Enum&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GenericList&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;Employee&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Constraints inform the compiler about the capabilities a type argument must have by restricting it. Without any constraints, the type argument could be any type. Imposing the constraint/s on the generic &lt;em&gt;increases the number of allowable operations and method calls to those supported by the constraining type and all types in its inheritance hierarchy&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Clarification
&lt;/h3&gt;

&lt;p&gt;So if I had a wooden drawer, it could pretty much take anything. In fact, it would most likely be very difficult to order things and keep things in their place. And with the continual opening and closing of the drawer, one is also unlikely to efficiently access their papers, pens, rulers, stapler etc.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UcYvLhP3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.stevesshelves.com/wp-content/uploads/2012/06/Logo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UcYvLhP3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.stevesshelves.com/wp-content/uploads/2012/06/Logo.png" alt="Drawer"&gt;&lt;/a&gt;&lt;br&gt;
But assume we have a drawer compartmentalized by inner linings of wood. All of a sudden, things have a place and are more structured in terms of access. And this is what Type constraints do. They impose restrictions to provide a more human and safe interface to the Generic type - &amp;lt;T&amp;gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hP9Pks8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/7constraints.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hP9Pks8n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://raw.githubusercontent.com/daneb/dev.to/master/7constraints.png" alt="Seven Constraints"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 7 constraints one could consider when using a Generic, so let's review a few to get an idea of how to use them.&lt;/p&gt;
&lt;h3&gt;
  
  
  Value Type Constraint
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&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="k"&gt;struct&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This constrains the type argument to a value and not a reference type.&lt;br&gt;
Examples of what would be &lt;em&gt;valid&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Votes&amp;lt;int&amp;gt;&lt;/li&gt;
&lt;li&gt;UploadedFiles&amp;lt;FileMode&amp;gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples of what would be &lt;em&gt;invalid&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap&amp;lt;object&amp;gt;&lt;/li&gt;
&lt;li&gt;ReportContent&amp;lt;StringBuilder&amp;gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Reference Type Constraint
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&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="k"&gt;class&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This constrains the type argument to be a reference type and not a value type.&lt;br&gt;
Examples are simply the reverse of what we just saw in the &lt;strong&gt;Value Type Constraint&lt;/strong&gt;. That is, the type argument &amp;lt;T&amp;gt; can be any class, interface, array or delegate.&lt;/p&gt;
&lt;h3&gt;
  
  
  Constructor Type Constraint
&lt;/h3&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&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="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This constrains the type argument to have a public parameter-less constructor. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;snip taken from &lt;a href="https://csharpindepth.com/"&gt;C# in Depth&lt;/a&gt;&lt;/em&gt;&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="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;CreateInstance&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="k"&gt;new&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;T&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;This methods returns a new instance of whatever type you specify, constrained of course by the fact that the type must have no parameters in its constructor. This is a useful constraint when working with the &lt;a href="https://sourcemaking.com/design_patterns/factory_method"&gt;Factory Method Design Pattern&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Sample Using Interface Constraint
&lt;/h3&gt;


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



&lt;p&gt;In the sample above, we see the creation of a rudimentary &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/tuples"&gt;Tuple&lt;/a&gt;. The type &lt;em&gt;MyTuple&lt;/em&gt; has an &lt;em&gt;interface type constraint&lt;/em&gt;. Thus the type argument must be or implement &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.iequatable-1?view=netframework-4.8"&gt;IEquatable&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;When developing our &lt;em&gt;MyTyple&lt;/em&gt;; &lt;strong&gt;IEquatable&lt;/strong&gt; enforces the following constraints: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You must implement it's method - Equals(&amp;lt;T&amp;gt;)&lt;/li&gt;
&lt;li&gt;And you should override the base class implementations of Equals(Object) and GetHashCode() so that their behaviour is consistent with that of the Equals(&amp;lt;T&amp;gt;) method.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are four main generic interfaces for comparisons, two of them &lt;strong&gt;IComparer&amp;lt;T&amp;gt;&lt;/strong&gt; and &lt;strong&gt;IComparable&amp;lt;T&amp;gt;&lt;/strong&gt; are about comparing values and for ordering. That is, one value &amp;lt;=, =&amp;gt; or == too another.&lt;/p&gt;

&lt;p&gt;The other two, &lt;strong&gt;IEqualityCompare&amp;lt;T&amp;gt;&lt;/strong&gt; and &lt;strong&gt;IEquatable&amp;lt;T&amp;gt;&lt;/strong&gt; are used for comparing two items for equality to some criteria and applying a hash of an item.&lt;/p&gt;

&lt;p&gt;The consumer of the code is constrained by a contract that describes the bounds of the generic. So while the generic is supportive of variations of a kind (or type) it is prescriptive around what it should do or be capable off.&lt;/p&gt;

&lt;h3&gt;
  
  
  Last words
&lt;/h3&gt;

&lt;p&gt;Generic type constraints are nothing more then the restrictions imposed on generic types preventing abuse, but also clearly communicating the constraints for its use and providing compile-time errors.&lt;/p&gt;

&lt;h4&gt;
  
  
  For more depth, please see the resource below:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.c-sharpcorner.com/article/story-of-equality-in-net-part-three/"&gt;Introduction to IEquatable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blogs.msdn.microsoft.com/jaredpar/2009/01/15/if-you-implement-iequatablet-you-still-must-override-objects-equals-and-gethashcode/"&gt;You must implement Equal and HashCode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=netframework-4.8"&gt;IEquality Interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#why-use-constraints"&gt;Why use constraints?&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>csharp</category>
      <category>deepdive</category>
      <category>nuggets</category>
    </item>
    <item>
      <title>Quick Tour Of Generics In C#</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 19 Jul 2019 10:30:09 +0000</pubDate>
      <link>https://dev.to/daneb/quick-tour-of-generics-in-c-2fea</link>
      <guid>https://dev.to/daneb/quick-tour-of-generics-in-c-2fea</guid>
      <description>&lt;p&gt;Generics have been around since C# 2.0 and has become a tool we leverage so naturally now in C# we almost don't even think about it. So let's change that...&lt;/p&gt;

&lt;h3&gt;
  
  
  Definitions
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Essentially generics allow you parameterize types and methods. Just as normal methods have &lt;em&gt;parameters&lt;/em&gt; to tell them what &lt;em&gt;values&lt;/em&gt; to use, generic types and methods have &lt;em&gt;type parameters&lt;/em&gt; to tell them what &lt;em&gt;types&lt;/em&gt; to use. &lt;/p&gt;

&lt;h3&gt;
  
  
  Clarification
&lt;/h3&gt;

&lt;p&gt;Type parameters are placeholders for a type.&lt;/p&gt;

&lt;p&gt;There are &lt;em&gt;constructed types&lt;/em&gt; and &lt;em&gt;generic types&lt;/em&gt;.&lt;br&gt;
A constructed type is when the type arguments are specified. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dictionary&amp;lt;int, string&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;A generic type is when the type arguments are not specified. For example,&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dictionary&amp;lt;TKey, TValue&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;As per the code snippet below, writing a method to swap two &lt;em&gt;Integer&lt;/em&gt; values is pretty rudimentary. But what if later we needed to do the very same but with &lt;em&gt;Strings&lt;/em&gt;. This would result in two methods, or perhaps overloads.&lt;/p&gt;


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



&lt;p&gt;Generics to the rescue. We've isolated the type as a form of abstraction (&lt;code&gt;T&lt;/code&gt;) to act as a placeholder for the type we intend to use.&lt;/p&gt;


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


&lt;p&gt;When the method is actually called, that placeholder is replaced with the type of the values used.&lt;/p&gt;


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


&lt;p&gt;The value of Generics here is that it grants you &lt;em&gt;productivity&lt;/em&gt; improvements, &lt;em&gt;expressiveness&lt;/em&gt; and moves some safety concerns from &lt;em&gt;execution time to compile time&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Note the two ways in which the &lt;em&gt;method&lt;/em&gt; can be called. One with the type explicit, and the other implicit through &lt;em&gt;type inference&lt;/em&gt;. The compiler is inferring the &lt;em&gt;type parameters&lt;/em&gt; based on the method arguments you pass.&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;generic&lt;/em&gt; is simply a placeholder for a type.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>deepdive</category>
      <category>nuggets</category>
    </item>
    <item>
      <title>C# Value and Reference Types</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 28 Jun 2019 08:43:50 +0000</pubDate>
      <link>https://dev.to/daneb/do-you-really-grok-value-and-reference-types-eck</link>
      <guid>https://dev.to/daneb/do-you-really-grok-value-and-reference-types-eck</guid>
      <description>&lt;p&gt;&lt;strong&gt;Value&lt;/strong&gt; and &lt;strong&gt;Reference&lt;/strong&gt; types are covered frequently all over the internet, in blog posts, technical articles and forums. Why? Because it's easy to develop software for a long time without stopping to 'smell the roses'. Or in this case, understand what they are and how they work. So here's a note to self, or you.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A &lt;strong&gt;Value Type&lt;/strong&gt; is some object that is the information itself.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Reference Type&lt;/strong&gt; is the details which points to an object containing the information. The details include things like type of object etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  An Analogy:
&lt;/h2&gt;

&lt;p&gt;Let's set the table with an analogy. Kim has just written a book. It's an auto-biography. She would like to get feedback so she prints a copy at home and gives it to her husband Kanya. Later that day she stumbles onto a publicist, who is intrigued and asks to read it. She fires up her Macbook and shares the link to the original via Google Drive.&lt;/p&gt;

&lt;p&gt;As time passes the publicist feeds back and is thoroughly impressed. She has highlighted a slew of spelling mistakes and a consideration for re-writing Chapter 4. Kim goes to work immediately and in a few days is done. Updating the original and requesting that the publicist review her changes. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what's this got to do with value and reference types?&lt;/strong&gt;&lt;br&gt;
Well, when Kim made Kanya a physical copy of the original. She provided him with an independent version of the truth. In fact after all her changes during her engagement with the publicist, Kanya's copy has now become outdated and obsolete. He had a &lt;strong&gt;value type&lt;/strong&gt;. An independent copy of the source, that was detached and indifferent to modifications of the source.&lt;/p&gt;

&lt;p&gt;The publicist on the other hand, well she had a Google Drive URL, a &lt;strong&gt;reference&lt;/strong&gt; to the original. In fact as Kim made changes the publicist could see those changes in real time. The &lt;strong&gt;reference&lt;/strong&gt; provided access to the original but was not the original. &lt;/p&gt;

&lt;h2&gt;
  
  
  Some Examples
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Array types are reference types, even if the element type is a value type (so int[] is still a reference type, even though &lt;strong&gt;int&lt;/strong&gt; is a value type)&lt;/li&gt;
&lt;li&gt;Enumerations are value types.&lt;/li&gt;
&lt;li&gt;Delegates are reference types.&lt;/li&gt;
&lt;li&gt;Interfaces are reference types, but they can be implemented as values.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Stack vs Heap
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;References types live on the Heap.&lt;br&gt;
Value types lives where their declared. &lt;br&gt;
Local variables and method parameters live on the Stack.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;strong&gt;Stack&lt;/strong&gt; is a simple FIFO memory structure. When a method is invoked, the CLR bookmarks the top of the stack. The method then pushes data onto the stack as it executes. When the method completes the CLR just resets the stack to it's previous bookmark. &lt;/p&gt;

&lt;h3&gt;
  
  
  Stack offers:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;very fast access&lt;/li&gt;
&lt;li&gt;space is managed efficiently by CPU, memory will not be fragmented&lt;/li&gt;
&lt;li&gt;local variables only&lt;/li&gt;
&lt;li&gt;limit on memory size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;strong&gt;Heap&lt;/strong&gt; is a random jumble of objects not managed by automatically for you. It is a free-floating region of memory (and is larger). In multi-threaded applications each thread will have its own stack. But all the different threads will share the heap.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heap Offers:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;variables can be accessed globally&lt;/li&gt;
&lt;li&gt;no limit on memory size&lt;/li&gt;
&lt;li&gt;(relatively) slower access&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's critical to note, that a value type lives where it's declared, so if you have a class with an instance variable of float, a value type. It will live wherever the rest of the object is found and that is on the heap.&lt;/p&gt;

&lt;p&gt;A last point, is that a string in C# is a reference type. Why? Because strings can be grow quite large in size and thus have to stored on the heap. &lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://vegibit.com/reference-types-and-value-types-in-c/"&gt;Reference Types and Value Types in C - Vegitbit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://csharpindepth.com/"&gt;C# In Depth - Jon Skeet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.albahari.com/valuevsreftypes.aspx"&gt;Values vs Reference Types - Joseph Albahari&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.programmerinterview.com/data-structures/difference-between-stack-and-heap/"&gt;Difference between Stack and Heap&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>csharp</category>
      <category>deepdive</category>
    </item>
    <item>
      <title>C#'s Type System</title>
      <dc:creator>Dane Balia</dc:creator>
      <pubDate>Fri, 21 Jun 2019 12:53:56 +0000</pubDate>
      <link>https://dev.to/daneb/c-s-type-system-3cij</link>
      <guid>https://dev.to/daneb/c-s-type-system-3cij</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Exploring the Terminology:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;C# 1's type system is static, explicit, and safe.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Statically&lt;/strong&gt; typed is where each variable is of a particular type, and that type is known at compile time. In other words, the compiler does type checking at compilation time.&lt;/p&gt;

&lt;p&gt;In the test method &lt;em&gt;StaticTypeWontCompile()&lt;/em&gt; you can see the string "Hello". But because we gave it the type Object, the compiler sees its type as Object and not String. Hence the &lt;em&gt;.Length&lt;/em&gt; method being unavailable. This test won't run because the compiler will state &lt;strong&gt;object does not contain a definition for 'Length'&lt;/strong&gt;.&lt;/p&gt;


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


&lt;p&gt;&lt;strong&gt;Explicitly Typed&lt;/strong&gt; is where the type of every variables must be explicitly stated. Of course, explicit vs implicit types is only relevant in statically typed languages.&lt;/p&gt;


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


&lt;p&gt;&lt;strong&gt;Safe Type System&lt;/strong&gt; is the characteristic of code that allows the developer to be certain that a value or object will exhibit certain properties so that he/she can use it in a specific way without fear of unexpected or undefined behavior. &lt;/p&gt;

&lt;p&gt;In the method &lt;em&gt;ThisIsSafe()&lt;/em&gt; the compiler will clearly state that an &lt;em&gt;Operator '+' cannot be applied to operands of type 'int' and 'bool'&lt;/em&gt;. This safety applies for the majority of C# up until we get to Collections, Inheritance, Overriding and Interfaces as witnessed in method &lt;em&gt;ThisIsUnsafe&lt;/em&gt; which compiles perfectly.&lt;/p&gt;


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

&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://csharpindepth.com/"&gt;C# In Depth - Jon Skeet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/859186/why-is-c-sharp-statically-typed"&gt;StackOverflow Why Is CSharp Statically Typed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/2437469/what-is-type-safe-in-net"&gt;StackOverflow What is Type Safey in .NET&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/"&gt;C#s Type System - Microsoft Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>csharp</category>
      <category>deepdive</category>
    </item>
  </channel>
</rss>
