Layne  Fadel

Layne Fadel


Strongly-typed Ids using C# Source Generators

It’s common to use intGuid, or string to represent entity ids because these types are well-supported by databases. The problems come when you have methods with multiple parameters of the same type. In this case, it’s easy to mix up parameters when calling these methods.


  • Code is self-documented
  • Leverage the compiler to avoid sneaky errors
  • It gives a location to add constant, methods, properties related to the type


  • Needs to create a wrapper for each id type
  • You need to write more code (but Source Generators do it for you!)
  • Doesn’t work with serializer / Entity Framework Core / ASP.NET Core (but there are easy workaround as explained here-after)

Note: If you use a struct to wrap the primitive type, you’ll get the same performance as using the primitive type directly. #c#

Strongly-typed Ids using C# Source Generators