PvWay.LoggerService.MethodResultWrapper.nc6 1.0.1

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

// Install PvWay.LoggerService.MethodResultWrapper.nc6 as a Cake Tool
#tool nuget:?package=PvWay.LoggerService.MethodResultWrapper.nc6&version=1.0.1                

Method Result Wrapper Core DotNet 6 by pvWay

Provides a generic wrapper that returns whether or not a method succeeded or failed carrying the method result on success or a list of notifications in case of failure.

Interfaces

Interfaces are defined in the LoggerService.Abstractions nuGet

MethodResult interfaces

public interface IMethodResult
{
    /// <summary>
    /// At least one notification has a severity
    /// greater or equal to Error
    /// </summary>
    bool Failure { get; }

    /// <summary>
    /// No notification or all notifications severity
    /// are lower than Error
    /// </summary>
    bool Success { get; }

    SeverityEnum Severity { get; }

    /// <summary>
    /// Bulk string made of the concatenation
    /// of the notifications separated by new
    /// lines
    /// </summary>
    string ErrorMessage { get; }

    IEnumerable<IMethodResultNotification> Notifications { get; }

    void AddNotification(string message, SeverityEnum severity);
    void AddNotification(IMethodResultNotification notification);

    /// <summary>
    /// Will throw new Exception(ErrorMessage)
    /// </summary>
    void Throw();
}

public interface IMethodResult<out T> : IMethodResult
{
    T? Data { get; }
}

public interface IMethodResultNotification
{
    SeverityEnum Severity { get; }
    string Message { get; }
}

Features

  • MethodResult (implementing IMethodResult) is a class that

    • returns whether or not a method succeeded, has fatals, errors or warnings
    • the returned object provides
      • a boolean property named Failure that will be set when at least on notification has a severity of error or fatal
      • a boolean property named Success that is simply equals to !Failure
      • a list of notifications (message and severity)
      • an ErrorMessage string (list of notifications separated by new lines)
      • a method that allows to throw an exception
  • MethodResult<T> (inheriting from IMethodResult) is a generic class that

    • returns an object of type T if the method succeeded

Usage

using PvWay.LoggerService.Abstractions.nc6;
using PvWay.LoggerService.MethodResultWrapper.nc6;

namespace PvWay.LoggerServiceLab.nc6;

internal class MethodResultWrapperDemo
{
    private readonly ILoggerService _ls;
    private readonly IUserStore _userStore;

    public MethodResultWrapperDemo(
        ILoggerService ls,
        IUserStore userStore)
    {
        _ls = ls;
        _userStore = userStore;
    }

    public async Task<IMethodResult<string>> GetUserFirstNameAsync(
        string userName)
    {
        // let's call the GetUser Method and see its result
        // the method returns a IMethodResult<IUser> object
        var getUser = await GetUserAsync(userName);
        if (getUser.Failure)
        {
            // something wrong happened
            // let's log this and return a
            // MethodResult object that will carry
            // the notifications collected by the getUser method
            await _ls.LogAsync(getUser);
            return new MethodResult<string>(getUser);
        }

        // the user was found
        // let's get the user object from the getUser.Data
        var user = getUser.Data!; // this returns an IUser

        var firstName = user.FirstName;

        // let's call the MethodResult success constructor
        // by passing the expected data type object (here 
        // a string)
        return new MethodResult<string>(firstName);
    }

    private async Task<IMethodResult<IUser>> GetUserAsync(
        string userName)
    {
        try
        {
            var user = await _userStore.GetUserAsync(userName);
            if (user != null)
            {
                // the user was found
                // let's call the MethodResult success constructor
                // by passing the expected data type object (here 
                // a IUser object)
                return new MethodResult<IUser>(user);
            }

            // the user was not found...
            // this is a Business (non technical error)
            // let's construct a failure MethodResult object
            // with the Error (business error) severity
            var err = new MethodResult<IUser>(
                $"User {userName} not found", SeverityEnum.Error);

            // let's log this (business) error
            await _ls.LogAsync(err);

            // let's return the MethodResult to the caller
            return err;
        }
        catch (Exception e)
        {
            // something raised an exception...
            // for example the data base might not be up
            // let's log this fatal error
            await _ls.LogAsync(e);
            // let's construct and return a failure MethodResult
            // with the Fatal (technical error) severity
            // and the exception.
            return new MethodResult<IUser>(e);
        }
    }

}

internal interface IUser
{
    string FirstName { get; }
}

internal interface IUserStore
{
    Task<IUser?> GetUserAsync(string userName);
}

internal class UserStore : IUserStore
{
    public Task<IUser?> GetUserAsync(string userName)
    {
        throw new Exception();
    }
}

Output

<p style="color: red;"> crit:8/27/2023 6:25:52 AM TDHPRO18A.GetUserAsync.80 'Exception: Exception of type 'System.Exception' was thrown. StackTrace: at PvWay.LoggerServiceLab.nc6.UserStore.GetUserAsync(String userName) in C:\GitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.LoggerServiceLab.nc6\MethodResultWrapperDemo.cs:line 104 at PvWay.LoggerServiceLab.nc6.MethodResultWrapperDemo.GetUserAsync(String userName) in C:\GitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.LoggerServiceLab.nc6\MethodResultWrapperDemo.cs:line 52' </p>

<p style="color: red;"> crit:8/27/2023 6:25:52 AM TDHPRO18A.GetUserFirstNameAsync.31 'Fatal:Exception: Exception of type 'System.Exception' was thrown. StackTrace: at PvWay.LoggerServiceLab.nc6.UserStore.GetUserAsync(String userName) in C:\GitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.LoggerServiceLab.nc6\MethodResultWrapperDemo.cs:line 104 at PvWay.LoggerServiceLab.nc6.MethodResultWrapperDemo.GetUserAsync(String userName) in C:\GitHub\pvWayNuGetsSolution\LoggerServiceSolution\PvWay.LoggerServiceLab.nc6\MethodResultWrapperDemo.cs:line 52' </p>

See Also

The following nuGet packages implement the LoggerService

  • pvWay.LoggerService.nc6

    • ConsoleLogger: Colorful console implementation
    • MuteLogger: Silent logger for uTesting
    • MicrosoftLogger: uses the Microsoft.Extensions.Logger
    • MicrosoftConsoleLogger: uses the Ms AddConsole extension
    • MultiChannelLogger: writes to multiple logs in one shot
  • pvWay.MsSqlLogWriter.nc6 Implementation for Ms SQL Database

  • pvWay.PgSqlLogWriter.nc6 Implementation for PostgreSQL Database

Happy coding !

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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
3.0.3 91 8/4/2024
3.0.2 66 8/4/2024
3.0.0 69 8/2/2024
1.0.1 167 8/28/2023

new documentation