StrongIDs is a NuGet package designed to simplify strongly typed IDs in C# projects by reducing boilerplate code and enhancing codebase clarity.
Strongly typed IDs provided by StrongIDs wrap Guids, offering a clean and efficient way to handle unique identifiers.
Install package via NuGet:
dotnet add package StrongIDsStrongly typed IDs can have the following access modifiers:
- protected internal
- protected
- internal
- public
Define a strongly typed ID using StrongIDs. Ensure it is readonly, partial, and a record struct. For example:
publicreadonlypartialrecordstructUserId:IStrongId<UserId>;This generates the following code, implementing the IEntityId interface:
publicpartialrecordstructUserId:IEntityId<UserId>{publicGuidValue{get;}publicboolHasValue=>Value!=Guid.Empty;privateUserId(Guidvalue)=>Value=value;publicstaticUserIdEmpty=>newUserId(Guid.Empty);publicstaticUserIdNew()=>newUserId(Guid.NewGuid());publicstaticUserIdParse(string?value)=>Guid.TryParse(value,outGuidid)?newUserId(id):Empty;}The IEntityId interface requires the above generated methods and properties to be implemented.