<?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>Forem: Thomas</title>
    <description>The latest articles on Forem by Thomas (@thom).</description>
    <link>https://forem.com/thom</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%2F1163387%2Fb09ca68f-15bf-44bb-a15f-881372bccec7.png</url>
      <title>Forem: Thomas</title>
      <link>https://forem.com/thom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thom"/>
    <language>en</language>
    <item>
      <title>A clean way to use required value types in ASP.NET Core</title>
      <dc:creator>Thomas</dc:creator>
      <pubDate>Sun, 17 Sep 2023 16:07:08 +0000</pubDate>
      <link>https://forem.com/thom/a-clean-way-to-use-required-value-types-in-aspnet-core-43jb</link>
      <guid>https://forem.com/thom/a-clean-way-to-use-required-value-types-in-aspnet-core-43jb</guid>
      <description>&lt;p&gt;How do you deal with required value types in input models without having to make all of them nullable?&lt;/p&gt;

&lt;p&gt;For a long time, the only solutions out there suggested:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;[BindRequired]&lt;/code&gt;, which doesn't work for body data.&lt;/li&gt;
&lt;li&gt;Using something like &lt;code&gt;[Range]&lt;/code&gt;, which only works for integers and only if they cannot be zero.&lt;/li&gt;
&lt;li&gt;Just making all your properties nullable, which is ugly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But you can actually get your input models looking like this by making a custom value provider &amp;amp; model binder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="n"&gt;InputModel&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Required&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ErrorMessage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Optional custom error"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;NonNullableInteger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The entire tutorial is kept updated on my blog, alongside source code and an example project: &lt;a href="https://thom.ee/blog/clean-way-to-use-required-value-types-in-asp-net-core/"&gt;https://thom.ee/blog/clean-way-to-use-required-value-types-in-asp-net-core/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aspnet</category>
      <category>webdev</category>
      <category>csharp</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
