LTRData.SemanticTypes.TypeSystem.Physics
2.0.1
dotnet add package LTRData.SemanticTypes.TypeSystem.Physics --version 2.0.1
NuGet\Install-Package LTRData.SemanticTypes.TypeSystem.Physics -Version 2.0.1
<PackageReference Include="LTRData.SemanticTypes.TypeSystem.Physics" Version="2.0.1" />
paket add LTRData.SemanticTypes.TypeSystem.Physics --version 2.0.1
#r "nuget: LTRData.SemanticTypes.TypeSystem.Physics, 2.0.1"
// Install LTRData.SemanticTypes.TypeSystem.Physics as a Cake Addin #addin nuget:?package=LTRData.SemanticTypes.TypeSystem.Physics&version=2.0.1 // Install LTRData.SemanticTypes.TypeSystem.Physics as a Cake Tool #tool nuget:?package=LTRData.SemanticTypes.TypeSystem.Physics&version=2.0.1
Semantic Types
Semantic Types help you reduce bugs and improve maintainability by letting the compiler ensure consistency in your code.
For example, instead of using a string everywhere to hold a email address, you create a new semantic type EmailAddress:
string emailAddressFromUser = ... ;
EmailAddress emailAddress = new EmailAddress(emailAddressFromUser);
The EmailAddress constructor ensures that the passed in value is a valid email address. If it is not valid, it throws an exception, so it fails hard and early.
Then where ever you use an email address, you use a EmailAddress, not a string. That gives you:
- Type based on meaning, not on physical storage: An EmailAddress is physically still a string. What makes it different is the way we think of that string - as an email address, not as a random collection of characters.
- Type safe: Having a distinct EmailAddress type enables the compiler to ensure you're not using some common string where a valid email address is expected - just as the compiler stops you from using a string where an integer is expected.
- Guaranteed to be valid: Because you can't create an EmailAddress based on an invalid email address, and you can't change it after it has been created, you know for sure that every EmaillAddress represents a valid email address.
- Documentation: When you see a parameter of type EmailAddress, you know right away it contain an email address, even if the parameter name is unclear.
Documentation
Introducing Semantic Types in .Net
Installation
Install via NuGet:
PM> Install-Package LTRData.SemanticTypes
Example
Here is an example implementation of a Semantic type. Note that almost all the functionality is in the SemanticType base class. This implements Equals, IComparable, the == operator, ToString and more:
public class EmailAddress : SemanticType<string>
{
public static bool IsValid(string value)
{
return (Regex.IsMatch(value,
@"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
@"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase));
}
public EmailAddress(string emailAddress) : base(IsValid, typeof(EmailAddress), emailAddress) { }
}
And here is how you might use it:
bool isValid = EmailAddress.IsValid("test@corp.com"); // True
EmailAddress emailAddress = new EmailAddress("test@corp.com"); // Ok
bool isValid = EmailAddress.IsValid("not a valid email address"); // False
EmailAddress emailAddress = new EmailAddress("not a valid email address"); // Throws exception
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- LTRData.SemanticTypes (>= 2.0.1)
-
.NETFramework 4.0
- LTRData.SemanticTypes (>= 2.0.1)
-
.NETStandard 2.0
- LTRData.SemanticTypes (>= 2.0.1)
-
net6.0
- LTRData.SemanticTypes (>= 2.0.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 |
---|---|---|
2.0.1 | 172 | 8/17/2023 |