OperationResult
is a nice way to return success error messages out of service classes.
Last active
December 6, 2021 16:36
-
-
Save Eonasdan/fb09f382c40b513aae41aea8e5096b06 to your computer and use it in GitHub Desktop.
OperationResult
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
using System.Collections.Generic; | |
public interface IOperationResult | |
{ | |
public bool Succeeded { get; } | |
public IEnumerable<OperationError> Errors { get; } | |
} | |
public interface IOperationResult<out T>: IOperationResult | |
{ | |
public T Value { get; } | |
} |
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
using System.Collections.Generic; | |
public class OperationResult: IOperationResult | |
{ | |
public bool Succeeded { get; init; } | |
public IEnumerable<OperationError> Errors { get; init; } = new List<OperationError>(); | |
public static OperationResult Success { get; } = new() { Succeeded = true }; | |
public static OperationResult<T> SucceededWithValue<T>(T value) | |
{ | |
return new() | |
{ | |
Succeeded = true, | |
Value = value | |
}; | |
} | |
public static OperationResult Failed(params OperationError[] errors) | |
{ | |
var result = new OperationResult | |
{ | |
Succeeded = false, | |
Errors = errors ?? Array.Empty<OperationError>() | |
}; | |
return result; | |
} | |
public static OperationResult Failed(IEnumerable<OperationError> errors) | |
{ | |
var result = new OperationResult | |
{ | |
Succeeded = false, | |
Errors = errors ?? Array.Empty<OperationError>() | |
}; | |
return result; | |
} | |
public static OperationResult<T> Failed<T>(params OperationError[] errors) | |
{ | |
var result = new OperationResult<T> | |
{ | |
Succeeded = false, | |
Errors = errors ?? Array.Empty<OperationError>() | |
}; | |
return result; | |
} | |
public static OperationResult<T> Failed<T>(IEnumerable<OperationError> errors) | |
{ | |
var result = new OperationResult<T> | |
{ | |
Succeeded = false, | |
Errors = errors ?? Array.Empty<OperationError>() | |
}; | |
return result; | |
} | |
} | |
public class OperationResult<T> : OperationResult, IOperationResult<T> | |
{ | |
public T Value { get; init; } | |
} | |
public class OperationError | |
{ | |
public OperationError(string description) | |
{ | |
Description = description; | |
} | |
/// <summary> | |
/// Gets or sets the description for this error. | |
/// </summary> | |
/// <value> | |
/// The description for this error. | |
/// </value> | |
public string Description { get; set; } | |
public static implicit operator OperationError(string description) | |
{ | |
return new(description); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment