-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperationResult.cs
More file actions
369 lines (324 loc) · 17.8 KB
/
OperationResult.cs
File metadata and controls
369 lines (324 loc) · 17.8 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
namespace OneBitSoftware.Utilities
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using OneBitSoftware.Utilities.Errors;
/// <summary>
/// A class for an operation result.
/// All errors will be logged if initialised with an instance of <see cref="ILogger"/>.
/// </summary>
public class OperationResult
{
/// <summary>
/// Contains <see cref="IOperationError"/> instances that have not been logged.
/// </summary>
private readonly List<(IOperationError Error, LogLevel? LogLevel)> _errorsNotLogged = new();
private readonly List<string> _successMessages = new();
private readonly ILogger? _logger;
/// <summary>
/// Gets or sets a value indicating whether the operation is successful or not.
/// </summary>
public bool Success => !this.Fail;
/// <summary>
/// Gets a value indicating whether the operation has failed.
/// </summary>
[JsonIgnore]
public bool Fail => this.Errors.Any();
/// <summary>
/// A collection of optional success messages that can be used to process positive operation result messages.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public IEnumerable<string>? SuccessMessages
{
get
{
return _successMessages.Any() ? _successMessages : null;
}
}
/// <summary>
/// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />.
/// </summary>
public List<IOperationError> Errors { get; internal set; } = new List<IOperationError>();
/// <summary>
/// Gets or sets the first exception that resulted from the operation.
/// </summary>
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Exception? InitialException { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class.
/// </summary>
/// <remarks>If the operation is a get operation, an empty result must return a truthy Success value.</remarks>
public OperationResult()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class.
/// </summary>
/// <param name="logger">An instance of <see cref="ILogger"/> to use for automatic logging.</param>
/// <remarks>If the operation is a get operation, an empty result (no results) must return a truthy Success value.</remarks>
public OperationResult(ILogger? logger)
{
if (logger != null)
{
this._logger = logger;
}
}
/// <summary>
/// Adds a success message to the internal collection.
/// </summary>
/// <param name="message">The message to add.</param>
public void AddSuccessMessage(string message)
{
if (!string.IsNullOrWhiteSpace(message))
{
this._successMessages.Add(message);
}
}
/// <summary>
/// Appends all errors from another <see cref="OperationResult"/> to the current <see cref="OperationResult"/>.
/// </summary>
/// <param name="otherOperationResult">The <see cref="OperationResult"/> to append errors from.</param>
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
public OperationResult AppendErrors(OperationResult otherOperationResult)
{
if (otherOperationResult is null) return this;
// store any messages for logging at a later stage, when merged to an OperationResult with a logger.
if (this._logger is null)
{
this._errorsNotLogged.AddRange(otherOperationResult._errorsNotLogged);
}
else
{
foreach (var (error, logLevel) in otherOperationResult._errorsNotLogged)
this.LogInternal(error, logLevel);
otherOperationResult._errorsNotLogged.Clear();
}
// Append the error message without logging (presuming that there is already a log message).
foreach (var error in otherOperationResult.Errors) this.AppendErrorInternal(error);
return this;
}
/// <summary>
/// This method will append an <see cref="OperationError"/> error with a specific `user-friendly` message to this operation result instance.
/// </summary>
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
/// <param name="code">The unique code of the error.</param>
/// <param name="logLevel">The logging severity.</param>
/// <param name="details">A <see cref="string"/> with error details.</param>
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
public OperationResult AppendError(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
{
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentNullException(nameof(message));
var error = new OperationError(message, code) { Details = details };
this.AppendError(error, logLevel);
return this;
}
/// <summary>
/// This method will append an <typeparamref name="T"/> error with a specific `user-friendly` message to this operation result instance.
/// </summary>
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
/// <param name="code">The unique code of the error.</param>
/// <param name="logLevel">The logging severity.</param>
/// <param name="details">A <see cref="string"/> with error details.</param>
/// <typeparam name="T">The type of <see cref="IOperationError"/> to append.</typeparam>
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
public OperationResult AppendError<T>(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
where T : IOperationError, new()
{
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentNullException(nameof(message));
var error = new T() { Message = message, Code = code, Details = details };
this.AppendError(error, logLevel);
return this;
}
/// <summary>
/// Appends an <see cref="IOperationError"/> to the internal errors collection.
/// </summary>
/// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
/// <param name="logLevel">The logging level.</param>
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
public OperationResult AppendError(IOperationError error, LogLevel? logLevel = LogLevel.Error)
{
this.AppendErrorInternal(error);
this.LogInternal(error, logLevel);
return this;
}
/// <summary>
/// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogLevel"/> level. A call to this method will set the Success property to false.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="errorCode">The error code.</param>
/// <param name="logLevel">The <see cref="LogLevel"/> logging severity.</param>
/// <returns>The current instance of the <see cref="OperationResult"/>.</returns>
public OperationResult AppendException(Exception exception, int errorCode = 0, LogLevel? logLevel = null)
{
if (exception is null) throw new ArgumentNullException(nameof(exception));
// Append the exception as a first if it is not yet set.
this.InitialException ??= exception;
var error = new OperationError(exception.ToString(), errorCode);
this.AppendError(error, logLevel);
return this;
}
/// <summary>
/// Use this method to get a string with all error messages.
/// </summary>
/// <returns>All error messages, joined with a new line character.</returns>
public override string ToString() => string.Join(Environment.NewLine, this.Errors);
/// <summary>
/// Creates an instance of <see cref="OperationResult"/> and appends the passed exception to it's error collection.
/// </summary>
/// <param name="exception">The <see cref="Exception"/> to append.</param>
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
/// <returns>An <see cref="OperationResult"/> containing the passed exception.</returns>
public static OperationResult FromException(Exception exception, ILogger? logger = null)
{
var result = new OperationResult(logger);
return result.AppendException(exception);
}
/// <summary>
/// Creates an instance of <see cref="OperationResult"/> and appends the passed error message details to it's internal error collection.
/// </summary>
/// <param name="message">A message to append to the internal errors collection.</param>
/// <param name="code">An optional code to include in the error.</param>
/// <param name="logLevel">A log event level. Defaults to Error.</param>
/// <param name="details">An optional detail message to add to the error.</param>
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
/// <returns>An <see cref="OperationResult"/> containing the passed exception.</returns>
public static OperationResult FromError(string message, int? code = null, LogLevel logLevel = LogLevel.Error, string? details = null, ILogger? logger = null)
{
var result = new OperationResult(logger);
return result.AppendError(message, code, logLevel, details);
}
/// <summary>
/// Appends an <see cref="IOperationError"/> to the internal errors collection.
/// </summary>
/// <param name="error">An instance of <see cref="IOperationError"/> to add to the internal errors collection.</param>
protected void AppendErrorInternal(IOperationError error) => this.Errors.Add(error);
/// <summary>
/// Logs to the internal logger if it is set, otherwise it will add the error to the internal errors collection.
/// </summary>
/// <param name="error">The <see cref="IOperationError"/> to log.</param>
/// <param name="logLevel">The log level.</param>
private void LogInternal(IOperationError error, LogLevel? logLevel)
{
if (this._logger is null)
{
this._errorsNotLogged.Add((Error: error, LogLevel: logLevel));
}
else
{
this._logger.Log(logLevel ?? LogLevel.Error, error.Message);
}
}
}
/// <summary>
/// A class for a system operation result with a generic result object type.
/// </summary>
/// <typeparam name="TResult">The type of object that will be returned.</typeparam>
public class OperationResult<TResult> : OperationResult
{
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class.
/// </summary>
/// <remarks>If the operation is a get operation, an empty result must return a truthy Success value.</remarks>
public OperationResult()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class.
/// </summary>
/// <param name="logger">An instance of <see cref="ILoggerService"/>.</param>
/// <remarks>If the operation is a get operation, an empty result must return a truthy Success value.</remarks>
public OperationResult(ILogger? logger) : base(logger)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class and sets the passed result object. Internally, this will set the Success result to True.
/// </summary>
/// <param name="resultObject">An initial failure message for the operation result. This will fail the success status.</param>
/// <param name="logger">An instance of <see cref="ILogger"/>.</param>
/// <remarks>If the operation is a get operation, an empty result must return a truthy Success value.</remarks>
public OperationResult(TResult resultObject, ILogger? logger) : base(logger)
{
this.ResultObject = resultObject;
}
/// <summary>
/// Initializes a new instance of the <see cref="OperationResult"/> class and sets the passed result object. Internally, this will set the Success result to True.
/// </summary>
/// <param name="resultObject">An initial failure message for the operation result. This will fail the success status.</param>
/// <remarks>If the operation is a get operation, an empty result must return a truthy Success value.</remarks>
public OperationResult(TResult resultObject) : base()
{
this.ResultObject = resultObject;
}
/// <summary>
/// Gets or sets the related result object of the operation.
/// </summary>
public TResult? ResultObject { get; set; }
/// <summary>
/// This method will append an error with a specific `user-friendly` message to this operation result instance.
/// </summary>
/// <param name="message">A label consuming component defining the 'user-friendly' message.</param>
/// <param name="code">The unique code of the error.</param>
/// <param name="logLevel">The logging severity.</param>
/// <param name="details">A <see cref="string"/> with error details.</param>
/// <returns>The current instance of the <see cref="OperationResult{TResult}"/>.</returns>
public new OperationResult<TResult> AppendError(string message, int? code = null, LogLevel? logLevel = null, string? details = null)
{
base.AppendError(message, code, logLevel, details);
return this;
}
/// <summary>
/// Appends an exception to the error message collection and logs the full exception as an Error <see cref="LogEventLevel"/> level. A call to this method will set the Success property to false.
/// </summary>
/// <param name="exception">The exception to log.</param>
/// <param name="errorCode">The error code.</param>
/// <param name="logLevel">The <see cref="LogEventLevel"/> logging severity.</param>
/// <returns>The current instance of the <see cref="OperationResult{TResult}"/>.</returns>
public new OperationResult<TResult> AppendException(Exception exception, int errorCode = 0, LogLevel? logLevel = null)
{
base.AppendException(exception, errorCode, logLevel);
return this;
}
/// <summary>
/// Creates an instance of <see cref="OperationResult{TResult}"/> and appends the passed error message details to it's internal error collection.
/// </summary>
/// <param name="message">A message to append to the internal errors collection.</param>
/// <param name="code">An optional code to include in the error.</param>
/// <param name="logLevel">A log event level. Defaults to Error.</param>
/// <param name="details">An optional detail message to add to the error.</param>
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
/// <returns>An <see cref="OperationResult{TResult}"/> containing the passed exception.</returns>
public static new OperationResult<TResult> FromError(string message, int? code = null, LogLevel logLevel = LogLevel.Error, string? details = null, ILogger? logger = null)
{
var result = new OperationResult<TResult>(logger);
return result.AppendError(message, code, logLevel, details);
}
/// <summary>
/// Creates an instance of <see cref="OperationResult"/> and appends the passed exception to it's error collection.
/// </summary>
/// <param name="exception">The <see cref="Exception"/> to append.</param>
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
/// <returns>An <see cref="OperationResult{TResult}"/> containing the passed exception.</returns>
public static new OperationResult<TResult> FromException(Exception exception, ILogger? logger = null)
{
var result = new OperationResult<TResult>(logger);
return result.AppendException(exception);
}
/// <summary>
/// Sets related object to the provided operation result and returns itself.
/// </summary>
/// <param name="operationResult">The <see cref="OperationResult{TResult}"/> instance to extend.</param>
/// <param name="relatedObject">The value to set to the <see cref="OperationResult{TResult}.ResultObject"/> property.</param>
/// <returns>Returns the original <paramref name="operationResult"/> with the appended message.</returns>
/// <remarks>
/// This method will throw if the provided <paramref name="operationResult"/> is null.
/// </remarks>
public OperationResult<TResult> WithRelatedObject(TResult relatedObject)
{
this.ResultObject = relatedObject;
return this;
}
}
}