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" />
<PackageReference Include="NucleusResults" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=NucleusResults&version=1.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
- 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.
- 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.
- 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 | Versions 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.
-
net8.0
- Microsoft.AspNet.Mvc (>= 5.3.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.