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
TThe 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
IsFailure
Gets a value indicating whether the result represents a failure state.
[JsonIgnore]
public bool IsFailure { get; }
Property Value
IsSuccess
Gets a value indicating whether the result refers to a successful outcome.
[JsonPropertyName("is_success")]
public bool IsSuccess { get; }
Property Value
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
errorDomainErrorThe 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
errorMessagestringThe error message that describes the reason for the failure. Cannot be null or empty.
statusintThe status code associated with the exception.
instancestringAn 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
exceptionExceptionThe exception to convert into a failure result. Cannot be null.
statusintThe status code associated with the exception.
instancestringAn 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
responseHttpResponseMessageThe 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
valueTThe value to be encapsulated in the successful result.
Returns
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
valueTWhen 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.