Table of Contents

Class Result<T>

Namespace
FoundationaLLM.Common.Models.Services
Assembly
FoundationaLLM.Common.dll

Represents the result of an operation that can succeed with a value of a specified reference type or fail with a domain error.

public class Result<T> where T : class

Type Parameters

T

The type of the value returned when the operation is successful. Must be a reference type.

Inheritance
Result<T>
Inherited Members
Extension Methods

Properties

Error

Gets the error associated with a failure result.

[JsonPropertyName("error")]
public DomainError? Error { get; }

Property Value

DomainError

IsFailure

Gets a value indicating whether the result represents a failure state.

[JsonIgnore]
public bool IsFailure { get; }

Property Value

bool

IsSuccess

Gets a value indicating whether the result refers to a successful outcome.

[JsonPropertyName("is_success")]
public bool IsSuccess { get; }

Property Value

bool

Value

Gets the value associated with the result.

[JsonPropertyName("value")]
public T? Value { get; }

Property Value

T

Methods

Failure(DomainError)

Creates a failed result with the specified domain error.

public static Result<T> Failure(DomainError error)

Parameters

error DomainError

The domain error that describes the reason for the failure. Cannot be null.

Returns

Result<T>

A failed result containing the specified domain error.

FailureFromErrorMessage(string, int, string?)

Creates a failed result with the specified error message.

public static Result<T> FailureFromErrorMessage(string errorMessage, int status = 500, string? instance = null)

Parameters

errorMessage string

The error message that describes the reason for the failure. Cannot be null or empty.

status int

The status code associated with the exception.

instance string

An optional instance identifier for the error.

Returns

Result<T>

A failed result containing the specified error message.

FailureFromException(Exception, int, string?)

Creates a failed result from the specified exception.

public static Result<T> FailureFromException(Exception exception, int status = 500, string? instance = null)

Parameters

exception Exception

The exception to convert into a failure result. Cannot be null.

status int

The status code associated with the exception.

instance string

An optional instance identifier for the error.

Returns

Result<T>

A failed result containing error information derived from the specified exception.

FailureFromHttpResponse(HttpResponseMessage)

Creates a failed result based on the specified HTTP response message.

public static Task<Result<T>> FailureFromHttpResponse(HttpResponseMessage response)

Parameters

response HttpResponseMessage

The HTTP response message from which to extract error information.

Returns

Task<Result<T>>

A task that represents the asynchronous operation. The task result contains a failed result with error details extracted from the HTTP response.

Success(T)

Creates a successful result containing the specified value.

public static Result<T> Success(T value)

Parameters

value T

The value to be encapsulated in the successful result.

Returns

Result<T>

A new Result<T> instance representing a successful outcome with the specified value.

ToActionResult()

Converts the current result to an appropriate ActionResult for use in ASP.NET Core MVC controllers.

public ActionResult ToActionResult()

Returns

ActionResult

An OkResult if the operation was successful; otherwise, an ObjectResult containing the ProblemDetails object with error information.

Remarks

Use this method to translate the result of an operation into a standardized HTTP response for controller actions. The returned result can be returned directly from an action method.

TryGetValue(out T)

Attempts to retrieve the stored value if the operation was successful.

public bool TryGetValue(out T value)

Parameters

value T

When this method returns, contains the value associated with a successful result; otherwise, the default value for the type T.

Returns

bool

true if the operation was successful and a value is available; otherwise, false.