diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ba4c409..d2d3881a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v3.23.3 +- *Fixed:* Added `Result.Adjusts` as wrapper for `ObjectExtensions.Adjust` to simplify support and resolve issue where the compiler sees the adjustment otherwise as a implicit cast resulting in an errant outcome. + ## v3.23.2 - *Fixed:* `DatabaseExtendedExtensions.DeleteWithResultAsync` corrected to return a `Task`.` diff --git a/Common.targets b/Common.targets index 768de54e..65741807 100644 --- a/Common.targets +++ b/Common.targets @@ -1,6 +1,6 @@  - 3.23.2 + 3.23.3 preview Avanade Avanade diff --git a/src/CoreEx/Results/ResultsExtensions.cs b/src/CoreEx/Results/ResultsExtensions.cs index 1114b0c4..4ae9bed5 100644 --- a/src/CoreEx/Results/ResultsExtensions.cs +++ b/src/CoreEx/Results/ResultsExtensions.cs @@ -83,6 +83,21 @@ public static async Task> AsResult(this Task> result) public static Result ThrowIfNull(this Result result, string? name = null) => result.IsSuccess && result.Value == null ? throw new ArgumentNullException(name ?? Validation.Validation.ValueNameDefault) : result; + /// + /// Enables adjustment (changes) to a via an action where the is + /// + /// The . + /// The . + /// The adjusting action (invoked only where the underlying is not null). + /// The resulting . + public static Result Adjusts(this Result result, Action adjuster) + { + if (result.IsSuccess) + result.Value.Adjust(adjuster); + + return result; + } + /// /// Checks whether the user has the required (see ). /// diff --git a/tests/CoreEx.Test/Framework/Results/ResultTTest.cs b/tests/CoreEx.Test/Framework/Results/ResultTTest.cs index 6e43d48d..57a8d894 100644 --- a/tests/CoreEx.Test/Framework/Results/ResultTTest.cs +++ b/tests/CoreEx.Test/Framework/Results/ResultTTest.cs @@ -226,5 +226,32 @@ public void Failure_Value() var ir = (IResult)Result.Fail("On no!"); Assert.Throws(() => _ = ir.Value); } + + [Test] + public void Adjusts() + { + var r = Result.Ok(new Person()); + Assert.Multiple(() => + { + Assert.That(r.IsSuccess, Is.True); + Assert.That(r.Value.Id, Is.EqualTo(0)); + }); + + var r2 = r.Adjusts(v => v.Id = 2); + Assert.Multiple(() => + { + Assert.That(r2.IsSuccess, Is.True); + Assert.That(r2.Value.Id, Is.EqualTo(2)); + }); + + r = Result.Fail(new BusinessException()); + r2 = r.Adjusts(v => v.Id = 2); + Assert.That(r2.IsSuccess, Is.False); + } + + public class Person + { + public int Id { get; set; } + } } } \ No newline at end of file