Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Maxi10022/StrongIDs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

StrongIDs

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.

Installation

Install package via NuGet:

dotnet add package StrongIDs

Usage

Strongly 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:

public readonly partial record struct UserId : IStrongId<UserId>;

This generates the following code, implementing the IEntityId interface:

public partial record struct UserId : IEntityId<UserId>
{
    public Guid Value { get; }

    public bool HasValue => Value != Guid.Empty;
    
    private UserId(Guid value) => Value = value;
    
    public static UserId Empty => new UserId(Guid.Empty);
    
    public static UserId New() => new UserId(Guid.NewGuid());

    public static UserId Parse(string? value) =>
        Guid.TryParse(value, out Guid id) ? new UserId(id) : Empty;
}

The IEntityId interface requires the above generated methods and properties to be implemented.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published