BWHazel.TypedId.EntityFrameworkCore 1.2.0

dotnet add package BWHazel.TypedId.EntityFrameworkCore --version 1.2.0
                    
NuGet\Install-Package BWHazel.TypedId.EntityFrameworkCore -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BWHazel.TypedId.EntityFrameworkCore" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BWHazel.TypedId.EntityFrameworkCore" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="BWHazel.TypedId.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BWHazel.TypedId.EntityFrameworkCore --version 1.2.0
                    
#r "nuget: BWHazel.TypedId.EntityFrameworkCore, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BWHazel.TypedId.EntityFrameworkCore@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BWHazel.TypedId.EntityFrameworkCore&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=BWHazel.TypedId.EntityFrameworkCore&version=1.2.0
                    
Install as a Cake Tool

Typed ID

A library for strongly-typed IDs for your entities!

Using Typed IDs

Typed IDs are available in the BWHazel.TypedId NuGet package.

A typed ID is a generic type which is associated with an entity, for example with the Person and Address types below. Notice how each of the IDs is tied to a specific entity type.

using BWHazel.Data;

public class Person
{
    public Uuid<Person> PersonId { get; set; }
    public Uuid<Address> AddressId { get; set; }
}

public class Address
{
    public Uuid<Address> AddressId { get; set; }
}

These typed IDs can be used in methods:

public bool IsAddressFor(Uuid<Person> personId, Uuid<Address> addressId) { }

These strongly typed IDs will ensure that IDs for different entities will not get confused. For this example, if the Person and Address IDs were swapped in the method call then it would result in a compile-time error.

Additionally, each type can be implicitly interconverted between and comapred with the underlying ID type, for example the Uuid<T> type uses a GUID:

Uuid<Person> personId = person.Id;
Guid personIdAsGuid = personId;

bool idEqualsGuid = personId == personIdAsGuid;
bool guidDoesNotEqualId = personIdAsGuid != personId;

Supported Types

Currently the library supports the following ID types, all in the BWHazel.Data namespace. Please see the detailed documentation, linked in the table below, for more examples.

ID Type Typed ID
GUID Uuid<T>
Integer IntId<T>
String StringId<T>

Use with Entity Framework Core

Entity Framework Core support for Typed IDs is available in the BWHazel.TypedId.EntityFrameworkCore NuGet package.

To use typed IDs in Entity Framework Core they need to be registered with the database context when creating the model in the DbContext.OnModelCreating() method. A helper method is provided to perform the correct mapping to database types for each typed ID in the Microsoft.EntityFrameworkCoew namespace.

Using the example Person and Address entities above this would look like the following. Notice that all uses of a typed ID need to be registered.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>()
        .Property(person => person.PersonId)
        .IsTypedUuid();

    modelBuilder.Entity<Person>()
        .Property(person => person.AddressId)
        .IsTypedUuid();

    modelBuilder.Entity<Address>()
        .Property(address => address.AddressId)
        .IsTypedUuid();
}

For each supported typed ID type, the helper methods are:

Typed ID Helper Method
Uuid<T> IsTypedUuid<T>()
IntId<T> IsTypedIntId<T>()
StringId<T> IsTypedStringId<T>()

A more generic example can be seen here.

Example Programs

Example programs are included to demonstrate how to use Typed IDs and their Entity Framework Core support.

Example Functionality
Basic Usage Creating typed IDs, how they interoperate with other typed IDs and underlying types and strong typing.
Entity Framework Core Using typed IDs with Entity Framework Core.

Motivation

IDs provide a unique identifier for entity objects and are often based on GUIDs or integers, e.g. using the example above:

public class Person
{
    public Guid PersonId { get; set; }
    public Guid AddressId { get; set; }
}

public class Address
{
    public Guid AddressId { get; set; }
}

However, this could result in runtime errors if the different IDs get confused, for example in the following method:

public bool IsAddressFor(Guid personId, Guid addressId) { }

What if the person and address IDs were passed into the method in the wrong order? As far as the compiler is concerned, no issue, but at runtime odd behaviour will result.

Hence, typed IDs!

Installation

Typed ID and Entity Framework Core support can be installed from NuGet:

Feature NuGet Package
Typed ID BWHazel.TypedId
Entity Framework Core Support BWHazel.TypedId.EntityFrameworkCore
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 410 12/26/2023
1.1.0 448 9/2/2023