MgResultPattern 1.0.0

dotnet add package MgResultPattern --version 1.0.0
                    
NuGet\Install-Package MgResultPattern -Version 1.0.0
                    
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="MgResultPattern" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MgResultPattern" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MgResultPattern" />
                    
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 MgResultPattern --version 1.0.0
                    
#r "nuget: MgResultPattern, 1.0.0"
                    
#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 MgResultPattern@1.0.0
                    
#: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=MgResultPattern&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MgResultPattern&version=1.0.0
                    
Install as a Cake Tool

MgResultPattern

A lightweight Result pattern library for .NET built on top of Dunet discriminated unions. It provides two generic union types — MgResult<T> and MgResults<T> — to represent the outcome of an operation without throwing exceptions.

Overview

Type Success case Failure case
MgResult<T> MgResult<T>.Success(T Value) MgResult<T>.Failure(FailureResponse Value)
MgResults<T> MgResults<T>.Success(T Value) MgResults<T>.Failure(List<FailureResponse> Values)

FailureResponse carries a human-readable Message and an HTTP-style Code (defaults to 400).

Use MgResult<T> when an operation produces at most one error, and MgResults<T> when it can produce multiple errors (e.g. validation).

Installation

Add the project or package reference to your solution. The only dependency is Dunet (≥ 1.11.3) targeting .NET 10.

How to Use

Creating results

// Success
MgResult<int> ok = new MgResult<int>.Success(42);

// Failure with default code (400)
MgResult<int> fail = new MgResult<int>.Failure(new FailureResponse("Something went wrong"));

// Failure with explicit code
MgResult<int> serverError = new MgResult<int>.Failure(new FailureResponse("Unexpected error", 500));

Checking the outcome

if (result.IsSuccess())
{
    var value = result.GetSuccessValue();
}

if (result.IsFailure())
{
    var error = result.GetFailureValue(); // FailureResponse
    Console.WriteLine($"[{error.Code}] {error.Message}");
}

Pattern matching with Match

Match is the safest way to handle both cases exhaustively:

var message = result.Match(
    success => $"Got value: {success.Value}",
    failure => $"Error {failure.Value.Code}: {failure.Value.Message}"
);

Multiple failures with MgResults<T>

// Success
MgResults<string> ok = new MgResults<string>.Success("done");

// Multiple failures (e.g. validation)
var errors = new List<FailureResponse>
{
    new FailureResponse("Name is required", 422),
    new FailureResponse("Email is invalid", 422)
};
MgResults<string> fail = new MgResults<string>.Failure(errors);

if (fail.IsFailure())
{
    foreach (var error in fail.GetFailureValues())
        Console.WriteLine($"[{error.Code}] {error.Message}");
}

Returning results from a service

public MgResult<User> GetUser(int id)
{
    var user = _repository.Find(id);
    if (user is null)
        return new MgResult<User>.Failure(new FailureResponse("User not found", 404));

    return new MgResult<User>.Success(user);
}

Extension Methods

Both types expose the following helpers via C# extension methods:

Method Description
IsSuccess() Returns true when the result is a success
IsFailure() Returns true when the result is a failure
GetSuccessValue() Returns the success value or default
GetFailureValue() (MgResult only) Returns the single FailureResponse
GetFailureValues() (MgResults only) Returns the List<FailureResponse>

Running Tests

dotnet test

Tests are written with xUnit and Shouldly and cover success/failure construction, IsSuccess/IsFailure guards, value retrieval, and Match exhaustiveness.

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

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.0.0 99 5/21/2026