-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperationResult.cs
112 lines (101 loc) · 4.28 KB
/
OperationResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.ComponentModel.DataAnnotations;
namespace Deveel
{
/// <summary>
/// An operation result that has no value returned.
/// </summary>
public readonly struct OperationResult : IOperationResult
{
private OperationResult(OperationResultType resultType, IOperationError? error)
{
ResultType = resultType;
Error = error;
}
/// <inheritdoc/>
public OperationResultType ResultType { get; }
/// <inheritdoc/>
public IOperationError? Error { get; }
/// <summary>
/// The result of a successful operation.
/// </summary>
public static readonly OperationResult Success = new(OperationResultType.Success, null);
/// <summary>
/// The result of an operation that has not changed the state
/// of an object.
/// </summary>
public static readonly OperationResult NotChanged = new(OperationResultType.Unchanged, null);
/// <summary>
/// The result of an operation that has been cancelled.
/// </summary>
public static readonly OperationResult Cancelled = new(OperationResultType.Cancelled, null);
/// <summary>
/// Creates a new instance of an operation result that has failed.
/// </summary>
/// <param name="error">
/// The error that has caused the operation to fail.
/// </param>
/// <returns>
/// Returns an instance of <see cref="OperationResult"/> that represents
/// a failed operation.
/// </returns>
public static OperationResult Fail(IOperationError error)
{
ArgumentNullException.ThrowIfNull(error, nameof(error));
return new OperationResult(OperationResultType.Error, error);
}
/// <summary>
/// Creates a new instance of an operation result that has failed.
/// </summary>
/// <param name="code">
/// The code of the error that has caused the operation to fail.
/// </param>
/// <param name="domain">
/// The domain where the error has occurred.
/// </param>
/// <param name="message">
/// A message that describes the error.
/// </param>
/// <param name="inner">
/// A nested error that has caused the operation to fail.
/// </param>
/// <returns>
/// Returns an instance of <see cref="OperationResult"/> that represents
/// a failed operation.
/// </returns>
public static OperationResult Fail(string code, string domain, string? message = null, IOperationError? inner = null)
=> Fail(new OperationError(code, domain, message, inner));
/// <summary>
/// Creates a new instance of an operation result that has failed
/// because of a validation error.
/// </summary>
/// <param name="code">
/// The code of the error that has caused the operation to fail.
/// </param>
/// <param name="domain">
/// The domain where the error has occurred.
/// </param>
/// <param name="validationResults">
/// The list of validation results that have caused the operation to fail.
/// </param>
/// <returns>
/// Returns an instance of <see cref="OperationResult"/> that represents
/// a failed operation.
/// </returns>
public static OperationResult ValidationFailed(string code, string domain, IEnumerable<ValidationResult> validationResults)
=> Fail(new OperationValidationError(code, domain, validationResults.ToList()));
/// <summary>
/// Implicitly converts an instance of an error to an operation result.
/// </summary>
/// <param name="error">
/// The error that has caused the operation to fail.
/// </param>
public static implicit operator OperationResult(OperationError error) => Fail(error);
/// <summary>
/// Implicitly converts an instance of an operation error to an operation result.
/// </summary>
/// <param name="error">
/// The error that has caused the operation to fail.
/// </param>
public static implicit operator OperationResult(OperationException error) => Fail(error);
}
}