Skip to content

Commit d7ee5df

Browse files
Merge pull request #2 from OneBitSoftware/backup/main-14-08-54
Added generic From* methods and made ResultObject nullable
2 parents fdbd96f + 9e92a0b commit d7ee5df

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/OneBitSoftware.Utilities.OperationResult/OneBitSoftware.Utilities.OperationResult.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
3333
<PackageReadmeFile>README.md</PackageReadmeFile>
3434
<PackageTags>OneBitSoftware; OperationResult;</PackageTags>
35-
<Version>1.4.0</Version>
35+
<Version>1.4.1</Version>
3636
</PropertyGroup>
3737

3838
</Project>

src/OneBitSoftware.Utilities.OperationResult/OperationResult.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public IEnumerable<string>? SuccessMessages
4444
/// Gets an <see cref="List{T}"/> containing the error codes and messages of the <see cref="OperationResult{T}" />.
4545
/// </summary>
4646
public List<IOperationError> Errors { get; internal set; } = new List<IOperationError>();
47-
47+
4848
/// <summary>
4949
/// Gets or sets the first exception that resulted from the operation.
5050
/// </summary>
@@ -267,7 +267,7 @@ public OperationResult(TResult resultObject) : base()
267267
/// <summary>
268268
/// Gets or sets the related result object of the operation.
269269
/// </summary>
270-
public TResult ResultObject { get; set; }
270+
public TResult? ResultObject { get; set; }
271271

272272
/// <summary>
273273
/// This method will append an error with a specific `user-friendly` message to this operation result instance.
@@ -334,6 +334,33 @@ public OperationResult<TResult> AppendErrorMessages<TOther>(TOther otherOperatio
334334
return this;
335335
}
336336

337+
/// <summary>
338+
/// Creates an instance of <see cref="OperationResult{TResult}"/> and appends the passed error message details to it's internal error collection.
339+
/// </summary>
340+
/// <param name="message">A message to append to the internal errors collection.</param>
341+
/// <param name="code">An optional code to include in the error.</param>
342+
/// <param name="logLevel">A log event level. Defaults to Error.</param>
343+
/// <param name="details">An optional detail message to add to the error.</param>
344+
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
345+
/// <returns>An <see cref="OperationResult{TResult}"/> containing the passed exception.</returns>
346+
public static new OperationResult<TResult> FromError(string message, int? code = null, LogLevel logLevel = LogLevel.Error, string? details = null, ILogger? logger = null)
347+
{
348+
var result = new OperationResult<TResult>(logger);
349+
return result.AppendError(message, code, logLevel, details);
350+
}
351+
352+
/// <summary>
353+
/// Creates an instance of <see cref="OperationResult"/> and appends the passed exception to it's error collection.
354+
/// </summary>
355+
/// <param name="exception">The <see cref="Exception"/> to append.</param>
356+
/// <param name="logger">An optional instance of <see cref="ILogger"/>.</param>
357+
/// <returns>An <see cref="OperationResult{TResult}"/> containing the passed exception.</returns>
358+
public static new OperationResult<TResult> FromException(Exception exception, ILogger? logger = null)
359+
{
360+
var result = new OperationResult<TResult>(logger);
361+
return result.AppendException(exception);
362+
}
363+
337364
/// <summary>
338365
/// Sets related object to the provided operation result and returns itself.
339366
/// </summary>

0 commit comments

Comments
 (0)