CSharp.StatusGeneric 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharp.StatusGeneric --version 1.2.1
                    
NuGet\Install-Package CSharp.StatusGeneric -Version 1.2.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="CSharp.StatusGeneric" Version="1.2.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharp.StatusGeneric" Version="1.2.1" />
                    
Directory.Packages.props
<PackageReference Include="CSharp.StatusGeneric" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharp.StatusGeneric --version 1.2.1
                    
#r "nuget: CSharp.StatusGeneric, 1.2.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.
#:package CSharp.StatusGeneric@1.2.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharp.StatusGeneric&version=1.2.1
                    
Install as a Cake Addin
#tool nuget:?package=CSharp.StatusGeneric&version=1.2.1
                    
Install as a Cake Tool

GenericServices.StatusGeneric

This is a fork of GenericServices.StatusGeneric, extending the original library with additional features.


This NuGet library provides a way to return the status of a method/class that you run. It contains two main things

  1. A IReadOnlyList of Errors, which may be empty. If the list is empty, then the IsValid property of the status will be true.
  2. A Message which can be set by you (default value is "Success"), but if it has any errors the Message returns "Failed with nn errors".
  3. The IStatusGeneric<T> version will return a Result which you can set, but returns default(T) if there are errors.

There are various methods to add errors to the Errors list, and a way to combine statuses which I show next.

NOTE: There is a companion library called EfCore.GenericServices.AspNetCore which can convert a GenericService into Model errors for ASP.NET Core MVC controllers or Razor pages, AND ASP.NET Core Web API - well worth looking at.

Simple usage examples

  1. Returns just a status telling you if the method was successful. You can also set the Message in the status - default is "Success".
public IStatusGeneric NonStatusGenericString(string s)
{
    var status = new StatusGenericHandler();

    //add error and return immediately
    if (s == null)
        //You can return just an error message, but adding the property name
        //will improve the error feedback in ASP.NET Core etc.
        return status.AddError("input must not be null", nameof(s));

    status.Message = "All went well";

    //If no errors were added then its returns a IsValid status with the message
    //If there are errors then the Message says something like "Failed with 1 error"
    //and the HasErrors will be true, IsValid will be false
    return status;
}

  1. Returns a status with a value
public IStatusGeneric<string> StatusGenericNumReturnString(int i)
{
    var status = new StatusGenericHandler<string>();

    //add error and return immediately
    if (i <= 0)
        return status.AddError("input must be positive", nameof(i));

    //This sets the Result property in the generic status
    status.SetResult(i.ToString());

    //If there are errors then the Result is set to the default value for generic type
    return status;
}

Typical patterns

You can use StatusGeneric in a few ways, but here is an example of the patterns I often find myself using.

public IStatusGeneric<int> StatusGenericNum(int i)
{```
    var status = new StatusGenericHandler<int>();

    //series of tests and then return all the errors together
    //Good because the user gets all the errors at once
    if (i == 20)
        status.AddError(HttpStatusCode.BadRequest, "input must not be 20", nameof(i));
    if (i == 30)
        status.AddError("input must not be 30", nameof(i));
    if (i == 40)
        status.AddError("input must not be 40", nameof(i));
    if (i == 50)
        status.AddError("input must not be 50", nameof(i));
    if (!status.IsValid)
        return status;

    //add error and return immediately
    if (i <= 0)
        return status.AddError("input must be positive", nameof(i));

    //combine error from another non-StatusGeneric method and return if has errors
    status.CombineStatuses(NonStatusGenericString(i == 10 ? null : "good"));
    if (status.HasErrors)
        return status;

    //combine errors from another generic status, keeping the status to extract later
    var stringStatus = StatusGenericNumReturnString(i);
    if (status.CombineStatuses(stringStatus).HasErrors)
        return status;

    //Do something with the Result from the StatusGenericNumReturnString method
    var combinedNums = int.Parse(stringStatus.Result) + i;
    //Set the result to be returned from this method if there are no errors
    status.SetResult(combinedNums);

    //You can put tests just before a result would be returned  - any error will set the result to default value
    if (combinedNums <= 0)
        status.AddError("input must be positive", nameof(i));

    //If you return a IStatusGeneric<T> and there are errors then
    //1. the result will be set to default value for that type
    //2  The Message will be set to "Failed with xx errors"
    return status;
}

When unit testing...

If you are testing a service its always good to check that the service returns the status you expect. If you expect it to pass then you can use this following xUnit fluent test which will

  1. Test that it was a successful result
  2. Show you the errors in the unit test window
status.IsValid.ShouldBeTrue(status.GetAllErrors());

Changelog

[1.2.0] - 2020-04-12

Added

[1.1.1] - 2020-04-12

Forked from GenericServices.StatusGeneric

Product 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 was computed.  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. 
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
1.3.4 902 4/18/2022
1.3.1 661 4/18/2022
1.3.0 619 10/22/2021
1.2.3 593 4/13/2021
1.2.2 561 4/12/2021
1.2.1 571 4/12/2021
1.2.0 592 4/12/2021

- Updated to provide needed features for GenericBizRunner
     - Added some interfaces and made Message read/write