StrongTypedId 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package StrongTypedId --version 1.0.3
NuGet\Install-Package StrongTypedId -Version 1.0.3
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="StrongTypedId" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StrongTypedId --version 1.0.3
#r "nuget: StrongTypedId, 1.0.3"
#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.
// Install StrongTypedId as a Cake Addin
#addin nuget:?package=StrongTypedId&version=1.0.3

// Install StrongTypedId as a Cake Tool
#tool nuget:?package=StrongTypedId&version=1.0.3

StrongTypedId

A super small library for providing strong typed Ids (as opposed to using primitives).

The benefit of this is simple: You don't run the risk of accidentally using the wrong type of id. (e.g. sending a UserId into a query for products)

This works through the use of an abstract base class (StrongTypedId<TStrongTypedId, TPrimitiveId>) which is inherited to gain the id functionality.

This project is inspired by Andrew Lock's StronglyTypedId: https://github.com/andrewlock/StronglyTypedId. However I needed support for .Net 5 and thus this project was born.

Installation

I recommend using the NuGet package: https://www.nuget.org/packages/StrongTypedId/ however you can also simply copy the class StrongTypedId into your project, as it's a single class solution.

Usage

Specify your class like this:

[JsonConverter(typeof(UserId.StrongTypedIdJsonConverter))]
public class UserId: StrongTypedId<UserId, Guid>
{
	public UserId(Guid value) : base(value)
	{
	}
}

This specifies that the class UserId is in fact a Guid and can be used in place of a Guid. And that's basically all there is to it, now you just use UserId in place of Guid where you're dealing with an User's Id.

You can omit the JsonConverter if you don't use json serialization.

Compatibility

WebAPI

This is supported through the use of the JsonConverter.

Entity Framework

You'll need to add a conversion for EF to work properly, luckily this is easily done in the OnModelCreating method on your DbContext.

Assume you have an User class which uses the UserId:

public class User
{
	public UserId Id { get; set; }
	public string Username { get;set; }
	// And so forth
}

You'll then add the conversion for the UserId in EF like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	modelBuilder.Entity<User>(entity =>
	{
		entity.HasKey(e => e.Id);

		entity.Property(e => e.Id)
				.HasConversion(e => e.PrimitiveId, primitiveId => new UserId(primitiveId));
	});
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net5.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on StrongTypedId:

Package Downloads
StrongTypedId.NewtonSoft

NewtonSoft.Json JsonConverter for the StrongTypedId library

StrongTypedId.Dapper.DDD.Repository

TypeConverter extension for easy usage of StrongTypedId with the Dapper.DDD.Repository.

StrongTypedId.EntityFrameworkCore

EntityFrame.Core ValueConverter for the StrongTypedId library

StrongTypedId.Swagger

Swagger support for the StrongTypedId library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.6 5,229 2/9/2024
1.6.5 324 2/5/2024
1.6.4 163 1/31/2024
1.6.3 704 1/8/2024
1.6.2 5,705 10/10/2023
1.6.1 513 9/8/2023
1.6.0 336 7/18/2023
1.5.1 427 7/15/2023
1.5.0 180 7/15/2023
1.4.3 324 7/11/2023
1.4.2 158 7/11/2023
1.4.1 191 7/11/2023
1.4.0 187 7/10/2023
1.3.0 1,092 5/21/2023
1.2.0 824 1/21/2023
1.1.7 1,859 12/7/2022
1.1.6 276 12/6/2022
1.1.5 583 8/5/2022
1.1.4 3,275 7/10/2022
1.1.3 787 7/10/2022
1.1.2 647 5/12/2022
1.1.1 1,009 3/12/2022
1.1.0 426 3/12/2022
1.0.3 375 3/12/2022
1.0.2 405 3/7/2022
1.0.1 412 3/6/2022
1.0.0 383 3/6/2022