NucleusResults 1.1.2

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

NucleusResults

Purpose

This pattern serves as an alternative to throwing exceptions for handling expected or non-critical errors, reducing unnecessary CPU overhead. Instead of relying on exceptions for flow control, NucleusResult<T> allows you to communicate results (data, messages, and exceptions) in a more structured and efficient way.

Key Benefits

  1. Improved Performance: Throwing exceptions incurs a CPU cost, particularly in high-throughput applications where exceptions can degrade performance. NucleusResult<T> avoids this by providing a lightweight way to signal success or failure.
  2. Enhanced Readability and Maintainability: Using NucleusResult<T> makes code flow more intuitive, as it’s clear when an operation succeeded or failed. Methods return a result directly, making it easier for developers to handle the outcome in a structured manner and avoiding null checks.
  3. Better User and Developer Experience: This pattern provides meaningful error messages and structured results, giving both end-users and upstream services clear feedback on what went wrong.

Usage

Success Case

public NucleusResult<Game> CreateGame()
{
	var game = new Game();
	return game;
}

Failure Case

public NucleusResult<Game> CreateGame()
{
	try { ... }
	catch(Exception ex)
	{
		var error = new Error(ex, "Failed to create game");
		return error;
	}
}

Handling Results

Direct return, can be Error or Success

public NucleusResult<SomeOtherObject> OuterMethod()
{
	var result = InnerMethod();
	return result;
}

When called from outer class

public NucleusResult<SomeOtherObject> OuterMethod()
{
	var result = InnerMethod();
	if (result.IsError)
		return result.Error;
	
	var game = result.UnWrap();
	...
	(Do something more)
	...
}

When called from controller

public IActionResult CreateGame()
{
	var result = CreateGame();
	return result.Resolve(
		success => Ok(result.Data),
		error => BadRequest(result.Message));
}

Cases where the type T needs to change**

public NucleusResult<Alfa> DoSomething()
{
	NucleusResult<Beta> result = InnerMethod();
	if (result.IsError)
		return result.Error;
	
	return result.ToType<Beta, Alfa>();	
}
public NucleusResult DoSomething()
{
	NucleusResult<Beta> result = InnerMethod();
	if (result.IsError)
		return result.Error;
	
	return result.RemoveType();	
}
public NucleusResult<Alfa> DoSomething()
{
	NucleusResult result = InnerMethod();
	if (result.IsError)
		return result.Error;
	
	return result.AddType<Alfa>();	
}
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.  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. 
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.1.2 105 3 months ago
1.1.1 107 4 months ago
1.1.0 93 4 months ago
1.0.0 91 4 months ago