SafeAgainst 8.0.1

dotnet add package SafeAgainst --version 8.0.1                
NuGet\Install-Package SafeAgainst -Version 8.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="SafeAgainst" Version="8.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SafeAgainst --version 8.0.1                
#r "nuget: SafeAgainst, 8.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 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

Build SonarCloud workflow codecov

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

  1. 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);
  1. 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);
  1. 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

General picture

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
8.0.1 195 6/2/2024
8.0.0 138 2/24/2024
7.0.1 117 6/23/2024
7.0.0 121 2/17/2024
6.0.0 163 2/10/2024
5.0.0 140 2/4/2024
3.1.0 135 1/27/2024
3.0.0 147 1/17/2024
2.2.0 153 1/13/2024
2.1.0 165 1/7/2024
2.0.0 169 1/3/2024
1.1.0 165 12/30/2023
1.0.0 150 12/27/2023

No code smells or other issues based on the default Sonarcloud check