<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jeremy Buisson on Medium]]></title>
        <description><![CDATA[Stories by Jeremy Buisson on Medium]]></description>
        <link>https://medium.com/@jbuisson?source=rss-c95fa9309609------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gtmcW1pcYb1FViVHMH2LEQ.jpeg</url>
            <title>Stories by Jeremy Buisson on Medium</title>
            <link>https://medium.com/@jbuisson?source=rss-c95fa9309609------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:37:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jbuisson/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[It’s all just 0 and 1: Binary Operations]]></title>
            <link>https://jbuisson.medium.com/its-all-just-0-and-1-binary-operations-7e436909c128?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e436909c128</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[digital]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Wed, 30 Oct 2024 18:06:45 GMT</pubDate>
            <atom:updated>2024-10-30T18:06:45.815Z</atom:updated>
            <content:encoded><![CDATA[<p>We often hear or say that computers are just a bunch of 1s and 0s. This series of short articles try to explain what it means!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OwxPvo4klHcLPyTgMa4MvA.jpeg" /><figcaption>How we imagine computers using 0s and 1s</figcaption></figure><p>This article is the first part of a series; you can find the others here:</p><ul><li><a href="https://jbuisson.medium.com/its-all-just-0-and-1-understanding-binary-8530629e1d1f">PART 1 — Understanding Binary</a></li><li><a href="https://medium.com/@jbuisson/its-all-just-0-and-1-about-bit-and-bytes-683b2d2b821a">PART 2 — About Bit and Bytes</a></li><li><a href="https://medium.com/@jbuisson/its-all-just-0-and-1-binary-operations-7e436909c128"><strong>PART 3 — Binary Operations</strong></a></li></ul><h3>PART 3 — Binary Operations</h3><p>In this part of the article series about Binary, we’ll cover how Binary brings its own operations. Specifically, we’ll focus on <strong><em>Bitwise Operations</em></strong>.</p><p>While the term <strong><em>Binary Operation</em></strong> in mathematics generally refers to a calculation that combines two operands to produce another value (such as addition or subtraction), in computing, we use a different type of operation— <strong><em>Bitwise Operations</em></strong>— that operates directly on each bit within a binary number (also called a Binary string).</p><p>While you could perform arithmetic operations (addition, subtraction, multiplication, division) in binary, it’s often easier to convert binary numbers into decimals, perform the calculation, and convert the result back into binary. Working directly with binary arithmetic can be tedious and error-prone for large numbers.</p><blockquote>I’ve created a <a href="https://binary.jeremy-buisson.fr/converter">small website here</a> to be able to quickly play around with conversion, you can also check the source code from <a href="https://gitlab.com/jbuisson/binary">Gitlab here</a>.</blockquote><p>Here, we’ll focus on operations that operate at the level of each bit in a binary number. There are four primary <strong><em>Bitwise Operations</em></strong>: <strong>NOT</strong>, <strong>AND</strong>, <strong>OR</strong>, and <strong>XOR</strong>.</p><h4>NOT</h4><p>The NOT operation is the simplest one. It works on a single Binary number and reverses each bit in that number. In programming languages, the <strong>NOT</strong> operation is often represented by the <em>tilde </em>symbol ~.</p><ul><li><strong>NOT </strong>1 = <em>0</em></li><li><strong>NOT </strong>0 = <em>1</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*Zq8DAK8Lc27l9u4-R6bjSg.png" /><figcaption><strong>NOT </strong>1001₂ = 0110₂</figcaption></figure><h4>AND</h4><p>The AND operation compares two Binary numbers bit by bit. It returns <strong>1</strong> only if both bits in the same position are <strong>1</strong>. Otherwise, it returns <strong>0</strong>. In programming languages, the <strong>AND </strong>operation is often represented by the <em>ampersand </em>symbol &amp;.</p><ul><li>0 <strong>AND </strong>0 = <em>0</em></li><li>1 <strong>AND </strong>0 = <em>0</em></li><li>0 <strong>AND </strong>1 = <em>0</em></li><li>1 <strong>AND </strong>1 = <em>1</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*02txNB73OatyxmYwg15Ifg.png" /><figcaption>1010₂ <strong>AND </strong>1100₂ = 1000₂</figcaption></figure><h4>OR</h4><p>The OR operation, like AND, compares two binary numbers bit by bit. It returns <strong>1</strong> if at least one of the bits is 1. If both bits are <strong>0</strong>, the result will be <strong>0. </strong>In programming languages, the <strong>OR </strong>operation is often represented by the <em>pipe </em>symbol |.</p><ul><li>0 <strong>OR </strong>0 = 0</li><li>1 <strong>OR </strong>0 = 1</li><li>0 <strong>OR </strong>1 = 1</li><li>1 <strong>OR </strong>1 = 1</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/237/1*G7bejSezXPcyWGUJwd_R-A.png" /><figcaption>1010₂ <strong>OR </strong>1100₂ = 1110₂</figcaption></figure><h4>XOR</h4><p>The XOR (exclusive OR) operation also operates on two operands but behaves differently from the regular OR. XOR returns <strong>1</strong> only if the operands are different. That means if both bits are <strong>1</strong> or both are <strong>0</strong>, the result will be <strong>0</strong>. Otherwise, the result will be <strong>1. </strong>In programming languages, the <strong>XOR</strong> operation is often represented by the <em>caret</em> symbol ^.</p><ul><li>0 X<strong>OR </strong>0 = 0</li><li>1 <strong>XOR </strong>0 = 1</li><li>0 <strong>XOR </strong>1 = 1</li><li>1 <strong>XOR </strong>1 = 0</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/254/1*9RaMIR7ifYZu5YSrGeeYjA.png" /><figcaption>1010₂ <strong>XOR </strong>1100₂ = 0110₂</figcaption></figure><p><strong>Shift Operations</strong></p><p>In addition to <strong>NOT</strong>, <strong>AND</strong>, <strong>OR</strong>, and <strong>XOR</strong>, I think it’s worth discussing two other <strong><em>Bitwise Operations</em></strong> known as <strong>shift operations</strong>: <strong>left shift</strong> and <strong>right shift</strong>. These operations move the bits in a Binary number to the left or right, filling in the gaps with zeros. Shifting is powerful because it can quickly multiply or divide a number by powers of two without computing or conversions.</p><p><strong>Left Shift (</strong><strong>&lt;&lt;)</strong>: In a left shift operation, each bit in the binary number is moved one or more places to the left, and zeros are added to the rightmost positions. This operation effectively multiplies the number by 2 for each position shifted. For example, shifting 1010₂ (<em>10 in decimal</em>) one place to the left (1010 &lt;&lt; 1) results to 10100₂ (<em>20 in decimal</em>).</p><p><strong>Right Shift (</strong><strong>&gt;&gt;)</strong>: Similarly, the right shift operation moves one or more places to the right, and zeros are filled in the leftmost positions, effectively dividing the number by 2 for each position shifted. The difference is we can discard the leftmost 0. For example, shifting 1010₂ one place to the right (1010 &gt;&gt; 1) gives 101₂ (<em>5 in decimal</em>).</p><p>Bitwise operations like NOT, AND, OR, and XOR form the foundation for logical operations in computing, especially at the hardware level. They’re not just an abstract concept but a tool for efficiency and control, helping programmers manipulate data to the smallest level — individual bits.</p><p>I hope you liked this short series of articles, even with the huge delay for the last part.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e436909c128" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[It’s all just 0 and 1: About Bit and Bytes]]></title>
            <link>https://medium.com/codex/its-all-just-0-and-1-about-bit-and-bytes-683b2d2b821a?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/683b2d2b821a</guid>
            <category><![CDATA[digital]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[binary]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Mon, 13 Sep 2021 04:09:01 GMT</pubDate>
            <atom:updated>2024-10-31T08:23:53.694Z</atom:updated>
            <content:encoded><![CDATA[<p>We often hear or say that computers are just a bunch of 1s and 0s. This series of short articles try to explain what it means!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1pzfTaISGHAIlNt8Z4WB5A.jpeg" /><figcaption>How we imagine computers using 0s and 1s</figcaption></figure><p>This article is the first part of a series; you can find the others here:</p><ul><li><a href="https://itnext.io/its-all-just-0-and-1-understanding-binary-8530629e1d1f">PART 1 — Understanding Binary</a></li><li><a href="https://medium.com/@jbuisson/its-all-just-0-and-1-about-bit-and-bytes-683b2d2b821a"><strong>PART 2 — About Bit and Bytes</strong></a></li><li><a href="https://jbuisson.medium.com/its-all-just-0-and-1-binary-operations-7e436909c128">PART 3 — Binary Operations</a></li></ul><blockquote>After the publication of <a href="https://itnext.io/its-all-just-0-and-1-understanding-binary-8530629e1d1f">the first article</a>, I’ve created a <a href="https://binary.jeremy-buisson.fr/converter">small website here</a> to quickly play around with Binary conversions, you can also check the source code from <a href="https://gitlab.com/jbuisson/binary">Gitlab here</a>.</blockquote><h3>PART 2 — About Bit and Bytes</h3><p>Nowadays, computers and digital information are so present in our day-to-day life that we don’t even notice the usage of the Binary system. Still, we all used the terms <strong>Bit</strong> and <strong>Bytes</strong>. Would it be to buy a laptop, choose your new smartphone or compare your mobile phone plan. Most of the time, even without knowing what it is, we understood what it means.</p><p>The <strong>Bit </strong>is considered the most basic unit in computing, and the word comes from the contraction of “<strong>B</strong>inary dig<strong>it</strong>”. Each digit in a Binary number is a Bit. It can either be 0 or 1.</p><p>Even if being a very widely used unit in computers and digital communications, it isn’t part of the <a href="https://en.wikipedia.org/wiki/International_System_of_Units">International System of Unit</a>. Nevertheless, it is commonly used in the same metric way, 1.000 bits would be written <strong>1kb </strong><em>(one kilobit)</em>, 1.000.000 bits would be written <strong>1Mb </strong><em>(one megabit)</em>, etc.</p><p>The <strong>Byte </strong>is considered to be a unit of digital information equal to 8 bits. Its number of bits changed during the early days of computers, with different companies and technologies doing as they needed. With more modern communication appearing, the need to define a standard quickly becomes inevitable, and the 8-bit Byte imposed itself as being the most commonly used.</p><p>As the <strong>Bit </strong>unit, the <strong>Byte </strong>isn’t part of the International System of Unit but still commonly uses its rules. When the Bit uses the lower <strong><em>“b”</em></strong>, the Byte uses the capital <strong><em>“B”</em></strong> as its symbol, 1.000 bytes would be written <strong>1kB</strong>, 1.000.000 bytes would become <strong>1MB</strong>, etc.</p><blockquote>The kibibyte is actually the mathematically correct binary unit used by experts instead of the kilobyte used by the general public. A kibibyte is precisely 1.024 bytes, while a kilobyte is only 1.000 bytes.</blockquote><p>You may have already noticed, but this is something you may actually use more often than you would have thought! When you want to buy a new computer, most people look for its <strong>storage capacity </strong>or <strong>memory</strong> (RAM). <em>You may also look at the CPU, and don’t worry, this is also linked to 0 and 1, but we’ll discuss it later in this series of articles.</em></p><p>When we talked about a <strong>256GB </strong>(<em>Giga Bytes</em>) hard drive, it just means that it can store 256.000.000.000 Bytes. As a single Byte is composed of 8 Bits, it can persist 8 x 256 x 10⁹ different values of <strong><em>0</em></strong> and <strong><em>1</em></strong>.</p><p>It’s the same for a computer’s memory. When you have <strong>16GB </strong>of RAM, your computer can basically store up to 8 x 16 x 10⁶ different values of 0 and 1 in its Random Access Memory.</p><p>You can even check this right now! If you’re on windows, open your Task Manager (<em>right-click the taskbar at the bottom </em>of your screen). Expand it to see more details. You’ll see under the Memory tab how much each process uses your memory, the amount of data it reads on the storage, and even the amount of data used on the network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*dfGt64srcxgyTyiJdiRmWQ.png" /><figcaption>Task Manager on windows 10 showing how many Bytes each process uses.</figcaption></figure><p>In the same way, if you played with some old video game console like the <a href="https://en.wikipedia.org/wiki/Nintendo_Entertainment_System">NES</a> then <a href="https://fr.wikipedia.org/wiki/Super_Nintendo">Super Nintendo</a>, or <a href="https://fr.wikipedia.org/wiki/Mega_Drive">Sega Megadrive</a>, you must remember we called them 8-bit systems, 16-bit, 32-bit, or 64-bit for the <a href="https://fr.wikipedia.org/wiki/Nintendo_64">N64</a>. Well, you guessed right, this is the link to the system of such console being able to use a more significant chunk of memory, the more bits, the best was the console’s capabilities!</p><p>As we’ve seen, the Binary system is in fact widely used in our day-to-day life, represented as Bit and Bytes, and present in all modern systems, but how is it used exactly?</p><h4>Binary in practice</h4><p>One of the most common things we can do on a computer is to store text files. But before being able to write or read a text file, we need to quickly discuss filesystem and encoding.</p><p>A filesystem is a system for organizing and filing on a storage medium the structure of the writing, searching, reading, storing, modifying, and deleting of files in a specific way. The most common filesystem is the <strong>NTFS </strong>(New Technology File System), introduced by Microsoft in 1993 for Windows. When you create a file, the <strong>NTFS</strong> system will store all the information relative to this file (except its content) in the <strong>MFT</strong>, for Master File System. Here it will store amongst other metadata, the name, the type, the length, the permissions, the creation date, or where on the physical storage to find its content. On Windows, you can easily find this information by doing a right-click and following the “Properties” option on a single file.</p><p>The encoding could be presenting as a mapping between binary codes, and their alphabet characters equivalent. There are multiple encodings, like <strong>ASCII</strong>, <strong>UTF-8</strong>, or <strong>ISO-8859–1 </strong>because there are multiple characters set. The most basic one, the <strong>ASCII </strong>for <em>American Standard Code for Information Interchange</em>, is originally built for the English alphabet and contains 128 characters from which 95 are printable. These include the digits 0 to 9, lowercase letters a to z, uppercase letters A to Z, and punctuation symbols. Non printable characters are also called control characters, they give information on how to display the content of the document, amongst them we have the white-space, the line-break, page-break, … The ASCII is a very limited encoding, as I won’t be able to write my own first name using it, not including accents like the “<strong>é</strong>” letter: <strong>Jérémy</strong>. More modern encoding, like <strong>UTF-16 </strong>for <em>16-bit Unicode Transformation Format</em>, allows up to 1.112.064 characters using the <a href="https://en.wikipedia.org/wiki/Unicode">Unicode</a> standard, which supports 154 modern and historic scripts, as well as symbols and emoji, like 🦄 or 🤘.</p><p><strong>Text as Binary</strong></p><p>Now that we know a bit more about filesystems and encodings, we can eventually write some text in a file and read its content as binary. Let’s create a simple TEXT file (.txt) containing: “Binary ! 🤘” <em>(</em><a href="https://gitlab.com/jbuisson/binary/-/raw/integration/assets/binary.txt"><em>download this example file here</em></a><em>)</em>.</p><p>Most operating systems will consider this file to be <strong>UTF-8 </strong>by default, being the most common encoding, and will so use the <a href="https://www.utf8-chartable.de/">UTF-8 characters table</a> to display the content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*xHh7kIWIlOBtEWM6cbueqQ.png" /><figcaption>A simple text file is displayed by Notepad on Windows 10. Note that depending on the operating system or even software you are using, the emoji could be different.</figcaption></figure><p>But if we read this file using a Binary reader, either with an extension in <a href="https://code.visualstudio.com/">VS Code</a> like <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.hexeditor">Hex Editor</a> or directly using <a href="https://binary.jeremy-buisson.fr/reader">this small website</a> I’ve created here, (<a href="https://gitlab.com/jbuisson/binary">source code available on Gitlab</a>), you’ll find the following binary content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fXht13LgUvVSIQXHMSy_w.png" /><figcaption>The Binary equivalent of “Binary ! 🤘”</figcaption></figure><p>Using the <a href="https://en.wikipedia.org/wiki/ASCII#Control_code_chart">ASCII table</a>, we can confirm the Hexadecimal value of each symbols. As said before, the ASCII table only covers <strong>128 </strong>characters, from the Hexadecimal value <strong>0</strong> to <strong>7F</strong>. Anything outside of this table will mean that it’s a non ASCII character. In our example the emoji needs 4 bytes to represents itself. The first one is <strong>F0</strong>, which means “<em>I’m a 4 bytes symbols</em>”, and all the others, combined uniquely represent the 🤘 emoji.</p><p>But you don’t want to save just simple text files on a computer. For the system to understand how to read a file we can provide what we call a file <strong>header </strong>or file <strong>signature</strong>. Doing so, we add some extra Binary numbers at the beginning of the file. This sequence of bytes, inform the system or a software reading its content, how to proces it. For example, to explicitly say that our file is an UTF-8 text file, we add 3 bytes: <strong>EF BB BF</strong> <em>(</em><a href="https://gitlab.com/jbuisson/binary/-/raw/integration/assets/binary-sign.txt"><em>download this example file here</em></a><em>)</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4oWZHOarj2EVP3GpZ0_meA.png" /><figcaption>The first bytes are used to determine the file type, here the 3 hexadecimal EF BB BF values mean UTF-8.</figcaption></figure><p>There is a huge list of file signatures, and it’s in constant evolution as more and more file types exists. The most complete list I’ve found so far is from <a href="https://medium.com/u/1c99f132edc2">Gary Kessler</a> and can be found <a href="https://www.garykessler.net/library/file_sigs.html">here</a>. I’ve tried to add as many as I can in my simple binary reader, but with thousands of them and only 2 hands, it’s far from complete. Feel free to play around anyway!</p><p>Just to give you a last example, I’ve created a very simple Image file in <a href="https://en.wikipedia.org/wiki/BMP_file_format">Bitmap</a> format <em>(</em><a href="https://gitlab.com/jbuisson/binary/-/raw/integration/assets/bitmap-16.bmp"><em>download this example file here</em></a><em>)</em>. For a system to understand it’s a picture to display (and not a text file), the Hexadecimal signature is <strong>42 4D</strong>, which is “<strong>BM</strong>” decoded in ASCII.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*585w-o5vjnTzLNlmKhWcvQ.png" /><figcaption>A simple picture using the Bitmap format</figcaption></figure><p>If you look at the <a href="https://en.wikipedia.org/wiki/BMP_file_format#Example_1">Bitmap format definition</a>, you can see that the 3rd byte is the size of the file <strong>8A</strong> in Hexadecimal is equivalent to <strong>138</strong> in Decimal, meaning our file contains 138 bytes.</p><p>Don’t hesitate to play around using the <a href="https://binary.jeremy-buisson.fr/reader">binary reader here</a>, and feel free to add any questions or remarks in the comments, I’ll do my best to answer!</p><p>I hope you found this second article of the <strong>Binary</strong> series interesting. In the next one, we’ll discuss new math operations related to the Binary system and what they are used for.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=683b2d2b821a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/its-all-just-0-and-1-about-bit-and-bytes-683b2d2b821a">It’s all just 0 and 1: About Bit and Bytes</a> was originally published in <a href="https://medium.com/codex">CodeX</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[It’s all just 0 and 1: Understanding Binary]]></title>
            <link>https://itnext.io/its-all-just-0-and-1-understanding-binary-8530629e1d1f?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/8530629e1d1f</guid>
            <category><![CDATA[understanding]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[general]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[binary]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Mon, 21 Jun 2021 07:35:05 GMT</pubDate>
            <atom:updated>2024-10-31T08:23:38.977Z</atom:updated>
            <content:encoded><![CDATA[<p>We often hear or say that computers are just a bunch of 1s and 0s. This series of short articles try to explain what it means!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1pzfTaISGHAIlNt8Z4WB5A.jpeg" /><figcaption>How we imagine computers using 0s and 1s</figcaption></figure><p>This article is the first part of a series; you can find the others here:</p><ul><li><a href="https://itnext.io/its-all-just-0-and-1-understanding-binary-8530629e1d1f"><strong>PART 1 — Understanding Binary</strong></a></li><li><a href="https://jbuisson.medium.com/its-all-just-0-and-1-about-bit-and-bytes-683b2d2b821a">PART 2 — About Bit and Bytes</a></li><li><a href="https://jbuisson.medium.com/its-all-just-0-and-1-binary-operations-7e436909c128">PART 3 — Binary Operations</a></li></ul><h3><strong>PART 1 - </strong>Understanding Binary</h3><p>The first time I was introduced to Binary at school, more than 20 years ago, there was a piece of paper on the wall with this text:</p><blockquote>There is <strong>10</strong> kinds of people, those who understand binary and the others.</blockquote><p>I didn’t understand the joke at this time. Maybe it’s also the case for you, but don’t worry, before the end of your reading, you’ll be able to explain it to anyone yourself!</p><p>To count in Binary, we first need to understand how we count in our day-to-day life, without even thinking about it.</p><h4>Counting in Decimal</h4><p>The <strong><em>Decimal </em></strong>term comes from the Latin “<em>Decem” </em>which means <strong><em>Ten</em></strong>, as we use ten symbols to count, using the commonly named <a href="https://en.wikipedia.org/wiki/Arabic_numerals">Arabic Numerals</a>: <strong>0, 1, 2, 3, 4, 5, 6, 7, 8, </strong>and <strong>9.</strong></p><p><em>If you didn’t know already, in computer science, we start counting from 0, not from 1.</em></p><p>When counting, we simply move from symbol to symbol, following the given sequence. When we’re at the end of this sequence, we simply start again with a new row of symbols to the left, with each row representing a different unit. The first is the simplest one: you just use the symbol’s value. The second is the symbol’s value multiplied by ten. The third is multiplied by a hundred, etc.</p><p>By doing some math, we can explain how we calculate numbers. I’ve decomposed the number <strong>1337 </strong>into rows in the following picture, starting from index 0 to index 3. We can very quickly identify the correlation between the symbol’s row and its calculated value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*KpsDRvLBf_13orM4dIw-kg.png" /><figcaption>How every day and without even thinking about it, we decompose numbers into Decimal.</figcaption></figure><p>Decimal basically means <em>“ten symbol-based numbers”</em> or just <em>“base 10 numbers”</em>. Now we’ll see how easy it is to apply the exact same method to represent numbers in Binary, which are <em>“base 2 numbers”</em>, using only the 0 and 1 symbols.</p><p>Before things get complicated with all these numbers, we’ll use the <em>“base number”</em> subscript to distinguish them when both Decimal and Binary numbers are present. For example, 42 in Decimal would be written with the ₁₀ in subscript, and Binary would have the ₂ in subscript as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/136/1*q9u47GJf7dhn6tS1YiPtPA.png" /><figcaption>101₁₀ in decimal which is one hundred and one.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/136/1*xK_VJFkjVs1R3o1FYj6Rog.png" /><figcaption>101₂ in binary, which is equivalent to 5₁₀ in decimal as you will soon discover.</figcaption></figure><h4>Counting in Binary</h4><p>If we apply our previous schema, but we replace the base 10 with by a base 2, here is what we get for “<em>1011</em>” in Binary:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*pE-yZAuONNFg1GneENpseg.png" /><figcaption><strong>1011</strong>₂<strong> </strong>is equivalent to <strong>11</strong>₁₀</figcaption></figure><p>As you can see, it’s pretty simple. Instead of multiplying each row by <strong><em>10 </em></strong>as in Decimal, we multiply by <strong><em>2</em></strong> each new row<em> </em>to the left<em>. </em><strong><em>2</em></strong> being the number of symbols.</p><p>The above schema can be easily rewritten with the rows values instead of index, as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/139/1*z_kspyrIhMvOGHh2-OAMbw.png" /><figcaption><strong>101</strong>₂ is equivalent to <strong>5</strong>₁₀</figcaption></figure><p>Here are two more examples of how to convert Binary numbers into Decimal numbers, following the same principle:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*AGPIuw_2NYp8AgFUxTX-BQ.png" /><figcaption><strong>100011</strong>₂ is equivalent to <strong>35</strong>₁₀<strong> </strong>and <strong>10101</strong>₂ is equivalent to <strong>42</strong>₁₀</figcaption></figure><p>From here, you know exactly how to count in Binary, which is, in fact, simply converted to Decimal. But what about doing the opposite, starting from a Decimal number, calculating its Binary equivalent?</p><p>By the way, at this moment of your reading, you should already be able to understand the <em>joke</em> I wrote about earlier in the article <em>(of course, it only works written)</em>:</p><blockquote>There is <strong>10</strong> kind of people, those who understand binary and the others.</blockquote><h4>Decimal to Binary</h4><p>There are multiple ways of converting a Decimal number to a Binary (<em>including a calculator…</em>). But the one I found to be the easiest is the division by <strong><em>2</em></strong> with the remainder.</p><p>Using this method, you start with your Decimal number, divide it by 2, write the result on the left, and keep the remainder below. You then divide by 2 again this result, still holding the remainder down, and put the new result on the left again. As we divide by 2, the only remainders possible are 0 or 1. Once you reach the result 0, you can take all the remainders, and you have your Binary number.</p><p>Let’s convert the Decimal 42 into its Binary equivalent step by step:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*cmVSs9XgWqg7CGOyEyrJ_w.gif" /><figcaption><strong>42</strong>₁₀ is equivalent to <strong>101010</strong>₂</figcaption></figure><p>Let’s do the same method to convert our first decimal number 1337:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/525/1*l25Z409YOchdO1Dix0OiNA.png" /><figcaption><strong>1337</strong>₁₀ is equivalent to <strong>10100111001</strong>₂</figcaption></figure><p>From this point, you know how to read Binary numbers and convert Decimal into Binary!</p><p>But as you may have already seen, Binary numeration is quite tedious… This is why engineers in the mid-fifties created a new base for Binary numbers, a <strong><em>16 symbol-based</em></strong> numeration called <strong>Hexadecimal</strong>.</p><h4>When 16 are better than 2</h4><p>You’ve likely been in contact with Hexadecimal numbers in your life without knowing it. Some internet providers, for example, create WiFi passwords using Hexadecimal numbers. This numeration allows representing a four symbol Binary number into a single symbol Hexadecimal number, so <strong><em>1010</em></strong> would become <strong><em>A</em></strong>.</p><p>The 16 numbers are using the known Decimal numbers for the 10 first symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9), and the 6 first letters of the <a href="https://en.wikipedia.org/wiki/Latin_alphabet">Latin Alphabet</a> for the remaining symbols: A, B, C, D, E and F. Using this symbols the letters once converted to Decimal’s value are: <strong>A=10</strong>, <strong>B=11</strong>, <strong>C=12</strong>, <strong>D=13</strong>, <strong>E=14</strong> and <strong>F=15</strong>.</p><p>Now, using the exact same system as for Binary, here is how to convert Hexadecimal to Decimal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*lS4tbEDtDLhBRyYE6mDj7Q.png" /><figcaption><strong>2C8A</strong>₁₆ is equivalent to <strong>11402</strong>₁₀ which is equivalent to <strong>101 100 1000 1010</strong>₂</figcaption></figure><p>And for the Decimal to Hexadecimal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*W-gjdjutBOeteDCJMurcKQ.png" /><figcaption><strong>1337</strong>₁₀ is equivalent to <strong>539</strong>₁₆</figcaption></figure><p>And finally, converting Hexadecimal into Binary is quite simple, as each Hexadecimal symbol represents 4 Binary symbols, you can convert them separately, and concatenate the resulting Binary numbers as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*Fi-IjgfPSChAwslyP-MJXA.png" /><figcaption><strong>539</strong>₁₆ is equivalent to <strong>101 0011 1001</strong>₂</figcaption></figure><p>You know almost all the basics about Binary, how to read it, and convert it in a more convenient format! I hope you learned something and enjoyed this article as much as I enjoyed writing it. Let me know your feedback and questions in the comments!</p><blockquote><strong>UPDATE:</strong> After the publication of this article, I’ve created a <a href="https://binary.jeremy-buisson.fr/converter">small website here</a> to be able to quickly play around with conversion, you can also check the source code from <a href="https://gitlab.com/jbuisson/binary">Gitlab here</a>.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8530629e1d1f" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/its-all-just-0-and-1-understanding-binary-8530629e1d1f">It’s all just 0 and 1: Understanding Binary</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[.NET Core is Sexy — Building a Web API]]></title>
            <link>https://itnext.io/net-core-is-sexy-building-a-web-api-cdb470cc8222?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/cdb470cc8222</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Sat, 14 Sep 2019 13:52:50 GMT</pubDate>
            <atom:updated>2019-09-21T15:13:47.187Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4">.NET Core is Sexy</a> — Building a Web API</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*5fQvZr2W6ydJ1fxjLgwhcg.png" /></figure><p>Any web developer knows that new applications are increasingly oriented towards API. As seen with the examples of RESTful and GraphQL, modern patterns use a back-end API to provide data and feature a front-end application (using, among others, <strong>React</strong> or <strong>Angular</strong>). All trendy technology aims to improve and ease developers’ lives by making it as straight forward as possible to create a <strong>Web API, </strong>and <strong>.NET Core</strong> is not an exception, as you will see in this article, the third from the series <strong>.NET Core is Sexy (and you should know it)</strong>.</p><blockquote>This article is part of a series about .NET Core and how it is now as easy and efficient as any other stack like Node or Rails to create modern applications.</blockquote><blockquote><a href="https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4">.NET Core is sexy and you should know it</a></blockquote><blockquote><a href="https://medium.com/@jbuisson/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83">.NET Core is sexy — Command Line Application</a></blockquote><blockquote><strong>.NET Core is sexy — Building a Web API</strong></blockquote><p>In the<a href="https://medium.com/@jbuisson/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83"> previous part</a>, we created a multi-platform console line application to handle our curation, by adding topics, listing them and removing a completed topic. We have put in place a nice and simple architecture, using some separation between the database access — via<a href="https://docs.microsoft.com/ef/core/"> Entity Framework</a> with<a href="https://www.sqlite.org/index.html"> Sqlite</a> — and the application itself. We also took the time to create a simple but useful abstraction for the database, allowing us to easily write tests and comply to some<a href="https://blog.cleancoder.com/"> Clean Code</a> patterns.</p><blockquote><strong>TL;DR<br></strong>If you want to see the final implementation, here is the repository for the completed Web API : <a href="https://gitlab.com/jbuisson/curator/tree/webapi">https://gitlab.com/jbuisson/curator/tree/webapi</a></blockquote><h3>Let’s start from the beginning</h3><pre>git clone --branch console <a href="/service/mailto:git@gitlab.com">git@gitlab.com</a>:jbuisson/curator.git<br>cd curator<br>dotnet build</pre><p>Using these commands, we can continue our development exactly from where we stopped in the last part. First, we clone the<a href="https://gitlab.com/jbuisson/curator/tree/console"> Gitlab repository</a>, using the git branch named <strong><em>console.</em></strong> Then, we build the whole project based on the solution file (.sln) at the root of the project.</p><p>This is what you should have in terms of folders tree:</p><pre>├── data<br>├── src<br>     ├── Curator.Console<br>     └── Curator.Data<br>         ├── Curator.Data.Entities<br>         └── Curator.Data.EntityFramework<br>             ├── Curator.Data.EntityFramework.Context<br>             ├── Curator.Data.EntityFramework.Memory<br>             └── Curator.Data.EntityFramework.Sqlite<br>└── tests<br>    ├── Curator.Console.Tests<br>    ├── Curator.Data.Entities.Tests<br>    └── Xunit.AssertExtensions</pre><p>We will now add a new project, <strong>Curator.Api</strong>, in which we will create our Web API.</p><h3>Minimum Viable Api</h3><p>Here, we will concentrate on manually developing the strict minimum to have a working Web API, doing the exact same thing as our command line application.</p><p>In the first article in this series, we saw <a href="https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4#8f37">how to self-host a web server</a> and handle http requests. We will now do the same thing but using <a href="https://docs.microsoft.com/aspnet/core/mvc/overview?view=aspnetcore-2.2">ASP.NET Core MVC</a> to manage routing, controllers and all the stuff we do not want to do by ourselves.</p><pre>dotnet new console -o src/Curator.Api<br>cd src/Curator.Api<br>dotnet add package Microsoft.AspNetCore<br>dotnet add package Microsoft.AspNetCore.Mvc</pre><p>Go to the <strong><em>Program.cs</em></strong> file and setup our web hosting:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3de61e24abb1e414d38102186291391f/href">https://medium.com/media/3de61e24abb1e414d38102186291391f/href</a></iframe><p>From within our newly created <strong>Curator.Api</strong> project, run the application using the command <strong><em>dotnet run</em></strong>. This command should start the server and listen for any requests, by default, on <a href="http://localhost:5000."><em>http://localhost:5000</em>.</a> However, we have not configured any controllers, so all requests will throw a 404 not found error. Below is an example of using<a href="https://curl.haxx.se/"> <strong>cURL</strong></a> in a new terminal:</p><pre>curl -i <a href="http://localhost:5000/items">http://localhost:5000</a><br>&gt; HTTP/1.1 404 Not Found<br>&gt; Date: Sun, 08 Sep 2019 12:01:57 GMT<br>&gt; Server: Kestrel<br>&gt; Content-Length: 0</pre><p>Let’s now add a first controller to handle curation items. This controller will use our <strong>CuratorContext</strong> to interact with the <strong>Sqlite</strong> database’s <strong>Items</strong>, so we need to start by adding references to our already existing projects<strong>.</strong></p><pre>dotnet add reference ../Curator.Data/Curator.Data.Entities</pre><pre>dotnet add reference ../Curator.Data/Curator.Data.EntityFramework/Curator.Data.EntityFramework.Context</pre><pre>dotnet add reference ../Curator.Data/Curator.Data.EntityFramework/Curator.Data.EntityFramework.Sqlite</pre><p>Next, here is the code with the controller itself, in the <strong><em>Program.cs</em></strong> file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb808565714d8b51727ce2a1012a34e3/href">https://medium.com/media/cb808565714d8b51727ce2a1012a34e3/href</a></iframe><p>The important lines of code here are the class and method’s <a href="https://docs.microsoft.com/dotnet/csharp/programming-guide/concepts/attributes/">attributes</a>, defining the <strong>URL</strong> and the request parameters we need.</p><p><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.routeattribute"><strong>Route</strong></a><strong>(“items”)]</strong> declares the controller’s endpoint. All action methods in this controller will inherit from this <strong>URL</strong>, as it is on the whole controller class.</p><p><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.httpgetattribute"><strong>HttpGet</strong></a><strong>]</strong> declares the following method to be only available for GET HTTP request. There is no routing path defined, so it will be at the root path of the controller: <strong>/<em>items</em></strong>.</p><p><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.httpgetattribute"><strong>HttpPost</strong></a><strong>]</strong> does the same thing as <strong>[HttpGet]</strong>, but for POST HTTP requests.</p><p><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.frombodyattribute"><strong>FromBody</strong></a><strong>]</strong> allows us to add a string parameter to the <strong><em>Post()</em></strong> method, which will be parsed from the request body. ASP.NET Core will handle the format for us depending on the request<a href="https://developer.mozilla.org/docs/Web/HTTP/Headers/Content-Type"> <strong>Content-Type</strong></a>.</p><p><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.httpdeleteattribute"><strong>HttpDelete</strong></a><strong>(“{id:int}”)]</strong> declares the following method to be only available for DELETE HTTP requests, and it adds a named parameter to the path that must be an integer and will be available by its name in the <strong><em>Delete()</em></strong> method.<br>Example path: <strong>/<em>items</em>/<em>42</em></strong>.</p><p>Finally, we need to add some configuration for the <strong>Sqlite</strong> data-source, using an <strong><em>appsettings.json</em></strong> file, exactly like we did for the command line application. Simply copy the file from <strong>Curator.Console</strong> into <strong>Curator.Api</strong>.</p><p>Start the server again using the command <strong><em>dotnet run</em></strong> and in another terminal, use <strong>cURL</strong> to play with the API on the <strong><em>items</em></strong> endpoint:</p><pre>curl -i <a href="http://localhost:5000/items">http://localhost:5000/items</a><br>&gt; HTTP/1.1 200 OK<br>&gt; Date: Sun, 08 Sep 2019 12:09:48 GMT<br>&gt; Content-Type: application/json; charset=utf-8<br>&gt; Server: Kestrel<br>&gt; Transfer-Encoding: chunked</pre><pre>&gt; []</pre><p>If you have not emptied your database used by the console application, you should see items displayed in<a href="https://www.json.org/"> <strong>JSON</strong></a> instead of an empty array in the example. Add a new item to the curation database like this:</p><pre>curl -X POST <a href="http://localhost:5000/items">http://localhost:5000/items</a> \<br>   -H &#39;Content-Type: application/json&#39; \<br>   -d &#39;&quot;medium.com/@jbuisson&quot;&#39;</pre><p>Do not forget to specify the <strong>Content-Type</strong> HTTP header so that ASP.NET Core knows how to parse the body’s data. Now you can fetch the created item. I’m using<a href="https://stedolan.github.io/jq/"> <strong>jq</strong></a> to prettify the <strong>JSON</strong> output, but it’s completely optional:</p><pre>curl <a href="http://localhost:5000/items">http://localhost:5000/items</a> | jq</pre><pre>[<br>  {<br>    &quot;id&quot;: 1,<br>    &quot;name&quot;: &quot;medium.com/@jbuisson&quot;,<br>    &quot;createdAt&quot;: &quot;2019-09-08T12:10:16.0666878&quot;<br>  }<br>]</pre><blockquote>If you do not want to use <strong>cURL,</strong> you can use tools like <a href="https://www.getpostman.com/"><strong>Postman</strong></a> to execute requests on the API.</blockquote><figure><img alt="Using Postman to fecth items." src="https://cdn-images-1.medium.com/max/1024/1*RUub6EsKo0a01bl1uNmFmw.png" /><figcaption>Using Postman to fecth items</figcaption></figure><p><strong>Congratulations</strong>! We have built a working web API with only a few lines of very simple code!</p><p>Here, I have voluntarily written all code by hand, and I did not use any helpers. I wanted to show you, without any magic, how to create a Web API in .NET Core. However, there is a better and easier way to do this.</p><h3>Easy API setup</h3><p>In the same way that we have created our first application, we can use the <strong>dotnet</strong> command line interface (CLI) to scaffold for us a nice Web API. This may break your heart, but delete all the code we just wrote. Delete the <strong>Curator.Api</strong> folder, and replace it with a new api project from scratch.</p><pre>dotnet new webapi -o Curator.Api</pre><p>This new project already has some new files:</p><p><strong>Program.cs</strong> is, as always, our application entry point, and creates a new self-hosting web server, using the <strong><em>Startup</em></strong> class.</p><p><strong>Startup.cs</strong> is a new file. This is where we setup and configure our api within the <strong><em>Startup</em></strong> class. Most of the code is explicit here, and default comments with documentation link should be enough if you have any interrogation.</p><p><strong>appsettings.**.json</strong> are configuration files. You have a default configuration and an override one for the <strong><em>Development</em></strong> runtime’s environment. Environments are declared in the next file.</p><p><strong>Properties/launchSettings.json</strong> contains launch configuration. It is where you find the configured environment and startup properties of the application. Part of the settings is used for Visual Studio (not VS Code) and could be simply ignored (like the ISS Express profile).</p><p><strong>Controllers/ValuesController.cs</strong> is a sample controller with fixed values to show how to create your own. You will spot several differences with the controller we have created:</p><ul><li><strong>[</strong><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.apicontrollerattribute"><strong>ApiController</strong></a><strong>]</strong> attribute enables API-specific behavior, such as routing requirement, automatic HTTP bad request (400) responses, binding, …<a href="https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#apicontroller-attribute"> More here on the documentation</a>.</li><li><a href="https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.mvc.controllerbase"><strong>ControllerBase</strong></a> inheritance for the <strong>ValuesController</strong> to enable helpers methods and properties, such as <strong><em>Request, Response</em></strong>, <strong><em>BadRequest()</em></strong>, <strong><em>CreatedAtAction()</em></strong>, …<a href="https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#controllerbase-class"> More here on the documentation</a>.</li></ul><p>You should now be able to create your own <strong><em>ItemsController</em></strong> based on the one we created previously and the <strong><em>ValuesController</em></strong> the CLI created for us.</p><p>Now we have two applications, the first being our command line application and the second being our newly created Web API. So, the issue that we are facing is that we have duplicated the logic, both in <strong>Curator.Console/Commands</strong> and in <strong>Curator.Api/Controllers</strong>. Code duplication is not only bad (to maintain, debug, etc…), but it can be dangerous for your application. So, you should already have guessed, now is the…</p><h3>Time to refactor</h3><p>What we need to do is to create a single service that will handle the curation logic regarding fetching, adding and removing items. This service should be used by the two applications we have developed. We will also be able to write tests for this service, avoiding to duplicate unit tests as well.</p><p>First, we will create another separated project alongside <strong>Curator.Api</strong> and <strong>Curator.Console</strong>, which will be called <strong>Curator.Core</strong>.</p><pre>dotnet new classlib -o Curator.Core</pre><p>In this project replace the generated class by our own service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e41ded40a33ba17a741e7c5e107cd15d/href">https://medium.com/media/e41ded40a33ba17a741e7c5e107cd15d/href</a></iframe><p>Do not forget to add the references:</p><pre>dotnet add reference ../Curator.Data/Curator.Data.Entities</pre><pre>dotnet add reference ../Curator.Data/Curator.Data.EntityFramework/Curator.Data.EntityFramework.Context</pre><p>I have also created a custom exception to handle not found items by the service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ead6b8c6646b3640a5c0e33b71b1e5f2/href">https://medium.com/media/ead6b8c6646b3640a5c0e33b71b1e5f2/href</a></iframe><p>Let’s take some time to consider all the classes that we have created and their relation to one another.</p><ul><li><strong>CuratorContext</strong> is the <strong>EntityFramework</strong> context, defining our data repository.</li><li><strong>DesignTimeDbContextFactory</strong> are the <strong>CuratorContext</strong> providers, configuring it for both <strong>InMemory</strong> dans <strong>Sqlite</strong> databases.</li><li><strong>ItemsService</strong> is the service using the <strong>CuratorContext</strong> to interact with the database without knowing which kind of provider we are using.</li><li><strong>ItemsController</strong> is the ASP.NET Core controller and should use the <strong>ItemsService</strong> to expose this service’s method on a Web API.</li><li><strong>CuratorCommand</strong> is the command’s definition for our existing console application. It should also use the <strong>ItemsService</strong> like we should for <strong>ItemsController</strong>.</li></ul><p>To properly configure all of this, we will use the<a href="https://docs.microsoft.com/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2"> <strong>Dependency Injection</strong></a> mechanism already in place in .NET Core.</p><p>Let’s update our already existing Console application, using<a href="https://github.com/natemcmaster/CommandLineUtils"> <strong>CommandLineUtils</strong></a> (by<a href="https://medium.com/u/bcd6cc2f75fd"> </a><a href="https://medium.com/u/bcd6cc2f75fd">Nate McMaster</a>) with the <strong>Dependency Injection</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa679094b90b399f5b7fd82f4c5be0c0/href">https://medium.com/media/fa679094b90b399f5b7fd82f4c5be0c0/href</a></iframe><p>In this application, we are using the <strong>ItemsService</strong> with a <strong>Sqlite CuratorContext</strong> implementation. It is now up to you to update the Commands using the <strong>ItemsService</strong> instead of the <strong>CuratorContext</strong>.</p><p>We can now do the same thing in our <strong>Curator.Api</strong> project, using the <strong>Startup</strong> class to configure the application. Simply add the following lines of code in the <strong>ConfigureServices(…)</strong> method:</p><pre>services.AddScoped&lt;CuratorContext&gt;(provider =&gt; new DesignTimeDbContextFactory(m_configuration).CreateDbContext(null));</pre><pre>services.AddTransient&lt;ItemsService&gt;();</pre><p>Our final implementation for the <strong>ItemsController</strong> should now look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2481c64ba17634958278ecd4dacbc755/href">https://medium.com/media/2481c64ba17634958278ecd4dacbc755/href</a></iframe><p>The only thing left to do before considering our application “<strong><em>Production Ready</em></strong>” is to have a proper database that multiple servers hosting our application (for example, behind a load balancer) could access at the same time. Also, <a href="https://www.sqlite.org/whentouse.html"><strong>Sqlite</strong></a> strives to provide local data storage for individual applications or devices and does not compete with client/server databases, so we are going to use<a href="https://www.mysql.com/fr/"> <strong>MySql</strong></a> instead.</p><h3>Adding a new database provider</h3><p>We already have two database providers, one using <strong>Sqlite</strong> and another using <strong>InMemory</strong> for testing purposes. It should then be easy to create a new one, but by using<a href="https://www.nuget.org/packages/MySql.Data.EntityFrameworkCore"> <strong>MySql.Data.EntityFrameworkCore</strong></a> instead of<a href="https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Sqlite/"> <strong>Microsoft.EntityFrameworkCore.Sqlite</strong></a>.</p><p>To do this, start with the same usual first step — create a new project. It is still verbose but you should know by now that I prefer explicit naming conventions:</p><pre>dotnet new classlib -o src/Curator.Data/Curator.Data.EntityFramework/Curator.Data.EntityFramework.Mysql</pre><p>Next, in the newly created project, we need to add references and packages for both <strong>Configurations</strong> and <strong>EntityFramework</strong> to finally create the <strong>DesignTimeDbContextFactory</strong> implementation:</p><pre>cd src/Curator.Data/Curator.Data.EntityFramework/Curator.Data.EntityFramework.Mysql</pre><pre>dotnet add package Microsoft.Extensions.Configuration<br>dotnet add package Microsoft.Extensions.Configuration.Json<br>dotnet add package MySql.Data.EntityFrameworkCore</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c1a2a66e3e8da1cd73d1a300583d82b/href">https://medium.com/media/3c1a2a66e3e8da1cd73d1a300583d82b/href</a></iframe><p>Do not forget to add an <strong>appsettings.json</strong> file in which you should put the <strong>ConnectionString</strong> to the <strong>Mysql</strong> server you’re using. We will need it in both the newly created <strong>Mysql</strong> project and the <strong>Web API</strong> project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84323e23e8291c764a46067b7bf211a8/href">https://medium.com/media/84323e23e8291c764a46067b7bf211a8/href</a></iframe><p>In the previous article from this series, I encouraged you to create clean separation between the <strong>Entities</strong>, the <strong>Context</strong> implementation, and the <strong>Sqlite</strong> provider. You should now understand the benefit of this separation, having a third provider (do not forget the <strong>InMemory</strong> for tests). It is even better now that we have a dedicated and separated service, <strong>ItemsService</strong>, in the <strong>Curator.Core</strong> project. We have configured the <strong>Console</strong> application to use the <strong>Sqlite</strong> provider and the <strong>Web Api</strong> application to use the <strong>Mysql</strong> one, without changing anything with the <strong>ItemsService</strong> logic!</p><blockquote>These are the main true benefits of using <strong>code abstraction</strong> and <strong>code segregation</strong> on our implementations.</blockquote><p>We now need a <strong>Mysql</strong> server for our local environment, and for that we will use<a href="https://www.docker.com/"> <strong>Docker</strong></a>. If you are not familiar with Docker, I really encourage you to read a<a href="https://www.google.com/search?q=docker+tutorial"> tutorial</a> or follow the<a href="https://docs.docker.com/get-started/"> get started</a> information.</p><p>If your prefer, you can of course use a standard <strong>Mysql</strong> installation on your local computer, or even an instance on<a href="https://aws.amazon.com/fr/rds/mysql/"> AWS RDS</a> accessible publicly if you want to.</p><p>If you have not already installed <strong>Docker</strong>, here are the links for the install on <a href="https://docs.docker.com/docker-for-mac/install/">Mac</a>,<a href="https://docs.docker.com/install/linux/docker-ce/ubuntu/"> Ubuntu</a>, and<a href="https://docs.docker.com/docker-for-windows/install/"> Windows</a>. Once done, you should have access to the<a href="https://docs.docker.com/compose/"> docker-compose</a> tool that we will use to create our <strong>Mysql</strong> container.</p><p>Create a file named <strong>docker-compose.yml</strong> at the root of the project, containing the following YML code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/260a912c1759da4ea11284033744ba57/href">https://medium.com/media/260a912c1759da4ea11284033744ba57/href</a></iframe><p>You should be able to start the container as follows:</p><pre>docker-compose up -d</pre><p>If you have followed along, you just need to update the <strong>Web API</strong> application to use <strong>Mysql</strong> instead of <strong>Sqlite</strong>, for that you can simply update the <strong><em>Curator.Api.csproj</em></strong> file and replace <strong>Sqlite</strong> with <strong>Mysql</strong> in the <strong>ProjectReference</strong> block, do not forget the <strong>ConnectionString</strong> in the <strong><em>appsettings.json</em></strong> file.</p><p>And, that’s it for now! We have already achieved a lot here. We have upgraded our console application as a Web API, and we have done some refactoring to clean it up and use the dependency injection mechanism to exploit all the benefits of the architecture that we have implemented. We have even set up a new database with a working local environment using Docker.</p><p><a href="https://gitlab.com/jbuisson/curator/tree/webapi">Files · webapi · Jérémy Buisson / curator</a></p><p>In the next article I will show you how you can package and deploy the API, with some <strong>Continuous Integration </strong>and <strong>Continuous Delivery</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cdb470cc8222" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/net-core-is-sexy-building-a-web-api-cdb470cc8222">.NET Core is Sexy — Building a Web API</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[.NET Core is sexy — Real-world Part 1: Command Line Application]]></title>
            <link>https://itnext.io/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/953f48394c83</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[realworldprojects]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Sun, 11 Aug 2019 09:50:57 GMT</pubDate>
            <atom:updated>2019-09-23T07:21:48.067Z</atom:updated>
            <content:encoded><![CDATA[<h3>.NET Core is sexy— Command Line Application</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ZXskQGMKQ2IuPDLPLWmEMw.png" /></figure><blockquote>This article is part of a series about .NET Core and how it is now as easy and efficient as any other stack like Node or Rails to create modern applications.</blockquote><blockquote><a href="https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4">.NET Core is sexy and you should know it</a></blockquote><blockquote><strong><em>.NET Core is sexy — Command Line Application</em></strong></blockquote><blockquote><a href="https://itnext.io/net-core-is-sexy-building-a-web-api-cdb470cc8222">.NET Core is sexy — Building a Web API</a></blockquote><p>My intention is to create a curation application that we will improve step by step, ending with a full production-ready platform. We will start creating a simple command line application to begin, with an Sqlite database.</p><h3>Our target application</h3><p>At this step, we are building a command line application that we will use to store items defined by a unique identifier, a name and a created date. We will then be able to display our current curation list and remove an item from it.</p><pre>curator add medium.com/@jbuisson<br>curator add blog.cleancoder.com<br>curator list<br>&gt; 1: medium.com/@jbuisson, 8/3/19 9:58:56 AM.<br>&gt; 2: blog.cleancoder.com, 8/3/19 9:59:17 AM.<br>curator remove 1<br>curator list<br>&gt; 2: blog.cleancoder.com, 8/3/19 9:59:17 AM.</pre><h3>Set up the application and packages</h3><p>Create a new console application:</p><pre>dotnet new console -o Curator</pre><p>We will use <a href="https://docs.microsoft.com/fr-fr/ef/core/">Microsoft.EntityFramework</a> as our database object-relational mapping (ORM), configured for Sqlite. In addition, we will use <a href="https://github.com/natemcmaster/CommandLineUtils">CommandLineUtils</a> — from <a href="https://medium.com/u/bcd6cc2f75fd">Nate McMaster</a> — an awesome package for easily writing command line applications.</p><p>From within our newly created project, run the following commands to add the packages:</p><pre>dotnet add package McMaster.Extensions.CommandLineUtils <br>dotnet add package Microsoft.EntityFrameworkCore.Sqlite<br>dotnet add package Microsoft.EntityFrameworkCore.Design</pre><h3>Time to code</h3><p>We now must create our <strong>Item</strong> model to enable saving it in an Sqlite database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aafb9704d4b43314de21f2326cce16d8/href">https://medium.com/media/aafb9704d4b43314de21f2326cce16d8/href</a></iframe><p>We have a C# class with public properties to get/set the <strong><em>Id</em></strong>, <strong><em>Name</em></strong> and <strong><em>CreatedAt</em></strong>. We also override the <strong><em>ToString</em></strong>() method to display the Item information.</p><p>Next, we create the EntityFramework <a href="https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0"><strong><em>DbContext</em></strong></a> and configure it to use Sqlite.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11ebccbfc57e56220bf2a187b8c905e1/href">https://medium.com/media/11ebccbfc57e56220bf2a187b8c905e1/href</a></iframe><p>Again, nothing is tricky here; the <strong><em>DbContext</em></strong> serves as the bridge for accessing data from the database. We declare a <strong><em>DbSet</em></strong> for the <strong><em>Item</em></strong> model, which indicates that we have an <strong><em>Items</em></strong> table in the database.</p><p>Additionally, we tell EntityFramework to <strong><em>UseSqlite</em></strong>, with the path to the Sqlite database file. This path is relative to the binary execution directory. Feel free to use any other path you see fit, but ensure the target directory exists; otherwise, you might encounter the following application error:</p><pre>SQLite Error 14: ‘unable to open database file’.</pre><p>Prior to setting up the application itself, we create a new EntityFramework migration and run it to set up the database.</p><pre>dotnet ef migrations add InitialCreate<br>dotnet ef database update</pre><p>You should see a <strong><em>curator.db</em></strong> file appears in the directory that you have previously set up in the <strong>DbContext</strong>. If you did not change the <strong>DataSource</strong> path, you should be able to find it at the root of the workspace.</p><p>Finally, we create our commands.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c73d53b803be1c828d6e924615635d0b/href">https://medium.com/media/c73d53b803be1c828d6e924615635d0b/href</a></iframe><p>The code is explicit. We have a base <strong><em>CuratorCommand</em></strong> class, which works as a parent class for all the other commands and is responsible for the <strong><em>CuratorContext</em></strong>. In addition, we have three other classes for the following three commands: <strong>add</strong>, <strong>list</strong>, <strong>remove</strong>.</p><p>I encourage you to explore the commands. Consider even creating your own to familiarize yourself with the application and how it works.</p><p>The final step is to update our application’s main entry point. Doing so should result in something similar to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12326427bbabb8d3ef30356711a08939/href">https://medium.com/media/12326427bbabb8d3ef30356711a08939/href</a></iframe><p>Now, it is time to test it.</p><pre>dotnet run add medium.com/@jbuisson<br>dotnet run add blog.cleancoder.com<br>dotnet run list<br>&gt; 1: medium.com/@jbuisson, 8/3/19 11:13:37 AM.<br>&gt; 2: blog.cleancoder.com, 8/3/19 11:13:42 AM.<br>dotnet run remove 1<br>dotnet run list<br>&gt; 2: blog.cleancoder.com, 8/3/19 11:13:42 AM.</pre><h3>Time to refactor</h3><p>We have a working command line application, but it is poorly designed and does not respect any <a href="https://clean-code-developer.com/weitere-infos/solid/">clean code principle</a>.</p><p>I am aiming for this project architecture, splitting the console application into multiple class libraries:</p><pre>├── data<br>├── src<br>    ├── Curator.Console<br>    ├── Curator.Data<br>        ├── Curator.Data.Entities<br>        ├── Curator.Data.EntityFramework<br>            ├── Curator.Data.EntityFramework.Context<br>            ├── Curator.Data.EntityFramework.Sqlite<br>├── tests</pre><p>This might look somewhat complicated — that is, having such depth and splitting Entities, Context and Sqlite into three different libraries. However, multiple reasons support using this method:</p><ul><li>I should be able to use Entities without EntityFramework; they are two concerns with their own responsibilities and so forth should be separated. This is not a dogmatic vision but a pragmatic one. We could remove EntityFramework at some point to use another ORM — or none — and doing so would not be too costly thanks to this approach.</li><li>EntityFramework allows us to use a <a href="https://entityframework.net/supported-database-providers">wide range of database</a>s. This means that while using the same Context, we could use different databases. Spoiler: we will do this both when testing and porting the application to the web stack.</li></ul><p>The data folder simply serves as our Sqlite repository. The tests folder is self-explanatory and will be used once the refactoring has been completed.</p><p>I will also include some runtime checks for the sake of <em>null</em> or <em>invalid</em> arguments.</p><h3>It is never too late to write tests</h3><p>I cannot consciously consider this step without adding some tests. Once more, you will see that it is simple to set up tests based on your code and then run them.</p><p>To test our command, we do not want to use a real Sqlite database. EntityFramework has an InMemory provider to help us do so.</p><pre>cd src/Curator.Data/Curator.Data.EntityFramework<br>dotnet new classlib -o Curator.Data.EntityFramework.Memory<br>cd Curator.Data.EntityFramework.Memory<br>dotnet add package Microsoft.EntityFrameworkCore.InMemory<br>dotnet add reference ../Curator.Data.EntityFramework/Curator.Data.EntityFramework.Context/</pre><p>We now only have to create a new <strong><em>DesignTimeDbContextFactory</em></strong> and set it up with <strong><em>UseInMemoryDatabase</em></strong>. A trick here is to add a unique database name on each new DbContext to isolate each test. For that I use <strong><em>DateTime.Now.Ticks</em></strong>, which is sufficiently safe, but you could instead use <strong><em>Guid.NewGuid()</em></strong> or any unique random generator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/78049229ff7602b9ef7d7b5e143308c8/href">https://medium.com/media/78049229ff7602b9ef7d7b5e143308c8/href</a></iframe><p>Once this has been completed, we should go to the tests folder and create a new test project.</p><pre>cd tests<br>dotnet new xunit -o Curator.Console.Tests</pre><p>We must add references to the project that we wish to test in addition to the new database provider that we have just created:</p><pre>dotnet add reference ../../src/Curator.Console<br>dotnet add reference ..\..\src\Curator.Data\Curator.Data.EntityFramework\Curator.Data.EntityFramework.Memory\Curator.Data.EntityFramework.Memory.csproj</pre><p>You can now write a simple test for the AddCommand:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac21d2a878e1c71a9bbfe61da237a41d/href">https://medium.com/media/ac21d2a878e1c71a9bbfe61da237a41d/href</a></iframe><p>Finally, once all tests have been written, we have a clean command line application using .NET Core, compatible with Linux, Windows and Mac! From my previous story you should now be able to release this application easily:</p><pre>dotnet publish src/Curator.Console -c Release — self-contained true -r [<a href="https://docs.microsoft.com/fr-fr/dotnet/core/rid-catalog">YOUR_RUNTIME_IDENTIFIER</a>] -o [INSTALLATION_PATH]</pre><p><a href="https://gitlab.com/jbuisson/curator/tree/console">Sign in</a></p><p>This is it for our first step. Next one will be the web API deployed on a server or using Docker.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=953f48394c83" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83">.NET Core is sexy — Real-world Part 1: Command Line Application</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[.NET Core is Sexy, and you should know it]]></title>
            <link>https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4?source=rss-c95fa9309609------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3d0c2eeedc4</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Jeremy Buisson]]></dc:creator>
            <pubDate>Fri, 02 Aug 2019 17:27:24 GMT</pubDate>
            <atom:updated>2019-09-21T15:15:04.383Z</atom:updated>
            <content:encoded><![CDATA[<h3>.NET Core is sexy, and you should know it</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*zc1BKfAHkpvrZlHPbUvuYA.png" /></figure><blockquote>This article is part of a series about .NET Core and how it is now as easy and efficient as any other stack like Node or Rails to create modern applications.</blockquote><blockquote><strong><em>.NET Core is sexy and you should know it</em></strong></blockquote><blockquote><a href="https://medium.com/@jbuisson/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83">.NET Core is sexy — Command Line Application</a></blockquote><blockquote><a href="https://itnext.io/net-core-is-sexy-building-a-web-api-cdb470cc8222">.NET Core is sexy — Building a Web API</a></blockquote><p>I always thought my first blog post would be a detailed tutorial about a serverless platform with multiple services, deployed using a single command line on AWS using GitLab CI. Or even a post praising the benefits of a Dungeons and Dragons (D&amp;D) game between friends or workmates.<br>I was so innocent…</p><p>A couple of days ago I heard by chance that some recruitment agencies and firms specializing in developers refuse, <strong>on principle</strong>, any candidates or companies wishing to work with .NET, for the following reasons:</p><blockquote>It is an old closed and proprietary ecosystem, completely opposed to other, sexier, development platforms which are much more agile and open, like NodeJS or RubyRails. A little like Java actually. But Java, not being supported by the evil Microsoft, is still tolerated in these same agencies and firms.</blockquote><p>Meanwhile I’m developing web apps using <strong>C#</strong> and<strong> .NET Core </strong>on my personal <strong>MacBook</strong>, which I deploy using <strong>GitLab</strong> on <strong>AWS</strong> using <strong>Lambdas </strong>functions<strong>,</strong> <strong>Linux</strong> EC2 and <strong>Docker</strong> containers. I’ve even pushed some code and tests on the <strong>official open source Github </strong>repository, <a href="https://github.com/dotnet/corefx/pull/34">5 years ago</a>, which are still on it.</p><p>Needless to say I started to feel uncomfortable hearing this. Then I had a feeling of déjà-vu from discussing another of my passions: D&amp;D, and hearing Role Playing Games are elitist with a bad reputation and which are very inaccessible. But I’ll talk about D&amp;D another time.</p><p>So here I am, climbing onto my most noble warhorse, putting on my shiniest armor and setting off on a quest, not to the defense of the widow and the orphan, but to the support of the .NET Core platform’s reputation, with the goal of convincing you that we can <strong>easily </strong>and <strong>quickly </strong>develop in<strong> C# </strong>and <strong>.NET Core</strong>, as we can do in JavaScript and NodeJS.</p><h3>What is the .NET Core platform?</h3><p>Things can quickly become rather obscure when we talk about the .NET Core platform, so I’ll just quote the <a href="https://docs.microsoft.com/fr-fr/dotnet/core/about#composition">official documentation</a>:</p><ul><li>The <a href="https://github.com/dotnet/coreclr">.NET Core runtime</a> provides a type system, assembly loading, a garbage collector, native interop and other basic services. The <a href="https://github.com/dotnet/corefx">.NET Core framework libraries</a> provide primitive data types, app composition types and fundamental utilities.</li><li>The<a href="https://github.com/aspnet/home"> ASP.NET runtime</a> offers a framework for building modern cloud-based internet-connected applications, such as web apps, IoT apps and mobile backends.</li><li>The <a href="https://github.com/dotnet/cli">.NET Core CLI tools</a> and language compilers (<a href="https://github.com/dotnet/roslyn">Roslyn</a> and<a href="https://github.com/microsoft/visualfsharp"> F#</a>) enable the .NET Core developer experience.</li><li>The<a href="https://github.com/dotnet/core-setup"> dotnet tool</a> is used to launch .NET Core apps and CLI tools. It selects and hosts the runtime, provides an assembly loading policy and launches apps and tools.</li></ul><p>In summary of the above:</p><ul><li>The runtime enables execution of .NET Core binaries.</li><li>ASP.NET Core is a framework and a set of libraries which are ready to use to build web apps and APIs.</li><li>.NET Core CLI is like other platform CLIs, as it allows you to create, build, publish, setup and scaffold your projects, among other actions.</li></ul><h3>Let’s code!</h3><p>Enough chit-chat. My objectives here are to show you how quick and easy it is to start working with .NET Core to create applications and websites, just as it would be using NodeJS or RubyRails −so let’s go.</p><p>Linux installation is quite simple; just <a href="https://dotnet.microsoft.com/download/linux-package-manager/rhel/sdk-current">follow this link</a> and choose your distribution. About three or four commands are needed to register the Microsoft key and feed to install the necessary packages.</p><p>On Ubuntu, here is how it looks from the terminal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0c7dfe1287ffb4c124b6b0d527e8a59/href">https://medium.com/media/c0c7dfe1287ffb4c124b6b0d527e8a59/href</a></iframe><p>Of course, you only need to do this once, and from thereon a simple command will confirm that all is working well:</p><pre>dotnet --version<br>&gt; 2.2.300</pre><p>Installation on MacOs or Windows is even simpler: just download it from the <a href="https://dotnet.microsoft.com/download/dotnet-core/2.2">official Microsoft portal</a> and let the installer do the job for you. It should only take a couple of minutes.</p><h3>Hello World!</h3><p>In following a well-known ritual, let’s create a console application with the sole purpose to display <strong><em>Hello World!</em></strong>.<br>For impatient readers, here is the Git repository:</p><p><a href="https://gitlab.com/jbuisson/hello-world">Jérémy Buisson / hello-world</a></p><p>First create a new folder, then initialize your project from it:</p><pre>mkdir hello-world<br>cd hello-world<br>dotnet new console</pre><p>You will have the following files and folders:</p><pre>hello-world<br>├── bin<br>├── obj<br>├── hello-world.csproj<br>├── Program.cs</pre><p>You can already ignore the <strong><em>bin</em></strong> and <strong><em>obj</em></strong> folders, which are only there for build and debug steps, so you’ll never have to look into them. In fact, I ignore them both on VSCode and Git.</p><p>The .csproj file contains information about the runtime, packages, versions and other project configuration properties. It’s really small by default.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4996baf47d835e4eb1ceacaa4e01347/href">https://medium.com/media/f4996baf47d835e4eb1ceacaa4e01347/href</a></iframe><p>And finally, our most important file: Program.cs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9757effd756711b0163a5cef97404808/href">https://medium.com/media/9757effd756711b0163a5cef97404808/href</a></iframe><p>Here again, nothing is tricky − you have your default namespace. A class in which you declare your main entry point via the <strong>static method Main()</strong>. The class or namespace can be changed to whatever you want and does not need to be public, nor does the <strong>Main</strong> method. Here the class is<a href="https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/internal"> internal</a>, and the method is<a href="https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/private"> private</a>.</p><p>The only constraint is to have at least this static Main method. Even the parameters are optional, but they exist to provide access to arguments through the command line execution.</p><p>Now, let’s run the application:</p><pre>dotnet run<br>&gt; Hello World!</pre><p>It’s as easy as that. To get a release version of the application which is deployable on any environment having the .NET Core runtime (version 2.2 here), simply publish the application as follows:</p><pre>dotnet publish -c Release -o dist</pre><p>The resulting <strong><em>dist</em></strong> folder should appear like this:</p><pre>dist<br>├── hello-world.deps.json<br>├── hello-world.dll<br>├── hello-world.pdb<br>├── hello-world.runtimeconfig.json</pre><p>The <strong><em>hello-world.pdb</em></strong> can be deleted without issue as it’s only useful for debugging purposes but is generated by default, even in release mode. You can disable this behavior by adding this snippet to your <strong><em>hello-world.csproj</em></strong> (under the &lt;Project /&gt; tag).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba6758a53a2a4e4b2093443ad536536f/href">https://medium.com/media/ba6758a53a2a4e4b2093443ad536536f/href</a></iframe><p>Then <strong><em>hello-world.dll</em></strong> is your compiled code, executable using this runtime command:</p><pre>dotnet hello-world.dll</pre><p>The <strong><em>deps.json</em></strong> and <strong><em>runtimeconfig.json</em></strong> files are in place to handle dependencies on other packages and to configure the runtime, respectively.</p><p>Finally, you could just as easily target the build for a specific platform when publishing the application, using the<a href="https://docs.microsoft.com/fr-fr/dotnet/core/rid-catalog"> Runtime Identifier Catalog</a>:</p><pre>dotnet publish -c Release -r win-x64 -o dist/win-x64<br>dotnet publish -c Release -r osx-x64 -o dist/osx-x64<br>dotnet publish -c Release -r linux-x64 -o dist/linux-x64</pre><h3>Hello Web !</h3><p>Ok that’s cool, we wrote a line on the console pretty easily, but what about some web stuff ? Fine, that’s also very easy to do !</p><p>First, let’s add the package for ASP.NET Core:</p><pre>dotnet add package Microsoft.AspNetCore</pre><p>This package will allow us to <strong>Configure</strong>, <strong>Build</strong> and <strong>Run</strong> a simple WebHost. This can be done in a single line of code, in the <strong><em>Main()</em></strong> method. You’ll also need to reference the new package, just like you would import npm packages for NodeJS. The resulting <strong><em>Program.cs</em></strong> should look as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbb38be0f0ce01980a9b0596e24cb3fb/href">https://medium.com/media/cbb38be0f0ce01980a9b0596e24cb3fb/href</a></iframe><p>You can use the same commands as above to run and build the app:</p><pre>dotnet run</pre><p>Now open your favorite browser, go to <a href="http://localhost:5000,">http://localhost:5000,</a> and enjoy this wonderful <strong><em>Hello World!</em></strong> web page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*dQJcBJADzxiCcukCpl-7Qg.png" /><figcaption>It’s alive!</figcaption></figure><p>That’s all for now. I’ll return with some more real-world examples, but after reading this I really hope your opinion about .NET Core has evolved.</p><p>If you have any questions, I’ll do my best to be as responsive as possible in the comments!</p><p><strong>UPDATE</strong>: You can now continue with a real world example here thanks to my next story <a href="https://medium.com/@jbuisson/net-core-is-sexy-real-world-part-1-command-line-application-953f48394c83">.NET Core is sexy — Real-world Part 1: Command Line Application</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3d0c2eeedc4" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/https-medium-com-buisson-jeremy-net-core-is-sexy-and-you-should-know-it-a3d0c2eeedc4">.NET Core is Sexy, and you should know it</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>