ktsu.StrongStrings 1.2.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package ktsu.StrongStrings --version 1.2.1                
NuGet\Install-Package ktsu.StrongStrings -Version 1.2.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="ktsu.StrongStrings" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ktsu.StrongStrings --version 1.2.1                
#r "nuget: ktsu.StrongStrings, 1.2.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 ktsu.StrongStrings as a Cake Addin
#addin nuget:?package=ktsu.StrongStrings&version=1.2.1

// Install ktsu.StrongStrings as a Cake Tool
#tool nuget:?package=ktsu.StrongStrings&version=1.2.1                

StrongStrings

A library that provides a transparent wrapper around system strings and gives you strong typing, compile time feedback, and runtime validation.

The intention is to be able to provide usage context and validation to naked strings in a similar way that Enums do for integers.

Usage

using ktsu.io.StrongStrings;

// Create a strong type by deriving a record class from StrongStringAbstract<TDerived>:
public record class MyStrongString : StrongStringAbstract<MyStrongString> { }

public class MyDemoClass
{
	public MyStrongString Stronk { get; set; } = new();

	public static void Demo(MyStrongString inStrongString, string inSystemString, out MyStrongString outStrongString, out string outSystemString)
	{
		// You can implicitly cast down to a System.String
		outSystemString = inStrongString;

		// You must explicitly cast up to a StrongString
		outStrongString = (MyStrongString)inSystemString;

		//You can provide a StrongString to a method that expects a System.String
		Path.Combine(inStrongString, inSystemString);

		// You can use the .WeakString property or the .ToString() method to get the value of the underlying System.String
		outSystemString = inStrongString.WeakString;
		outSystemString = inStrongString.ToString();

		// You can not implicitly cast up to a StrongString
		// outStrongString = inSystemString; // This will not compile

		// You can not cast from one StrongString to another
		// OtherStrongString other = inStrongString; // This will not compile
		// OtherStrongString other = (OtherStrongString)inStrongString; // This will not compile either
	}
}

Validation

You can provide custom validators which will throw a FormatException at runtime to help you catch data errors.

Implement the ktsu.io.StrongStrings.IValidator interface and provide it as a generic parameter when deriving your class:

public abstract class StartsWithHttp : IValidator
{
	public static bool IsValid(AnyStrongString? strongString)
	{
		ArgumentNullException.ThrowIfNull(strongString);

		return strongString.StartsWith("http", StringComparison.InvariantCultureIgnoreCase);
	}
}

public abstract class EndsWithDotCom : IValidator
{
	public static bool IsValid(AnyStrongString? strongString)
	{
		ArgumentNullException.ThrowIfNull(strongString);

		return strongString.EndsWith(".com", StringComparison.InvariantCultureIgnoreCase);
	}
}

public record class MyValidatedString : StrongStringAbstract<MyValidatedString, StartsWithHttp, EndsWithDotCom> { }
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on ktsu.StrongStrings:

Package Downloads
ktsu.Extensions

Extension Methods for common operations to help with expressiveness and maintainability.

ktsu.StrongPaths

A library that provides strong typing for common filesystem paths providing compile time feedback and runtime validation.

ktsu.AppDataStorage

Application data management library using JSON serialization to save and load data in the user's app data folder.

ktsu.CredentialCache

CredentialCache

ktsu.GitIntegration

Git Integration

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.10 189 11/13/2024
1.2.9 797 11/1/2024
1.2.8 1,574 10/4/2024
1.2.7 874 9/19/2024
1.2.6 310 9/19/2024
1.2.5 237 9/19/2024
1.2.4 96 9/18/2024
1.2.3 98 9/18/2024
1.2.2 201 9/18/2024
1.2.1 399 9/18/2024
1.2.0 523 9/18/2024
1.1.1 407 9/14/2024
1.1.0 93 9/14/2024