<?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 Alessandro Chitolina on Medium]]></title>
        <description><![CDATA[Stories by Alessandro Chitolina on Medium]]></description>
        <link>https://medium.com/@alekitto?source=rss-441254540973------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RsEQn5-C-3NNvC1uk3_CLQ.jpeg</url>
            <title>Stories by Alessandro Chitolina on Medium</title>
            <link>https://medium.com/@alekitto?source=rss-441254540973------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 01:42:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alekitto/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[Git: what is it anyway? Part 2]]></title>
            <link>https://medium.com/codex/git-what-is-it-anyway-part-2-95253291a4cf?source=rss-441254540973------2</link>
            <guid isPermaLink="false">https://medium.com/p/95253291a4cf</guid>
            <category><![CDATA[version-control-system]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Alessandro Chitolina]]></dc:creator>
            <pubDate>Mon, 15 Mar 2021 07:03:16 GMT</pubDate>
            <atom:updated>2021-03-15T07:45:55.207Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="http://medium.com/codex">CODEX</a></h4><h4>How git works internally and why is this awesome?</h4><p><a href="https://medium.com/p/git-what-is-it-anyway-7fe34d40ae0e">In the first part</a>, we talked about GIT core: hashes, objects, trees, blobs and other low-level fundamentals.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B4-G90xa3COVaiuM" /><figcaption>Photo by <a href="https://unsplash.com/@yancymin?utm_source=medium&amp;utm_medium=referral">Yancy Min</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Now we’re going to see how these objects are used in higher-level commands.</p><h4>Git: blockchain before it was mainstream</h4><p>In the first part, we created a commit to take an immutable snapshot of our code, but the fun part is that the commits are linked to one another like in a chain: every commit contains a reference to its parent (or parents) and that is the magic trick which guarantees their immutability.</p><p>An example is worth a thousand words, so let’s create a new file, add it to the staging area and create a commit object like in part 1:</p><pre>$ echo &quot;another file&quot; &gt; NEWFILE.txt<br>$ git add NEWFILE.txt<br>$ git commit -m &quot;added new file&quot;<br>[master 031b65d] added new file<br> 1 file changed, 1 insertion(+)<br> create mode 100644 NEWFILE.txt</pre><p>Simple huh? Now let’s try to inspect this new commit object (031b65d )</p><pre>$ git cat-file -p 031b65d<br>tree 2eb34818fda8b4fbd27371f07988b5944d11b047<br>parent aeb27a2953068137e0d205b0ed21834607ccd56d<br>author Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt; 1614982391 +0100<br>committer Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt; 1614982391 +0100</pre><pre>added new file</pre><p>A new parent property is present on this commit object. aeb27a2 do you remember it? Sure! It was the first commit we’ve created in part 1!</p><p>If something would change in the parent its hash will be different, so the hashes of all its children. This simple thing guarantees the complete immutability of commits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4UFziObEECdkoFgZ" /><figcaption>Photo by <a href="https://unsplash.com/@matthewelancaster?utm_source=medium&amp;utm_medium=referral">Matthew Lancaster</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>Branch: a short reference to a commit chain</h4><p>Ok ok, but it’s not so convenient to always use these long hashes in everyday work, we need shorter and meaningful references to commits…</p><p>Ladies and gentlemen, it’s a pleasure to introduce you <em>branch. </em>We already encountered one while we created commits, its name is master , which is the default branch in git.</p><p>What we call “branch” is only a reference to a commit, the latest commit in the branch. We can see it reading a file in the .git directory:</p><pre>$ cat .git/refs/heads/master<br>031b65d4cca27bcb2063214a440d12141220cfa1</pre><p>Can you see it? Exactly the same hash of the last commit we’ve just created!</p><p>When we call git commit we are not only creating a commit object with all its related tree and blob objects, but we are also updating the branch head reference.</p><p>This system of references and hashes is the base of the git killer feature, the one which makes the real difference upon other version control systems: it can create branches in a blink of an eye.</p><pre>$ git branch test-branch</pre><p>A new reference is instantly created for the test-branch branch. No need to copy files and folders, fix links, permissions and other I/O heavy operations, just create a reference to the HEAD commit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZVQpoaI3dpnr7ZgiRME9Zw.jpeg" /><figcaption>Hercules and Hydra</figcaption></figure><h4>HEADful references</h4><p>A special reference called HEAD is always present in a git repository and it points to the latest commit, the one we are working on and the one which is referenced as parent in the newly created commits.</p><p>But like the Hydra, a git repository has many heads, one per local branch, plus one per remote branch (we’ll talk about remotes in part 3), plus the special HEAD reference.</p><p>We can change the HEAD every time we want using the checkout command, effectively switching between branches, tags, commits and other references.</p><p>We’ve previously created the test-branch branch, but the simple creation of a branch does not mutate the HEAD . To switch to that branch we need to execute:</p><pre>$ git checkout test-branch<br>Switched to branch &#39;test-branch&#39;</pre><p>Pretty easy! From now on, every commit we create, will update the reference of the test-branch head.</p><pre>$ echo &quot;branched&quot; &gt; BRANCHED.txt<br>$ git add BRANCHED.txt<br>$ git commit -m &quot;commit on a branch&quot;<br>[test-branch 83b1774] commit on a branch<br> 1 file changed, 1 insertion(+)<br> create mode 100644 BRANCHED.txt</pre><p>Now the modifications we’ve done are present only in a commit referenced by test-branch . We can switch back to the master branch to check it.</p><pre>$ git checkout master<br>Switched to branch &#39;master&#39;<br>$ ls<br>NEWFILE.txt README</pre><pre>$ git log<br>commit 031b65d4cca27bcb2063214a440d12141220cfa1<br>Author: Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt;<br>Date:   Fri Mar 5 23:13:11 2021 +0100</pre><pre>added new file</pre><pre>commit aeb27a2953068137e0d205b0ed21834607ccd56d<br>Author: Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt;<br>Date:   Sun Feb 28 10:59:41 2021 +0100</pre><pre>Added README</pre><p>As you can see, the 83b1774 commit is not present, such as the BRANCHED.txt file. We can now create a new commit with another file.</p><pre>$ echo &quot;this is only on master&quot; &gt; MASTER.txt<br>$ git add MASTER.txt<br>$ git commit -m &quot;commit only on master&quot;<br>[master edde583] commit only on master<br> 1 file changed, 1 insertion(+)<br> create mode 100644 MASTER.txt</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hbpf_FRBK1h1j2MVs-cOAQ.png" /><figcaption>Merging roads</figcaption></figure><h4>git merge: magically combine branches</h4><p>Now the two branches are diverging. They share the same commits until 031b65d , but then two different commits are created from that.</p><pre>  * commit on a branch (83b1774)<br>  |<br>* commit only on master (edde583)<br>|/<br>* added new file (031b65d)<br>|<br>* Added README (aeb27a2)</pre><p>This is a common situation when working in a team, because every team member works on one feature, creating and updating its own branch until the work is finished.<br>At the end of the work, the feature branch must be combined with the main development branch (master ).</p><p>To do that, we must use the git merge command. This command not only combines the current head with another one, but also creates a new commit object with <strong>two</strong> parents, one per merged head.</p><pre>$ git merge test-branch --no-edit<br>Merge made by the &#39;recursive&#39; strategy.<br> BRANCHED.txt | 1 +<br> 1 file changed, 1 insertion(+)<br> create mode 100644 BRANCHED.txt</pre><p>Hooray! We successfully merged our two branches! We can also inspect the merge commit with the old good cat-file command</p><pre>$ git cat-file -p HEAD<br>tree 9f7377525008d0846feb8a2bb3aa0278b81f5927<br>parent edde583143be80ccb88b5495436ceb1998d395e0<br>parent 83b17747d65b40c50fa07e2e28dd04bf7cd4e8c0<br>author Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt; 1614991406 +0100<br>committer Alessandro Chitolina &lt;<a href="/service/mailto:alekitto@gmail.com">alekitto@gmail.com</a>&gt; 1614991406 +0100</pre><pre>Merge branch &#39;test-branch&#39;</pre><p>We have two parents (only merge commits have multiple parents), but all was pretty much the same as standard commit, right?</p><p>The graph log shows us the situation more clearly</p><pre>* Merge branch &#39;test-branch&#39; (d963718)<br>|\<br>| * commit on a branch (83b1774)<br>| |<br>* commit only on master (edde583)<br>|/<br>* added new file (031b65d)<br>|<br>* Added README (aeb27a2)</pre><p>End of part 2. In part 3 we’ll talk about rebase, remotes and some useful git commands.</p><p>Thanks for reading,<br>A.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95253291a4cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/git-what-is-it-anyway-part-2-95253291a4cf">Git: what is it anyway? Part 2</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[Rich domain models for the win!]]></title>
            <link>https://medium.com/@alekitto/rich-domain-models-for-the-win-696ddade1319?source=rss-441254540973------2</link>
            <guid isPermaLink="false">https://medium.com/p/696ddade1319</guid>
            <category><![CDATA[doctrine]]></category>
            <category><![CDATA[domain-modeling]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Alessandro Chitolina]]></dc:creator>
            <pubDate>Wed, 10 Mar 2021 07:01:11 GMT</pubDate>
            <atom:updated>2021-03-19T12:08:24.198Z</atom:updated>
            <content:encoded><![CDATA[<h4>Do yourself a favor: stop writing anemic domain models now</h4><p><strong>Disclaimer</strong>: Please do not change your entire (and working) application because a random guy on the internet told you so, but you can experiment a new way to do things and become familiar with it.</p><p><strong>Disclaimer 2</strong>: I’m using PHP in this post, but the concepts exposed here can be applied on other languages too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0tuQXC-f9X0ktJZF" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So you are building a shiny new project, you wrote your <em>anemic</em> entities with some typed properties, all setters and getters, the services which manipulates these entities and all is working correctly. Great!</p><p>Data and behaviours are completely separated, validation and manipulation happen only in <strong>services</strong> and everything is super clean. But…</p><p>You have to remember what the services do because no logic is present in the entities which are now more similar to DTOs. And that’s the root cause of a myriad of bugs out there.</p><p>Let’s pretend we have to create a rent system for a library. With anemic model we write something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98981704fe1adaf8380631d6c4a2ee4d/href">https://medium.com/media/98981704fe1adaf8380631d6c4a2ee4d/href</a></iframe><p>What’s wrong with this? Almost nothing apart from two little things:</p><ol><li>In createRental we just forget to decrease the stock of the books we rented.</li><li>We have to manually validate the order.</li></ol><p>Let’s proceed in order</p><h4>Rich model to avoid confusion</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U4wdcv380Ku65ynA" /><figcaption>Photo by <a href="https://unsplash.com/@yogidan2012?utm_source=medium&amp;utm_medium=referral">Daniele Levis Pelusi</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Yes, we introduced a bug: we forget to check the stock of books and decrease its number when we are creating a rent. We could simply add the StockService to the dependencies of the RentalService and fix the bug with two lines of code, BUT…</p><p>We still have the chance to create a Rent entity without checking the stock, which is a severe error; nothing prevents us to do a new Rent() and go on. Imagine that a new developer begins to work on the project: he probably knows nothing about the stock existence.</p><p>Let’s try to modify our Book entity to overcome this problem:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a2c7bfba785ad0ca7694f39271d170b/href">https://medium.com/media/0a2c7bfba785ad0ca7694f39271d170b/href</a></iframe><p>Just a little better: first of all, we removed the need of a RentalService. And then, everything we need is in the constructor and you simply cannot create a rent without touching the stock. Also you <strong>DON’T HAVE TO REMEMBER</strong> what service to call, how and what effects it produces, you are sure that everything has been handled just calling the Book constructor.</p><p>But we can do better</p><h4>Self-validating rich model entities</h4><p>Having smart entities means we can write entities that CANNOT BE INVALID.</p><p>Remember when we called orderService-&gt;validate($order) in the anemic model? This is one thing we can forget one day or another and will spread bugs in our code and database for sure. Not only this: if you are validating an entity <em>after </em>you set invalid data on it, nothing prevents you to persist that invalid data <em>which effectively have been set into the entity</em>.</p><p>In rich model we can write our order entity to be self-validating, requiring mandatory properties in the constructor, validating values on setters and adders and throwing exceptions if something is wrong.</p><p>Let’s try with our book entity:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c926211a674496f25ca024ac16343cdd/href">https://medium.com/media/c926211a674496f25ca024ac16343cdd/href</a></iframe><p>Now you’re safe and sure that no rental is created with invalid data or without a mandatory attribute.</p><h4>Ok, but why we have to do this?</h4><p>“We are good developers, we cannot make this kind of mistakes”. How many times I heard these words? Millions of times. And obviously something went wrong.</p><p>Not because they were bad developers (ok, in some cases they were actually..), but because they simply had to remember too many things.<br>Rich model avoids you to remember those two or three things (per entity) and rely on the IDE tooltips to switch on a lamp in our brains when writing a constructor call.</p><blockquote>The less we have to remember, the better it is</blockquote><p>So, long live rich model entities!</p><p>Thank you for reading<br>A.</p><p>PS: if you want to read more on this argument, you have to check <a href="https://web.archive.org/web/20201126065134/https://www.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html">this illuminant page on Doctrine documentation</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=696ddade1319" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git: what is it anyway?]]></title>
            <link>https://medium.com/codex/git-what-is-it-anyway-7fe34d40ae0e?source=rss-441254540973------2</link>
            <guid isPermaLink="false">https://medium.com/p/7fe34d40ae0e</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[version-control-system]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Alessandro Chitolina]]></dc:creator>
            <pubDate>Fri, 05 Mar 2021 07:03:08 GMT</pubDate>
            <atom:updated>2021-03-05T22:18:55.977Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="http://medium.com/codex">CODEX</a></h4><h4>How git works internally and why is this awesome?</h4><p>We like GIT. We love GIT. ’Cause it saves our lives and our time dozens of times each day and it makes us happy :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YgKRI2EIDeZIlqdD" /><figcaption>Photo by <a href="https://unsplash.com/@yancymin?utm_source=medium&amp;utm_medium=referral">Yancy Min</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>And we’re <strong>git pull</strong>ing and <strong>git push</strong>ing all the time, and <strong>merg</strong>ing, and <strong>commit</strong>ting, and so on… But, wait! How does it really work?</p><h3>What is git in its core?</h3><p>Well, GIT is just a persistent hashmap: you give him an object and he gives you back an hash corresponding to that object. That’s it. Pretty simple, right?</p><pre>$ echo &quot;something&quot; | git hash-object --stdin<br>deba01fc8d98200761c46eb139f11ac244cf6eb5</pre><p>GIT is doing this all the time: it reads the content, stores it and generates hashes <strong>based on it</strong>.</p><p>And that means <strong>same generated hash == same content</strong>. And this is <em>VERY VERY IMPORTANT</em>, ’cause git doesn’t like duplicates and <em>NEVER</em> stores objects with the same content (and with the same hash) more than once.</p><p>Ok, but what are these <em>objects </em>anyway?</p><p>An object is a piece of content: it could be the content of a file, a directory structure. An object is anything that git can store.</p><p>Let’s try to store the string “something” into GIT.</p><pre>$ echo &quot;something&quot; | git hash-object --stdin -w<br>fatal: Not a git repository (or any of the parent directories): .git</pre><p>An error… Great!</p><p>GIT complains that this folder is “Not a git repository”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ed8qASJnG_qkKYTIINYVAg.jpeg" /></figure><h3>What is a git repository?</h3><p>Well, GIT stores content, so, from a certain point of view, GIT is a filesystem. And, as well as other filesystems, it needs to be initialized.</p><pre>$ git init<br>Initialized empty Git repository</pre><p>Differently from the other filesystems, GIT uses files to store the objects, not a low-level structure on the disk, but its pretty similar.</p><p>Now that we have initialized the GIT repository, we’re ready to store objects:</p><pre>$ echo &quot;something&quot; | git hash-object --stdin -w<br>deba01fc8d98200761c46eb139f11ac244cf6eb5</pre><p>Stored. Hooray!</p><p>Now we need a way to retrieve that object. That’s easy:</p><pre>$ git cat-file -p deba01f<br>something</pre><p><strong>git cat-file</strong> is a powerful one. Used with the -p flags it prints the object and exits, but we can ask it for the type of an object using the flag -t. In this case it will answer stating that the object is of type blob, a binary large object, a user content.</p><p>Yeah yeah, you’re right: storing and retrieving object in this way is pretty uncomfortable and we are not versioning anything. We need something much easier…</p><p>Oh, i have an idea! Let’s transform files into objects!</p><p>Great! And we can store folders and all the file tree too! Let’s do it.</p><pre>$ echo &quot;something&quot; &gt; README</pre><p>Ok, I’ve created a file, and now? Let’s ask GIT (using <strong>git status</strong>)</p><pre>$ git status<br>On branch master</pre><pre>Initial commit</pre><pre>Untracked files:<br>  (use &quot;git add &lt;file&gt;...&quot; to include in what will be committed)</pre><pre>    README</pre><pre>nothing added to commit but untracked files present (use &quot;git add&quot; to track)</pre><p>Whoa! Hey, wait. What’s that? What’s a commit? A branch? What are we talkin’ about?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*-bE0eswzlP21-uuuky2X5g.jpeg" /></figure><h3>Commits</h3><p>We will talk about branches later; for what a commit is, I can tell you that is a snapshot of the state in which files and folders are when the commit has been created.</p><p>So, what we need here is to create a commit.</p><p>Git uses a two-stage process to create a commit: in the first one it creates the snapshots of the files into his “staging area”, in the second one it stores those snapshots into a commit object.</p><p>It’s easier done than said: let’s add the README file to the staging area</p><pre>$ git add README<br>$ git status<br>On branch master</pre><pre>Initial commit</pre><pre>Changes to be committed:<br>  (use &quot;git rm --cached &lt;file&gt;...&quot; to unstage)</pre><pre>new file: README</pre><p>Ok, added! git status shows that the file has been added to the stage (“Changes to be committed” sounds good, right?)</p><p>Let’s save those changes:</p><pre>$ git commit -m &quot;Added README&quot;<br>[master (root-commit) aeb27a2] Added README<br>1 file changed, 1 insertion(+)<br>create mode 100644 README</pre><p>Well done! We’ve created our first commit! But… Can you see it? aeb27a2 seems an object hash.</p><p>Let’s see what there’s inside!</p><pre>$ git cat-file -p aeb27a2<br>tree a8986e4af597c38dd3bb1ec08bea46cb123a929e<br>author Alessandro Chitolina &lt;alekitto@gmail.com&gt; 1614506381 +0100<br>committer Alessandro Chitolina &lt;alekitto@gmail.com&gt; 1614506381 +0100</pre><pre>Added README</pre><p>“Added README” is what I’ve written just a few minutes ago, but the other infos?</p><p>Well, “author” and “committer” seem pretty easy: that’s me. And the number must be the timestamp at which the commit has been created.</p><p>But the “tree”? Seems an object… Ok, let’s inspect it!</p><pre>$ git cat-file -p a8986e4af597c38dd3bb1ec08bea46cb123a929e<br>100644 blob deba01fc8d98200761c46eb139f11ac244cf6eb5 README</pre><p>Surprise, surprise! A tree is a “folder” representation on GIT. There’s only one file named “README”, stored as object deba01fc8d98200761c46eb139f11ac244cf6eb5(I’ve heard this number before, right?), with permissions 100644 (or just 0644) and type blob (an acronym for “Binary Large Object”, so it a file with <em>REAL</em> content).</p><p>Now we can create and add more files (or folders) with new commits, or modify the existing files, but even in this case this commit object will remain immutable and will always contain the same tree object pointer with the same files representation and this will ensure that no data will be lost in time.</p><p>End of part one. In the second part I will talk to you about branches, remotes and some useful git commands.</p><p>Thanks for reading.<br>A.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7fe34d40ae0e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codex/git-what-is-it-anyway-7fe34d40ae0e">Git: what is it anyway?</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[Why I stopped using API Platform]]></title>
            <link>https://medium.com/@alekitto/why-i-stopped-using-api-platform-c09cb030f76e?source=rss-441254540973------2</link>
            <guid isPermaLink="false">https://medium.com/p/c09cb030f76e</guid>
            <category><![CDATA[symfony]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[api-tool]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Alessandro Chitolina]]></dc:creator>
            <pubDate>Mon, 01 Mar 2021 07:02:16 GMT</pubDate>
            <atom:updated>2021-03-01T07:02:16.903Z</atom:updated>
            <content:encoded><![CDATA[<h4>And built my own toolkit to write rock solid APIs in PHP (and Symfony)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*plxV1rU09lYj5bKZ9zhjTw.png" /></figure><p><strong>Disclaimer</strong>: Though I’m listing here what make my life harder when using API Platform, I still consider it a great solution to build rapid crud prototypes. This article is addressed to those who want build a long-term api-based solution.</p><p>In principle there was FOSRestBundle, a shiny little magic box to create something similar to a REST API. Actually, there was too much magic in it..</p><h3><strong>And then API Platform came, and headaches with it.</strong></h3><p>Despite the documentation states that “Everything is fully customizable through a powerful event system and strong OOP”, the truth is that is nearly impossibile to customise deep behaviours and logic inside the api component and the choice to tightly couple it to the Symfony Serializer (probably the less powerful and poor of configuration options serializers I ever seen) make it even worse. But this is what I discovered only at the end of this incredible journey into API Platform..</p><h3><strong>The journey begins</strong></h3><p>The first thing I noted when I began to use API Platform was its no support for versioning. You can put a prefix in your path and pretend that is a versioning system, but the fact is that the tool does not help you at all. Ultimately I didn’t like versions in path: it is not so RESTy, I’d rather prefer to pass it through a request header (a custom one or an attribute of the Accept header).</p><p>So I began to start exploring services, normalizers, final classes with twelve (!) required dependencies and four optional dependencies to discover that everything in API Platform revolves around the OpenAPI specification.</p><h3><strong>OpenAPI, oh my!</strong></h3><p>I consider OpenAPI a great example on how NOT to write a specification. Partly because it tries to derive a protocol from an architectural standard (which REST is) and this is a nearly impossible task. And partly because of the limitations it puts upon a great designed protocol such as the HTTP.</p><p>For example: OpenAPI limits the HTTP methods you can use to the standardised ones (not even <em>all</em> the standardised methods, but this is another story), although the HTTP specification clearly states that a method is simply a group of US-ASCII characters. It does not even mandates the registration of non-standard methods to IANA, so why a specification used to define how to write your own API protocol should limit it?</p><p>Anyway, the extraordinary effort to produce an OpenAPI schema leads to the impossibility to have a versioned API for real. When I realised that, my mind blown up.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl0NhZ0aUSE8fXag12%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl0NhZ0aUSE8fXag12%2Fgiphy.mp4&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2Fl0NhZ0aUSE8fXag12%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4e886e0d7f43ab7cae176ae1ab8fb42/href">https://medium.com/media/c4e886e0d7f43ab7cae176ae1ab8fb42/href</a></iframe><p>After overriding something like 14 services just to modify a single call contained in a final class which is required throughout all the API Platform codebase without specifying an interface, I started doubting..</p><h3><strong>The journey continues: DTOs, Serializer and validation</strong></h3><p>One of the most important things I learned from my work on monolithic projects was the use of DTOs and the rich model entities. They allowed me to avoid errors in database, simplifies the logics of dynamic forms and started to be a sort of holy grail of my projects: it was simply too convenient to use it.</p><p>I thought that maybe the DTOs could help me with this problem too. I was wrong.</p><p>API Platform does support DTOs, but not very well (to be honest, one of my colleagues told me that the DTOs support is better now, but when I tried, it was very very bad). There was not so much documentation, so I had to try to use them directly on code.</p><p>First try: write a simple DTO, declare it in input and output of the ApiResource annotation, add validation annotations on DTOs properties, write the transformer, send correct data, everything’s ok. Then I tried to pass invalid data: 500.</p><p>Umm.. I opened the web profiler, went to validation section and… surprise! API Platform called validation on the entity. WTF?!</p><p>That approach is bad for multiple reasons, but is obviously incompatible with the rich model entities as invalid data will throw exceptions when trying to set them into the entity. In addition, if a field is invalid on the entity, but is not present on the DTO (or is present with an aliased name), an error with an unknown field is returned to the client. No way!<br><strong>Edit: </strong>someone told me that this behaviour is fixed now, but it was present at the time and anyway the DTOs support is still limited.</p><p>What makes it worse, is the use of Symfony Serializer which is impossible to replace. Its scarcity of configuration options (at the time a basic functionality such as the ignore attribute was not present) and the lack of control about the attributes to be exposed and the object construction was frustrating.<br>Additionally, the use of deserialisers which can throw an exception on invalid data makes everything really unstable and unreliable because those errors are returned to the client in a completely different way than the validation errors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qexDblPnCu2yMAbD" /><figcaption>Photo by <a href="https://unsplash.com/@usmanyousaf?utm_source=medium&amp;utm_medium=referral">Usman Yousaf</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Headaches Platform: the end of the journey</strong></h3><p>What makes everything more and more complicated was the intensive use of hydra and jsonld. The aim of API Platform is to be the only one, the only API present in the system. But in the real world you are probably working on one API exposed by one service developed alongside other services and other APIs. These APIs are probably communicating each other and expose foreign references and data.</p><p>Producing only ONE documentation for the whole architecture can’t be done from one of these services as it cannot know the architecture of the other ones.</p><p>Out-of-scope functionalities (JWT authentication, admin generators, etc), bad designed filters (cannot be aliased for example), pagination with pages (which are a frontend concept) and a limited support for PATCH requests gave me only the confirmation of my doubts:</p><blockquote>API Platform is not ready and is not designed to be a reliable API tool in a world of microservices.</blockquote><h3><strong>So what?</strong></h3><p>So I started from scratch. Started a new Symfony project, begin writing stuff to have a powerful versioning system, to be compatible with rich model entities, without the limitations of OpenAPI and with DTOs to be a central point of the API architecture.</p><p>Now I decided to open my code and publish it on GitHub. I wanted this project to be a rock solid toolbox to write REST APIs which are maintenable in a long long term project, learning from all the things I don’t like in other API toolsets.</p><p>I called it Solido, and here’s the doc: <a href="https://solid-o.github.io/docs/">https://solid-o.github.io/docs/</a></p><p>It’s obviously under active development and I’m looking for others to join me building a very strong, well written and useful open source API toolbox.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sDmWj5gG5VN7O6vX" /><figcaption>Photo by <a href="https://unsplash.com/@richygreat?utm_source=medium&amp;utm_medium=referral">Richy Great</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Thank you for reading,<br>A.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c09cb030f76e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>