diff --git a/CHANGELOG.md b/CHANGELOG.md index d2d3881a..f738dfc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ Represents the **NuGet** versions. +## v3.23.4 +- *Fixed:* Added `Result.AdjustsAsync` to support asynchronous adjustments. + ## 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. diff --git a/Common.targets b/Common.targets index 65741807..2b4c81c6 100644 --- a/Common.targets +++ b/Common.targets @@ -1,6 +1,6 @@  - 3.23.3 + 3.23.4 preview Avanade Avanade diff --git a/src/CoreEx/Results/ResultsExtensions.cs b/src/CoreEx/Results/ResultsExtensions.cs index 4ae9bed5..c1af2dec 100644 --- a/src/CoreEx/Results/ResultsExtensions.cs +++ b/src/CoreEx/Results/ResultsExtensions.cs @@ -98,6 +98,47 @@ public static Result Adjusts(this Result result, Action adjuster) return 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 async Task> Adjusts(this Task> result, Action adjuster) + { + var r = await result.ConfigureAwait(false); + return r.Adjusts(adjuster); + } + + /// + /// 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 async Task> AdjustsAsync(this Result result, Func adjuster) + { + if (result.IsSuccess && result.Value is not null) + await adjuster(result.Value).ConfigureAwait(false); + + return 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 async Task> AdjustsAsync(this Task> result, Func adjuster) + { + var r = await result.ConfigureAwait(false); + return await r.AdjustsAsync(adjuster).ConfigureAwait(false); + } + /// /// 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 57a8d894..d34f7e32 100644 --- a/tests/CoreEx.Test/Framework/Results/ResultTTest.cs +++ b/tests/CoreEx.Test/Framework/Results/ResultTTest.cs @@ -249,6 +249,64 @@ public void Adjusts() Assert.That(r2.IsSuccess, Is.False); } + [Test] + public async Task AdjustsAsync() + { + var r = Result.Ok(new Person()); + var r2 = await r.AdjustsAsync(async v => + { + await Task.CompletedTask; + v.Id = 2; + }); + + Assert.Multiple(() => + { + Assert.That(r2.IsSuccess, Is.True); + Assert.That(r2.Value.Id, Is.EqualTo(2)); + }); + } + + [Test] + public async Task Adjusts2Async() + { + var r = Result.GoAsync(async () => + { + await Task.CompletedTask; + return new Person(); + }); + + var r2 = await r.AdjustsAsync(async v => + { + await Task.CompletedTask; + v.Id = 2; + }); + + Assert.Multiple(() => + { + Assert.That(r2.IsSuccess, Is.True); + Assert.That(r2.Value.Id, Is.EqualTo(2)); + }); + } + + [Test] + public async Task Adjusts2AsyncTightLoop() + { + for (int i = 0; i < 10000; i++) + { + var r = Result.GoAsync(async () => + { + await Task.CompletedTask; + return new Person(); + }); + + var r2 = await r.AdjustsAsync(async v => + { + await Task.CompletedTask; + v.Id = 2; + }); + } + } + public class Person { public int Id { get; set; }