SafeAgainst 8.0.1
dotnet add package SafeAgainst --version 8.0.1
NuGet\Install-Package SafeAgainst -Version 8.0.1
<PackageReference Include="SafeAgainst" Version="8.0.1" />
paket add SafeAgainst --version 8.0.1
#r "nuget: SafeAgainst, 8.0.1"
// Install SafeAgainst as a Cake Addin #addin nuget:?package=SafeAgainst&version=8.0.1 // Install SafeAgainst as a Cake Tool #tool nuget:?package=SafeAgainst&version=8.0.1
SafeAgainst
Provides functionality for creation of both synchronous and asynchronous generic guard clauses with custom preconditions and already contains implementation of basic ones for Collections, Enums, Strings, Objects and Numeric types.
Usage
- Eliminate Leading or trailing white spaces
- passing value by reference:
// Arrange
var expectedValue = "Without leading or trailing whitespaces";
var value = $" {expectedValue} ";
// Act
StringGuards.Safe.AgainstLeadingOrTrailingWhiteSpaces(ref value);
// Assert
Assert.Equal(expectedValue, value);
- or using a
SafeContainer
:
// Arrange
var expectedValue = "Without leading or trailing whitespaces";
var value = $" {expectedValue} ";
var container = SafeContainer.Create(value);
// Act
StringGuards.Safe.AgainstLeadingOrTrailingWhiteSpaces(container);
value = container.Value;
// Assert
Assert.Equal(expectedValue, value);
- Assigning a predefined value, if enum contains an invalid one
- passing value by reference:
internal enum PowerOfTwo
{
One = 1,
Two = 2,
Four = 4,
Eight = 8,
Sixteen = 16
};
// Arrange
PowerOfTwo powerOfTwo = default;
var initialValue = powerOfTwo;
// Act
EnumGuards.Safe<PowerOfTwo>.AgainstNotInRange(ref powerOfTwo, PowerOfTwo.One);
// Assert
Assert.True(initialValue == 0);
Assert.Equal(powerOfTwo, PowerOfTwo.One);
- or using a
SafeContainer
:
// Arrange
PowerOfTwo powerOfTwo = default;
var initialValue = powerOfTwo;
var container = SafeContainer<PowerOfTwo>.Create(powerOfTwo);
// Act
EnumGuards.Safe<PowerOfTwo>.AgainstNotInRange(container, PowerOfTwo.One);
powerOfTwo = container.Value;
// Assert
Assert.True(initialValue == 0);
Assert.Equal(PowerOfTwo.One, powerOfTwo);
- Eliminating NULL elements from collection:
- passing value by reference:
// Arrange
IEnumerable<int?> enumeration = Enumerable.Empty<int?>()
.Append(2)
.Append(10)
.Append(null)
.Append(1);
// Act
CollectionGuards.Safe<int?>.AgainstNullElements(ref enumeration);
// Assert
Assert.DoesNotContain(enumeration, x => x == null);
Assert.Contains(enumeration, x => x != null);
Assert.True(enumeration.ToList().Count == 3);
- or using a
SafeContainer
:
// Arrange
IEnumerable<int?> enumeration = Enumerable.Empty<int?>()
.Append(2)
.Append(10)
.Append(null)
.Append(1);
var container = SafeContainer.Create(enumeration);
// Act
CollectionGuards.Safe<int?>.AgainstNullElements(container);
enumeration = container.Value;
// Assert
Assert.DoesNotContain(enumeration, x => x == null);
Assert.Contains(enumeration, x => x != null);
Assert.True(enumeration.ToList().Count == 3);
See way much more examples in the unit tests from the corresponding source repository.
Global Picture
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 (2)
Showing the top 2 NuGet packages that depend on SafeAgainst:
Package | Downloads |
---|---|
SafeAgainst.Extensions
Provides extension methods for both synchronous and asynchronous guard clauses for generic types, as well as for Enums, Objects, Collections, Strings, and various numeric types encapsulated in 'SafeContainer'. |
|
WcfClient.Dynamic
This package simplifies the process of consuming web endpoints of SOAP style, allowing dynamic (re)configuration of endpoint address, binding type, and all related data within appsettings. Incoming and outgoing messages, as well as other properties, can be configured using this package. |
GitHub repositories
This package is not used by any popular GitHub repositories.
No code smells or other issues based on the default Sonarcloud check