-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Result instead of tuple where applicable (#408)
* Using Result instead of tuple where applicable * fixed code smell * removed result as return for orderrequestservice
- Loading branch information
Showing
18 changed files
with
235 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace Altinn.Notifications.Core.Shared; | ||
|
||
/// <summary> | ||
/// A simple implementation of the Result class to handle success XOR failure as separate return | ||
/// values in a type safe way. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type to be assigned to indicate success.</typeparam> | ||
/// <typeparam name="TError">The type to be assigned to indicate failure.</typeparam> | ||
public readonly struct Result<TValue, TError> | ||
{ | ||
private readonly TValue? _value; | ||
private readonly TError? _error; | ||
|
||
private Result(TValue value) | ||
{ | ||
IsError = false; | ||
_value = value; | ||
_error = default; | ||
} | ||
|
||
private Result(TError error) | ||
{ | ||
IsError = true; | ||
_value = default; | ||
_error = error; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the Result contains an error value. | ||
/// </summary> | ||
public bool IsError { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the Result contains a success value. | ||
/// </summary> | ||
public bool IsSuccess => !IsError; | ||
|
||
/// <summary> | ||
/// Implicit operator used when creating an instance of Result when assigning a success value. | ||
/// </summary> | ||
/// <param name="value">An object of the type indicating success.</param> | ||
public static implicit operator Result<TValue, TError>(TValue value) => new(value); | ||
|
||
/// <summary> | ||
/// Implicit operator used when creating an instance of Result when assigning an error value. | ||
/// </summary> | ||
/// <param name="error">An object of the type indicating failure.</param> | ||
public static implicit operator Result<TValue, TError>(TError error) => new(error); | ||
|
||
/// <summary> | ||
/// This method will call either the success OR the failure function based on it's error state. | ||
/// </summary> | ||
/// <typeparam name="TResult">The type to be returned by the given functions.</typeparam> | ||
/// <param name="success">The function to call if Result holds a success value.</param> | ||
/// <param name="failure">The function to call if Result holds an error value.</param> | ||
/// <returns>An instance of the defined type.</returns> | ||
public TResult Match<TResult>( | ||
Func<TValue, TResult> success, | ||
Func<TError, TResult> failure) => | ||
!IsError ? success(_value!) : failure(_error!); | ||
} |
2 changes: 1 addition & 1 deletion
2
...Notifications.Core/Models/ServiceError.cs → ...Notifications.Core/Shared/ServiceError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.