What are the benefits of using Primitively?
Codebases that adopt the Primitively approach of encapsulating single value .NET native types, offer the following benefits: -
- Avoids Primitive Obsession: Using Primitively source generated types helps avoid bugs introduced by using primitive types¹². For further information see What is Primitive Obsession?.
- Catch Errors Early: The C# type system can catch errors for us, such as requesting an entity using the wrong ID¹. Primitively provides type safety and ensures that apples can only ever be compared with apples, and pears can only ever be compared to pears.
- Self-Documented Code: The code becomes self-documented, making it easier to understand³. Primitively source generated types contain metadata properties that negate the need to use reflection. Each class library that references Primitively contains a source generated repository class called PrimitiveRepository. Metadata for each Primitively type in the assembly is compiled into a collection. At runtime, this information can be accessed e.g. during start up, in a highly optimised way without the need for reflection.
- Compiler Assistance: It leverages the compiler to avoid sneaky errors³.
- Location for Related Elements: It provides a location to add validation, constants, methods, properties related to the type³.
- Avoid Helper Classes: No more need for helper classes³.
Appendix
Source: Conversation with Bing, 06/02/2024
- An introduction to strongly-typed entity IDs - Andrew Lock.
- Using C# 9 records as strongly-typed ids - Thomas Levesque.
- Strongly-typed Ids using C# Source Generators - Meziantou's blog.
- Strongly-typed Ids using C# Source Generators - Meziantou's blog.
- GitHub - andrewlock/StronglyTypedId: A Rosyln-powered generator - Andrew Lock.
- Strongly-typed ID update 0.2.1 - Andrew Lock.