StrongTypedId.NewtonSoft
1.1.1
See the version list below for details.
dotnet add package StrongTypedId.NewtonSoft --version 1.1.1
NuGet\Install-Package StrongTypedId.NewtonSoft -Version 1.1.1
<PackageReference Include="StrongTypedId.NewtonSoft" Version="1.1.1" />
paket add StrongTypedId.NewtonSoft --version 1.1.1
#r "nuget: StrongTypedId.NewtonSoft, 1.1.1"
// Install StrongTypedId.NewtonSoft as a Cake Addin #addin nuget:?package=StrongTypedId.NewtonSoft&version=1.1.1 // Install StrongTypedId.NewtonSoft as a Cake Tool #tool nuget:?package=StrongTypedId.NewtonSoft&version=1.1.1
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. However I needed support for .Net 5 and thus this project was born.
Installation
I recommend using the NuGet package: StrongTypedId however feel free to clone the source instead if that suits your needs better.
Usage
Specify your class like this:
[TypeConverter(typeof(StrongTypedIdTypeConverter<UserId, Guid>))]
[JsonConverter(typeof(SystemTextJsonConverter<UserID, Guid>))]
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 as well as the TypeConverter
if you're not using WebAPI or MVC.
Compatibility
MVC
This is supported through the use of the TypeConverter
.
WebAPI
This is supported through the use of the JsonConverter
and TypeConverter
.
NewtonSoft.Json
You'll need a NewtonSoft based JsonConverter
for this. Add the Nuget package StrongTypedId.NewtonSoft which includes the NewtonSoftJsonConverter
class for this purpose.
The reason it's split into another package is to avoid NewtonSoft dependencies in the core package, for those who don't use NewtonSoft.
After adding the package, just add converter similarly to the built-in converters:
[TypeConverter(typeof(UserId.StrongTypedIdTypeConverter))]
[JsonConverter(typeof(UserId.StrongTypedIdJsonConverter))]
[Newtonsoft.Json.JsonConverter(typeof(UserId.StrongTypedIdNewtonSoftJsonConverter))]
public class UserId: StrongTypedId<UserId, Guid>
{
public UserId(Guid value) : base(value)
{
}
}
Notice how you can have both JsonConverters applied simultaneously to support both WebAPI and NewtonSoft at the same time.
Entity Framework
You'll need an Entity Framework based ValueConverter
for this. Add the Nuget package StrongTypedId.EntityFrameworkCore which includes the StrongTypedIdValueConverter
class for this purpose.
After doing that you need to add the converter to EF in your DbContext
class like this:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder
.Properties<UserId>()
.HaveConversion<StrongTypedIdValueConverter<UserId, Guid>>();
}
And that's all there is to it, now any of your entities can use the UserId
in place of guids.
Product | Versions 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. |
-
net5.0
- Newtonsoft.Json (>= 13.0.1)
- StrongTypedId (>= 1.1.1)
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.9.2 | 1,942 | 11/15/2024 |
1.9.1 | 69 | 11/13/2024 |
1.9.0 | 86 | 11/11/2024 |
1.8.0 | 13,945 | 8/26/2024 |
1.7.0 | 15,736 | 5/10/2024 |
1.6.6 | 8,183 | 2/9/2024 |
1.6.5 | 314 | 2/5/2024 |
1.6.4 | 169 | 1/31/2024 |
1.6.3 | 694 | 1/8/2024 |
1.6.2 | 5,655 | 10/10/2023 |
1.6.1 | 405 | 9/8/2023 |
1.6.0 | 180 | 7/18/2023 |
1.5.1 | 206 | 7/15/2023 |
1.5.0 | 212 | 7/15/2023 |
1.4.3 | 183 | 7/11/2023 |
1.4.2 | 183 | 7/11/2023 |
1.4.1 | 184 | 7/11/2023 |
1.4.0 | 175 | 7/10/2023 |
1.3.0 | 194 | 5/21/2023 |
1.2.0 | 353 | 1/21/2023 |
1.1.5 | 1,601 | 8/5/2022 |
1.1.4 | 1,324 | 7/10/2022 |
1.1.3 | 434 | 7/10/2022 |
1.1.2 | 429 | 5/12/2022 |
1.1.1 | 417 | 3/12/2022 |