StronglyTypedIds 1.0.1
dotnet add package StronglyTypedIds --version 1.0.1
NuGet\Install-Package StronglyTypedIds -Version 1.0.1
<PackageReference Include="StronglyTypedIds" Version="1.0.1" />
paket add StronglyTypedIds --version 1.0.1
#r "nuget: StronglyTypedIds, 1.0.1"
// Install StronglyTypedIds as a Cake Addin #addin nuget:?package=StronglyTypedIds&version=1.0.1 // Install StronglyTypedIds as a Cake Tool #tool nuget:?package=StronglyTypedIds&version=1.0.1
StronglyTypedIds
StronglyTypedIds helps to type entity identifiers for compile-time checking.
What problem are we solving?
Quite often, Guid, int, long, or string are used as identifiers for domain entities. Identifiers can be passed as arguments:
void AssignContractResponsible(Guid contractId, Guid employeeId);
In this example, 2 identifiers are used. You can only determine which one is which by the name of the argument. One day you can make a mistake and pass the identifiers in the wrong order. The problem is exacerbated if there are entities with similar names in the domain:
Contract и Contractor
Order и OrderTemplate
PriceList и PricePosition
Another example:
IDictionary<Guid, Contract> GetContractsByClients(IEnumerable<Guid> clientIds);
In this case, the client's identifier will probably act as the key of the dictionary. But we can't know this for sure, unless we look at the implementation.
Solution
The proposed solution is to pass not only the value of the identifier, but also the type of the identifiable entity.
So instead of:
void AssignContractResponsible(Guid contractId, Guid employeeId);
It becomes:
void AssignContractResponsible(GuidFor<Contract> contractId, GuidFor<Person> employeeId);
And instead of:
IDictionary<Guid, Contract> GetContractsByClients(IEnumerable<Guid> clientIds);
It will be:
IDictionary<GuidFor<Client>, Contract> GetContractsByClients(IEnumerable<GuidFor<Client>> clientIds);
License
This project is licensed under the MIT License - see the LICENSE.md file for details
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
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.