LTRData.SemanticTypes 2.0.1

dotnet add package LTRData.SemanticTypes --version 2.0.1                
NuGet\Install-Package LTRData.SemanticTypes -Version 2.0.1                
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="LTRData.SemanticTypes" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LTRData.SemanticTypes --version 2.0.1                
#r "nuget: LTRData.SemanticTypes, 2.0.1"                
#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 LTRData.SemanticTypes as a Cake Addin
#addin nuget:?package=LTRData.SemanticTypes&version=2.0.1

// Install LTRData.SemanticTypes as a Cake Tool
#tool nuget:?package=LTRData.SemanticTypes&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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on LTRData.SemanticTypes:

Package Downloads
LTRData.SemanticTypes.TypeSystem.Physics

Package Description

LTRData.SemanticTypes.Examples

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 461 8/17/2023