Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.23.3 #112

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v3.23.3
- *Fixed:* Added `Result<T>.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<Result>`.`

Expand Down
2 changes: 1 addition & 1 deletion Common.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.23.2</Version>
<Version>3.23.3</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
Expand Down
15 changes: 15 additions & 0 deletions src/CoreEx/Results/ResultsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public static async Task<Result<U>> AsResult<T, U>(this Task<Result<T>> result)
public static Result<T> ThrowIfNull<T>(this Result<T> result, string? name = null)
=> result.IsSuccess && result.Value == null ? throw new ArgumentNullException(name ?? Validation.Validation.ValueNameDefault) : result;

/// <summary>
/// Enables adjustment (changes) to a <see cref="Result{T}.Value"/> via an <paramref name="adjuster"/> action where the <paramref name="result"/> is <see cref="Result{T}.IsSuccess"/>
/// </summary>
/// <typeparam name="T">The <see cref="Result{T}.Value"/> <see cref="Type"/>.</typeparam>
/// <param name="result">The <see cref="Result{T}"/>.</param>
/// <param name="adjuster">The adjusting action (invoked only where the underlying <see cref="Result{T}.Value"/> is not <c>null</c>).</param>
/// <returns>The resulting <see cref="Result{T}"/>.</returns>
public static Result<T> Adjusts<T>(this Result<T> result, Action<T> adjuster)
{
if (result.IsSuccess)
result.Value.Adjust(adjuster);

return result;
}

/// <summary>
/// Checks whether the user has the required <paramref name="permission"/> (see <see cref="ExecutionContext.UserIsAuthorized(string)"/>).
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/CoreEx.Test/Framework/Results/ResultTTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,32 @@ public void Failure_Value()
var ir = (IResult)Result<int>.Fail("On no!");
Assert.Throws<BusinessException>(() => _ = ir.Value);
}

[Test]
public void Adjusts()
{
var r = Result<Person>.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<Person>.Fail(new BusinessException());
r2 = r.Adjusts(v => v.Id = 2);
Assert.That(r2.IsSuccess, Is.False);
}

public class Person
{
public int Id { get; set; }
}
}
}
Loading