<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anthony</title>
    <description>The latest articles on DEV Community by Anthony (@anthonytr).</description>
    <link>https://dev.to/anthonytr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F866080%2F0ae2f21d-4699-4cb8-b075-f86cafca05dc.jpg</url>
      <title>DEV Community: Anthony</title>
      <link>https://dev.to/anthonytr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anthonytr"/>
    <language>en</language>
    <item>
      <title>Why you shouldn’t use the Dynamic type in .NET</title>
      <dc:creator>Anthony</dc:creator>
      <pubDate>Fri, 27 May 2022 07:15:01 +0000</pubDate>
      <link>https://dev.to/anthonytr/why-you-shouldnt-use-the-dynamic-type-in-net-3hk6</link>
      <guid>https://dev.to/anthonytr/why-you-shouldnt-use-the-dynamic-type-in-net-3hk6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you’re an enthusiast like myself, you will get excited when Microsoft release some sort of updates on the language, new libraries or any documentation on what is the new fancy way of doing ‘This’.&lt;/p&gt;

&lt;p&gt;Microsoft target those releases to respond and enhance our development experience, thus each feature is the answer to a specific problem in our code. But as times goes by, new developers may question the relevance of some of these additions, and ultimately have a stand with or against them. So, is Dynamic and ExpandoObject obsolete already? Should we use them?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Rant :)
&lt;/h2&gt;

&lt;p&gt;The idea for this article came from a PR I was recently checking, I saw my colleague using Dynamic and ExpandoObject… (Sorry R**** !)&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Dynamic
&lt;/h2&gt;

&lt;p&gt;Briefly, the usage of dynamic tell the compiler to skip all checks like syntaxes and evaluate them on runtime instead:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dynamic test=new object();&lt;/code&gt;&lt;br&gt;
&lt;code&gt;test.Bark();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This code will compile normally, even though we know that object does not contain a Bark() function. This error will be shown on runtime instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Just like the Async keyword, when you start using dynamic, you can’t stop. Your dynamic code will expand just like a virus and bubble up everywhere, removing one of the most underrated benefits of C# which is static types and compile-time checking…&lt;/p&gt;

&lt;p&gt;In a way, you will start writing Python or JS in your C# code. If you really want to do that, use that other language from the beginning !&lt;/p&gt;

&lt;p&gt;Next, &lt;strong&gt;Readability&lt;/strong&gt;: You’re using an object that you cannot track with your IDE, how can you express your intent with the code you’re writing if even the compiler will skip understanding it. Even C# does not understand what you’re writing and will evaluate it on the fly so how should we ;)&lt;/p&gt;

&lt;p&gt;As it wasn’t enough, &lt;strong&gt;Performance&lt;/strong&gt;: The building and compilation phase saves a lot of time ahead of execution. This is the main difference between compiled and interpreted languages…&lt;br&gt;
Each one has his pros and cons but in a cloud environment when you want to squeeze every bit of performance from your app, you should also consider that.&lt;/p&gt;

&lt;p&gt;Last but certainly not least, think about your bug hunting journeys, when a newcomer changes anything in the objects or functions. Do you really feel safe while using this feature?&lt;br&gt;
Even if you have unit tests, doesn’t accepting each and every possible value in your algorithm scare you?&lt;br&gt;
&lt;strong&gt;Well it should !&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Exception
&lt;/h2&gt;

&lt;p&gt;If this is the case, Why did Microsoft introduce it back in 2010 ?! As discussed before each feature belong to a specific context and solve a problem. So back in the time, C# was Windows exclusive. If you remember .NET Framework (Time flies…), it was a direct competitor to java and other languages.&lt;/p&gt;

&lt;p&gt;With the dynamic type feature, Microsoft wanted to achieve something called &lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/native-interop/cominterop"&gt;COM Interop&lt;/a&gt;. The idea was that you can integrate and run diverse applications and platforms inside C# and .NET. This was good for integrating with existing systems and scripts into your codebase.&lt;/p&gt;

&lt;p&gt;A practical example would be that you have an existing Ruby app and you started using C# for a Desktop App. You can install a library called IronRuby and load your Ruby code INSIDE C#. So if you created a class in Ruby, you can instantiate and use it in your .NET App, which was insane !&lt;/p&gt;

&lt;p&gt;This feature was actually made for this purpose. Nowadays with our new and portable .NET. We don’t find this very relevant most of the time and is considered a niche feature.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Actual Exception
&lt;/h2&gt;

&lt;p&gt;But there’s only a small practical scenario where I still use them… Navigating a complex JSON object. Honestly this is arguably the only context where I see this fitting even.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to not use it
&lt;/h2&gt;

&lt;p&gt;I am a huge fan of static types and compile time checking. So if I can remove dynamics and even boxing types I would find that ideal.&lt;/p&gt;

&lt;p&gt;So as a general rule and conclusion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try to use generics, if that doesn’t work:&lt;/li&gt;
&lt;li&gt;Try to use objects and boxing, if and only if it doesn’t work:&lt;/li&gt;
&lt;li&gt;Your only resort is now the Dynamic type and ExpandoObject :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But hey, at least you tried !&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>programming</category>
      <category>microservices</category>
      <category>performance</category>
    </item>
    <item>
      <title>Why you shouldn't use the Repository pattern in .NET</title>
      <dc:creator>Anthony</dc:creator>
      <pubDate>Sun, 22 May 2022 08:55:25 +0000</pubDate>
      <link>https://dev.to/anthonytr/why-you-shouldnt-use-the-repository-pattern-in-net-4ke1</link>
      <guid>https://dev.to/anthonytr/why-you-shouldnt-use-the-repository-pattern-in-net-4ke1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;So you’re writing code using .NET, and you surely follow the Clean Architecture and its layered abstractions. Apparently and even though the concept was introduced long ago, this is getting very trendy now.&lt;/p&gt;

&lt;p&gt;One of your arguments may be that you want your infrastructure to be entirely swappable, you want your application and core business logic to be independent from the database you are using right? The idea is not wrong, as they are hundreds of application relying on this concept. So what is the problem?&lt;/p&gt;

&lt;h2&gt;
  
  
  Designing an application
&lt;/h2&gt;

&lt;p&gt;Applications are needed to solve a business need. When you design an app, you want it to fill a business and domain gap. Every app stores its data somewhere right?&lt;/p&gt;

&lt;p&gt;So you have to carefully choose your infrastructure that corresponds to your application. Let’s say you’re choosing which database to use, how do you generally make the choice?&lt;/p&gt;

&lt;p&gt;From my biased and opinionated experience, this choice comes from one of the following criteria's:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your team is already using SQL Server in this other project, so you will most likely start with it. Install Entity Framework, build your context and you’re done. It can be any provider really..&lt;/li&gt;
&lt;li&gt;You’re tied to the technology stack of the company you are in and you can’t just do as you please.&lt;/li&gt;
&lt;li&gt;You’re tied by the on premises or cloud constraints of your app (Pricing for example).&lt;/li&gt;
&lt;li&gt;Or you actually have this freedom to choose your most optimal database, isn’t that great?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It surely is, but let’s think this through… To persist your business domain, you will start to think on the ‘How should I store my entities’. This will also vary depending on the database you are using:&lt;br&gt;
If you’re using SQL, you will start drawing links and relations to navigate your fixed tables.&lt;br&gt;
If you’re using DynamoDB or Cosmos, you will think about your partition and sort keys, how the entities should be grouped and distributed.&lt;br&gt;
If you’re using MongoDB or anything else really, you will find yourself between the two.&lt;/p&gt;

&lt;p&gt;My point is, even though there is a lot of database providers, your plan to concept your database may vary a lot depending on the infrastructure you are using. And yes your domain should not worry about those changes so let’s abstract it. This is when the famous Repository pattern come in handy.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;You abstracted your famous repository, which contains another abstraction of your database provider (EF Core for example). The benefit? you can swap now the inner abstraction with Dapper… but why would we do that?&lt;/p&gt;

&lt;p&gt;The abstraction itself is great, but why do we need to implement it even if it’s not that useful? With that thinking, we should abstract the ILogger interface, or any other module in our system. So that we can ‘swap it when needed’, even when it’s not actually needed…&lt;/p&gt;

&lt;p&gt;You’re actually smarter than this, you need to switch from SQL to Cosmos for a cloud migration on Azure. So this is actually very handy right? Well not really:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The repository interface is a contract that the application is using and cannot be changed. If it changes than you actually implemented all of this for nothing.&lt;/li&gt;
&lt;li&gt;Not changing it is also a problem, even if you’re app is not tied to the persisted entities, storing and optimizing transactions is highly dependent on the provider. You will store, retrieve and perform CRUD actions differently when switching from SQL to Cosmos. And to honor this famous contract that you cannot break, you will find yourself writing a big mud of code to transform and fetch your entities.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Projecting over time and supposing you’re in a Agile environment where iterating and adding functionalities is mandatory, this bug ball of mud will grow to an unmaintainable or manageable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ‘SMART’ solution
&lt;/h2&gt;

&lt;p&gt;A very smart idea will pop up in your head. The app is really slow and need optimization. This bottleneck will guide to write the entire thing from scratch while optimizing and accommodating to a new schema and a new DB provider. Everything ends well you saved the day !&lt;/p&gt;

&lt;h2&gt;
  
  
  Feedback
&lt;/h2&gt;

&lt;p&gt;You didn’t actually save the day, you waited and built a big ball of mud that created maintainability and performance issues. It most certainly took you double the time to rewrite with all the functionalities you added over time.&lt;/p&gt;

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

&lt;p&gt;Knowing how to abstract your repository is great, but this should not be a standard where you implement everywhere. Most of the DB clients are already an abstraction so why add another layer on the top? If you’re in a Microservices architecture, your bounded context should not be big enough to rewrite it. 1 or 2 entities are involved only.&lt;/p&gt;

&lt;p&gt;Also, it is not that you change your provider everyday, you chose or are bound to a provider and when you do change (which you rarely, RARELY do), it is really useful to see what will break and make the most adequate and optimal change for this new source.&lt;/p&gt;

&lt;p&gt;DON”T OVERUSE THIS PATTERN FOR THE SAKE OF CLEAN CODE.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>microservices</category>
    </item>
  </channel>
</rss>
