feat: create request projects for basic CQRS
This commit is contained in:
commit
d614788e06
190 changed files with 12236 additions and 0 deletions
90
src/request.result/_extensions/Extensions.Enumerable.cs
Normal file
90
src/request.result/_extensions/Extensions.Enumerable.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
// Copyright (c) The Geekeey Authors
|
||||
// SPDX-License-Identifier: EUPL-1.2
|
||||
|
||||
namespace Geekeey.Request.Result;
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for or relating to <see cref="Result{T}"/>.
|
||||
/// </summary>
|
||||
public static partial class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Turns a sequence of results into a single result containing the success values in the results only if all the
|
||||
/// results have success values.
|
||||
/// </summary>
|
||||
/// <param name="results">The results to turn into a single sequence.</param>
|
||||
/// <typeparam name="T">The type of the success values in the results.</typeparam>
|
||||
/// <returns>A single result containing a sequence of all the success values from the original sequence of results,
|
||||
/// or the first failure value encountered within the sequence.</returns>
|
||||
/// <remarks>
|
||||
/// This method completely enumerates the input sequence before returning and is not lazy. As a consequence of this,
|
||||
/// the sequence within the returned result is an <see cref="IReadOnlyList{T}"/>.
|
||||
/// </remarks>
|
||||
public static Result<IReadOnlyList<T>> Join<T>(this IEnumerable<Result<T>> results)
|
||||
{
|
||||
_ = results.TryGetNonEnumeratedCount(out var count);
|
||||
var list = new List<T>(count);
|
||||
|
||||
foreach (var result in results)
|
||||
{
|
||||
if (!result.TryGetValue(out T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Join{T}(IEnumerable{Result{T}})"/>
|
||||
/// <remarks>
|
||||
/// For parallel execution of the async tasks, one should await the <c>Task.WhenAll()</c> of the provided list
|
||||
/// before calling this function
|
||||
/// </remarks>
|
||||
/// <seealso cref="Join{T}(IEnumerable{Result{T}})"/>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async ValueTask<Result<IReadOnlyList<T>>> Join<T>(this IEnumerable<ValueTask<Result<T>>> results)
|
||||
{
|
||||
_ = results.TryGetNonEnumeratedCount(out var count);
|
||||
var list = new List<T>(count);
|
||||
|
||||
foreach (var result in results)
|
||||
{
|
||||
if (!(await result).TryGetValue(out T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Join{T}(IEnumerable{Result{T}})"/>
|
||||
/// <remarks>
|
||||
/// For parallel execution of the async tasks, one should await the <c>Task.WhenAll()</c> of the provided list
|
||||
/// before calling this function
|
||||
/// </remarks>
|
||||
/// <seealso cref="Join{T}(IEnumerable{Result{T}})"/>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static async Task<Result<IReadOnlyList<T>>> Join<T>(this IEnumerable<Task<Result<T>>> results)
|
||||
{
|
||||
_ = results.TryGetNonEnumeratedCount(out var count);
|
||||
var list = new List<T>(count);
|
||||
|
||||
foreach (var result in results)
|
||||
{
|
||||
if (!(await result).TryGetValue(out T? value, out var error))
|
||||
{
|
||||
return new Result<IReadOnlyList<T>>(error);
|
||||
}
|
||||
|
||||
list.Add(value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue