CoreConditions 2.0.0
dotnet add package CoreConditions --version 2.0.0
NuGet\Install-Package CoreConditions -Version 2.0.0
<PackageReference Include="CoreConditions" Version="2.0.0" />
paket add CoreConditions --version 2.0.0
#r "nuget: CoreConditions, 2.0.0"
// Install CoreConditions as a Cake Addin #addin nuget:?package=CoreConditions&version=2.0.0 // Install CoreConditions as a Cake Tool #tool nuget:?package=CoreConditions&version=2.0.0
Welcome to CoreConditions
CoreConditions is a library that helps developers to write pre- and postcondition validations in their .NET applications. This is a fork of CuttingEdge.Conditions created by dotnetjunkie in order to make it compatible with .NET Core.
Overview
CoreConditions is build up upon the C# and VB.NET extension method mechanism and it allows you to validate arguments in a simple and fluent way. The following example gives a quick overview on the way you could write your pre- and postconditions.
C# example
public ICollection GetData(Nullable<int> id, string xml, IEnumerable<int> col)
{
// Check all preconditions:
Condition.Requires(id, nameof(id))
.IsNotNull() // throws ArgumentNullException on failure
.IsInRange(1, 999) // ArgumentOutOfRangeException on failure
.IsNotEqualTo(128); // throws ArgumentException on failure
Condition.Requires(xml, nameof(xml))
.StartsWith("<data>") // throws ArgumentException on failure
.EndsWith("</data>") // throws ArgumentException on failure
.Evaluate(xml.Contains("abc") || xml.Contains("cba")); // arg ex
Condition.Requires(col, nameof(col))
.IsNotNull() // throws ArgumentNullException on failure
.IsEmpty() // throws ArgumentException on failure
.Evaluate(c => c.Contains(id.Value) || c.Contains(0)); // arg ex
// Do some work
// Example: Call a method that should not return null
object result = BuildResults(xml, col);
// Check all postconditions:
Condition.Ensures(result, nameof(result))
.IsOfType(typeof(ICollection)); // throws PostconditionException on failure
return (ICollection)result;
}
public static int[] Multiply(int[] left, int[] right)
{
Condition.Requires(left, nameof(left)).IsNotNull();
// You can add an optional description to each check
Condition.Requires(right, nameof(right))
.IsNotNull()
.HasLength(left.Length, "left and right should have the same length");
// Do multiplication
}
VB.NET example
Public Function GetData(ByVal id As Integer?, ByVal xml As String, _
ByVal col As ICollection) As ICollection
' Check all preconditions:
Condition.Requires(id, nameof(id)).IsNotNull().IsInRange(1, 999).IsNotEqualTo(128)
Condition.Requires(xml, nameof(xml)).StartsWith("<data>").EndsWith("</data>")
Condition.Requires(col, nameof(col)).IsNotNull().IsEmpty()
' Do some work
' Example: Call a method that should not return null
Dim result = BuildResults(xml, col)
' Check all postconditions:
Condition.Ensures(result, "result").IsOfType(GetType(ICollection))
Return result
End Function
Public Shared Function Multiply(left As Integer(), right As Integer()) As Integer()
left.Requires("left").IsNotNull()
' You can add an optional description to each check
right.Requires("right").IsNotNull() _
.HasLength(left.Length, "left and right should have the same length")
' Do multiplication
End Function
The previous example showed some important features of the library. The example showed the library's ability to:
- do precondition checks by writing
Condition.Requires
; - do postcondition checks by writing
Condition.Ensures
; - fluently chain calls to the validation methods, just separated by a dot;
- work with nullable data types;
- validate string objects;
- validate collections;
- do a type check on the variable.
Note: A particular validation is executed immediately when it's method is called, and therefore all checks are executed in the order in which they are written.
Getting started
CoreConditions is available as NuGet package.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 | net461 was computed. net462 is compatible. 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 4.6.2
- No dependencies.
-
.NETStandard 2.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.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 119,470 | 7/30/2018 |
2.0.0-dev01 | 851 | 7/17/2018 |
2.0.0
- Initial version