WithUnity.Tools 1.0.2-beta

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

// Install WithUnity.Tools as a Cake Tool
#tool nuget:?package=WithUnity.Tools&version=1.0.2-beta&prerelease                

WithUnity.Tools

WithUnity.Tools Provides:

  1. The Result class to allow fluent Validation.
  2. The MayBe struct is used in conjunction with Fody.NullGuard to make optional object parameters more evident and to ensure ArgumentNullException is thrown if the MayBe struct is not used.
  3. Logging of the validation results if desired using the Log class.
  4. The ValueProperty class to create readonly properties. Adding validation to individual properties and throw InvalidCastException if the data is not valid. The ValueProperty class can be made transparent to the user as implicit casts to and from the ValueProperty can be provided.
  5. A few common uses of ValueProperty classes FileExtension EmailAddress, Unicode16 string. Using these ValueProperty classes is transparent to the user as implicit casts to and from the ValueProperty and the HeldType have been provided.
  6. Result uses the ReflectiveTools class to determine where errors occur in the code. The Log class and the ReflectiveTools class can be used independently.

Example usage of Result, Maybe and a ValueProperty implementation.

/* Copyright 2015-2020 Robin Murison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
using NullGuard;
using System;

namespace WithUnity.Tools.ValueProperties
{
    /// <summary>
    /// A Value Property for validating email addresses.
    /// </summary>
    /// <remarks>
    ///     This is provided with minimal validataion for an EmailAddress. You may want to create your own variant with different validation.
    /// </remarks>
    public sealed class EmailAddress : ValueProperty<EmailAddress, string>
    {
        /// <summary>
        /// The Main constructor for an email address
        /// </summary>
        /// <param name="emailAddressValue"></param>
        /// <exception cref="InvalidCastException">This is thrown if the string passed in is not a valid email with the appropriate error.</exception>
        /// <remarks>
        /// This verifies:  
        ///     that the email is not <see langword="null"/>;
        ///     that email is at leaset 3 characters long;
        ///     that the email does not start with an @ sign;
        ///     that the email does not end with an @ sign;
        ///     that the email contains 1 and only 1 @ sign;
        /// </remarks>
        public EmailAddress(MayBe<string> emailAddressValue) : base(emailAddressValue)
        {
            Result<string> result = (emailAddressValue.HasValue ? Result.Ok<string>(emailAddressValue.Value) : Result.Fail<string>("Null email Address"))
                .OnSuccess<string>(ema => ema.Trim())
                .Ensure(ema => 3 <= ema.Length, "The email address is too short")
                .Ensure(ema => 0 < ema.IndexOf("@", StringComparison.InvariantCulture), "No preceding name before @ sign in EmailAddress")
                .Ensure(ema => ema.IndexOf("@", StringComparison.InvariantCulture) != (ema.Length - 1), "No domain name after @ sign in EmailAddress")
                .Ensure(ema => ema.Split('@').Length == 2, @"There are {ema.Split('@').Length - 1} @ signs. Should be 1.")
                .OnFailure(error => { throw new InvalidCastException(error); });
        }

        /// <summary>
        /// Implicit conversion for simpler readability to MayBe&lt;String&gt;
        /// </summary>
        /// <param name="possibleEmail"></param>
        public static implicit operator EmailAddress([AllowNull]string possibleEmail)
        {
            return new EmailAddress(new MayBe<string>(possibleEmail));
        }

        /// <summary>
        /// Implicit conversion from May&lt;Be&gt; string to be more explicit about whether the string is null.
        /// </summary>
        /// <param name="possibleEmail"></param>
        public static implicit operator EmailAddress(MayBe<string> possibleEmail)
        {
            return new EmailAddress(possibleEmail);
        }

        /// <summary>
        /// The Valid EmailAddress value
        /// </summary>
        public string EmailAddressValue
        {
            get
            {
                return Value.Value;
            }
        }

        /// <summary>
        /// Note: by the time this is called all checks for null and 
        /// NoValue have already been called so we just deal with the equality of valid objects.
        /// </summary>
        /// <param name="other"></param>
        /// <returns>whether the two are equal</returns>
        protected override bool EqualsCore(string other)
        {
            return Value.Equals(other);
        }

        /// <summary>
        /// ValueProperty inner GetHash routine
        /// </summary>
        /// <returns>an Integer hash of the  emailaddress class</returns>
        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
    }
}


Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.3 388 11/15/2021
1.2.1 279 10/14/2021
1.0.2 316 10/4/2021
1.0.2-beta 378 9/27/2021

This is a test release to get all the issues of releasing a Nuget Package correct.