Class LayerResponse<TResult>
The result of a fallible operation that returns a value: success with Result, or failure with
Errors. return value;, return error; and return ex; all convert implicitly.
Inherited Members
Namespace: ZhEaIsNsAaBn.Utilities
Assembly: ZhEaIsNsAaBn.LayerResponse.dll
Syntax
public class LayerResponse<TResult> : LayerResponse
Type Parameters
| Name | Description |
|---|---|
| TResult | The value type. |
Constructors
LayerResponse(bool, List<Error>?, TResult?)
Creates a response from a list of errors.
Declaration
protected LayerResponse(bool isSuccess, List<Error>? errors, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | isSuccess | Whether the operation succeeded. |
| List<Error> | errors | Empty or null on success; at least one real error on failure. |
| TResult | result | The value, if any. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Success with errors, or failure with no real error. |
LayerResponse(bool, Error, TResult?)
Creates a response from one error.
Declaration
protected LayerResponse(bool isSuccess, Error error, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | isSuccess | Whether the operation succeeded. |
| Error | error | None on success; a real error on failure. |
| TResult | result | The value, if any. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Success with a real error, or failure with None. |
Properties
IsFailureOrEmpty
Whether the operation failed, or succeeded with a default value or an empty collection.
Declaration
public bool IsFailureOrEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Result
The value. default on failure unless one was supplied.
Declaration
public TResult? Result { get; }
Property Value
| Type | Description |
|---|---|
| TResult |
Methods
Failure(List<Error>, TResult?)
Creates a failed response.
Declaration
public static LayerResponse<TResult> Failure(List<Error> errors, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| List<Error> | errors | The errors; at least one. |
| TResult | result | An optional value to return with the failure. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> | A failed response. |
Failure(Error, TResult?)
Creates a failed response.
Declaration
public static LayerResponse<TResult> Failure(Error error, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Error | error | The error. |
| TResult | result | An optional value to return with the failure. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> | A failed response. |
Failure(LayerResponse, TResult?)
Creates a failed response with the errors of another response.
Declaration
public static LayerResponse<TResult> Failure(LayerResponse layerResponse, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse | layerResponse | The failed response to copy errors from. |
| TResult | result | An optional value to return with the failure. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> | A failed response. |
Failure<TOtherResult>(LayerResponse<TOtherResult>, TResult?)
Creates a failed response with the errors of another typed response.
Declaration
public static LayerResponse<TResult> Failure<TOtherResult>(LayerResponse<TOtherResult> layerResponse, TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse<TOtherResult> | layerResponse | The failed response to copy errors from. |
| TResult | result | An optional value to return with the failure. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> | A failed response. |
Type Parameters
| Name | Description |
|---|---|
| TOtherResult | The value type of the source response. |
Success(TResult?)
Creates a successful response.
Declaration
public static LayerResponse<TResult> Success(TResult? result = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TResult | result | The value. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> | A successful response. |
ToLayerResponse(LayerResponse)
Copies the outcome into a plain LayerResponse.
Declaration
public static LayerResponse ToLayerResponse(LayerResponse result)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse | result | The response to copy. |
Returns
| Type | Description |
|---|---|
| LayerResponse | Success, or failure with the same errors. |
ToLayerResponse<TResult2>(LayerResponse)
Copies the outcome into a LayerResponse<TResult> of another type, with no value.
Declaration
public static LayerResponse<TResult2> ToLayerResponse<TResult2>(LayerResponse result)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse | result | The response to copy. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult2> | Success, or failure with the same errors. |
Type Parameters
| Name | Description |
|---|---|
| TResult2 | The value type of the new response. |
Operators
implicit operator LayerResponse<TResult>(List<Error>)
Wraps errors as a failed response.
Declaration
public static implicit operator LayerResponse<TResult>(List<Error> errors)
Parameters
| Type | Name | Description |
|---|---|---|
| List<Error> | errors | The errors; at least one. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> |
implicit operator LayerResponse<TResult>(Exception)
Wraps an exception as a failed response with Unknown(Exception, IReadOnlyDictionary<string, object>?).
Declaration
public static implicit operator LayerResponse<TResult>(Exception exception)
Parameters
| Type | Name | Description |
|---|---|---|
| Exception | exception | The exception. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> |
implicit operator LayerResponse<TResult>(Error)
Wraps an error as a failed response.
Declaration
public static implicit operator LayerResponse<TResult>(Error error)
Parameters
| Type | Name | Description |
|---|---|---|
| Error | error | The error. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> |
implicit operator List<Error>(LayerResponse<TResult>)
Gets the errors.
Declaration
public static implicit operator List<Error>(LayerResponse<TResult> response)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse<TResult> | response | The response. |
Returns
| Type | Description |
|---|---|
| List<Error> |
implicit operator Error(LayerResponse<TResult>)
Gets the first error, or None on success.
Declaration
public static implicit operator Error(LayerResponse<TResult> response)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse<TResult> | response | The response. |
Returns
| Type | Description |
|---|---|
| Error |
implicit operator TResult(LayerResponse<TResult>)
Gets the value.
Declaration
public static implicit operator TResult(LayerResponse<TResult> result)
Parameters
| Type | Name | Description |
|---|---|---|
| LayerResponse<TResult> | result | The response. |
Returns
| Type | Description |
|---|---|
| TResult |
implicit operator LayerResponse<TResult>(TResult?)
Wraps a value as a successful response.
Declaration
public static implicit operator LayerResponse<TResult>(TResult? result)
Parameters
| Type | Name | Description |
|---|---|---|
| TResult | result | The value. |
Returns
| Type | Description |
|---|---|
| LayerResponse<TResult> |