Use Strongly Typed IDs for Entities

Опубликовано: 16 Март 2026
на канале: Zoran on C#
6,319
357

Become a sponsor to access source code ►   / zoranhorvat  
Join Discord server with topics on C# ► https://codinghelmet.com/go/discord
Enroll course Beginning Object-Oriented Programming with C# ► https://codinghelmet.com/go/beginning...
Subscribe ►    / @zoran-horvat  

If you are applying Domain-Driven Design and wish to implement a strongly typed ID for your entity class, then pay attention.
Many .NET programmers choose to define a record that wraps a primitive type, such as GUID.
Don't do this.
A record is a class, implying needless allocation, garbage collection, and performance penalty.
Use a readonly record struct instead. It is a value type that causes no overhead.
Expose a convenient factory property to create an empty ID.
And another to instantiate a unique value.
Then, use these in the entity class like a sir.
Strongly typed ID also gives you the opportunity to choose the underlying value and algorithm, such as lexicographically sortable identifiers.
Come to my channel and watch the full demo in .NET.

▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️COPYRIGHT 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.