Why Don’t C# Records Validate Their Values?

preview_player
Показать описание
I receive repeated questions from many programmers asking why don't I add validation to records in C#.
The answer is simple. Records are just values. Could an int validate itself? The same value could sometimes be valid, other times not. It all depends on the outer context.
A record, many believe, is more than just an int, and so it should validate itself. Let's examine that.
Should a 2-dimensional size insist on being positive? Or at least non-negative? Again, whatever the choice, that depends on the context.
You better have a separate function or an interface that does that. Now we're talking!
One variant would only allow strictly positive values. Another allows vertical and horizontal flipping, but will not let the size degenerate into a line. Yet another would allow just anything.
It is therefore common to pick or inject an appropriate strategy in code which must validate records on creation.

Thank you so much for watching! Please like, comment & share this video as it helps me a ton!! Don't forget to subscribe to my channel for more amazing videos and make sure to hit the bell icon to never miss any updates.🔥❤️

⭐ Learn more from video courses:
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my YouTube channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️RIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.

#csharp #dotnet #functionalprogramming
Рекомендации по теме
Комментарии
Автор

I have been professionally coding for 30 years.
"Can an int validate itself?" just single-handedly leveled me up another notch.

thygrrr
Автор

Yup! Factory methods are the way forward, I would probably return empty records on failing validation, or even better an empty maybe monad to avoid unneeded exceptions though😊

adamgoldsmith
Автор

In your scenario, you cannot create self-validating Size, because you don't have all required information. When you're operating on Size, you don't know if it's Mirrored or Regular, so later in the code, when you get Size as a parameter, you will also need to check if it's Mirrored or Regular. Wouldn't it better to also store that piece of information inside the Size? It would also allow you to self-validate the object. Maybe it's worth to create a video about why self-validating records are not a good idea? I've been using this approach for some time and never run into an issue.

ProstoDoCelu
Автор

Are you suggesting that callers should always validate e.g. Email value object before using them because the value object will never guarantee it's validity?

pyce.
Автор

What advantage does this bring over factory method?

iorch
Автор

Hi Zoran. Let's say you create a domain where all Size objects must be of the Mirrored type. If the Size record has a public ctor with no inherent validation, how would you enforce this invariant for all consumers of the type?

r
Автор

I so disagree. That's why I only use records for dtos. Otherwise I keep using ValueObject. The pb with having both a public ctor as well as factories is that some developers will use the ctor where they should have used a factory.

francoismouchati
Автор

Can you explain to this noob what the delegate Size Create is doing please ? 😅

slowjocrow