StrongTypedId 1.9.2
dotnet add package StrongTypedId --version 1.9.2
NuGet\Install-Package StrongTypedId -Version 1.9.2
<PackageReference Include="StrongTypedId" Version="1.9.2" />
paket add StrongTypedId --version 1.9.2
#r "nuget: StrongTypedId, 1.9.2"
// Install StrongTypedId as a Cake Addin #addin nuget:?package=StrongTypedId&version=1.9.2 // Install StrongTypedId as a Cake Tool #tool nuget:?package=StrongTypedId&version=1.9.2
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. It has since evolved to .Net 7.
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(StrongTypedValueTypeConverter<UserId, Guid>))]
[StrongTypedIdJsonConverter<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.
Furthermore there are a couple of base classes available to you:
StrongTypedValue
for anything that's not an id, this supportsstring
as a primitive value.StrongTypedId
for anything that IS an id, this only supportsstruct
types as primitives (therefore nostrings
).- Adds the static
Parse(string)
andTryParse(string, out TStrongTypedId)
methods.
- Adds the static
StrongTypedGuid
a further specialization ofStrongTypedId
.- Adds the static
New()
method for instantiating new ids with random values as well as the staticEmpty
property.
- Adds the static
Finally you can recognize a StrongTyped
value by calling the extension method IsStrongTypedValue()
. All strong typed values furthermore implements both IStrongTypedValue<TPrimitiveValue>
as well as IStrongTypedValue
(The latter being strictly a marker interface).
Compatibility
Dapper.DDD.Repository
This can work without any extensions, however it's a bit simpler to use via the package StrongTypedId.Dapper.DDD.Repository.
Entity Framework
This is supported through the package StrongTypedId.EntityFrameworkCore.
WebAPI
This is supported through the use of the built-in JsonConverter
and TypeConverter
.
MVC
This is supported through the use of the built-in TypeConverter
.
NewtonSoft.Json
This is supported through the package StrongTypedId.NewtonSoft.
Swagger
This is supported through the package StrongTypedId.Swagger.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
NuGet packages (5)
Showing the top 5 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 |
|
StrongTypedId.MongoDB
Serialization extension for easy usage of StrongTypedId with MongoDB. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.9.2 | 437 | 11/15/2024 |
1.9.1 | 61 | 11/13/2024 |
1.9.0 | 102 | 11/11/2024 |
1.8.0 | 14,272 | 8/26/2024 |
1.7.0 | 16,019 | 5/10/2024 |
1.6.6 | 8,295 | 2/9/2024 |
1.6.5 | 382 | 2/5/2024 |
1.6.4 | 227 | 1/31/2024 |
1.6.3 | 835 | 1/8/2024 |
1.6.2 | 5,772 | 10/10/2023 |
1.6.1 | 589 | 9/8/2023 |
1.6.0 | 397 | 7/18/2023 |
1.5.1 | 482 | 7/15/2023 |
1.5.0 | 238 | 7/15/2023 |
1.4.3 | 366 | 7/11/2023 |
1.4.2 | 206 | 7/11/2023 |
1.4.1 | 233 | 7/11/2023 |
1.4.0 | 217 | 7/10/2023 |
1.3.0 | 1,134 | 5/21/2023 |
1.2.0 | 878 | 1/21/2023 |
1.1.7 | 2,330 | 12/7/2022 |
1.1.6 | 300 | 12/6/2022 |
1.1.5 | 606 | 8/5/2022 |
1.1.4 | 3,749 | 7/10/2022 |
1.1.3 | 844 | 7/10/2022 |
1.1.2 | 660 | 5/12/2022 |
1.1.1 | 1,078 | 3/12/2022 |
1.1.0 | 465 | 3/12/2022 |
1.0.3 | 399 | 3/12/2022 |
1.0.2 | 437 | 3/7/2022 |
1.0.1 | 444 | 3/6/2022 |
1.0.0 | 398 | 3/6/2022 |