public static async Task <IServiceResponse <IEnumerable <T2> > > BindManyAsync <T, T2>( this IServiceResponse <IEnumerable <T> > serviceResponse, Func <T, Task <IServiceResponse <T2> > > bindFunc) { if (serviceResponse.IsLeft) { return(serviceResponse.CreateGenericErrorResponse <IEnumerable <T>, IEnumerable <T2> >(serviceResponse.GetLeft())); } var result = new List <T2>(); foreach (var element in serviceResponse.GetRight()) { var elementResponse = await bindFunc(element); if (elementResponse.IsLeft) { return(serviceResponse.CreateGenericErrorResponse <IEnumerable <T>, IEnumerable <T2> >(elementResponse.GetLeft())); } result.Add(elementResponse.GetRight()); } return(serviceResponse.CreateGenericDataResponse(result)); }
/// <summary> /// If <paramref name="serviceResponse"/>.IsLeft, returns a new <see cref="IServiceResponse{T2}" /> instance with the current /// <paramref name="serviceResponse"/>.Error. Otherwise, it binds the <paramref name="serviceResponse"/>.Data into /// the specified <paramref name="mappingFunction" />. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="T2">The type of the next <see cref="IServiceResponse{T2}" /> to return.</typeparam> /// <param name="serviceResponse">The service response.</param> /// <param name="mappingFunction">The mapping function.</param> /// <returns>Instance of <see cref="IServiceResponse{T2}" />.</returns> public static IServiceResponse <T2> Fmap <T, T2>(this IServiceResponse <T> serviceResponse, Func <T, T2> mappingFunction) { if (serviceResponse.IsLeft) { return(serviceResponse.CreateGenericErrorResponse <T, T2>(serviceResponse.GetLeft())); } T2 result = mappingFunction.Invoke(serviceResponse.GetRight()); return(serviceResponse.CreateGenericDataResponse(result)); }
/// <summary> /// Invokes the specified function if <seealso cref="IServiceResponse{T}.Error" /> is not null. Allows you to re-direct control flow with a new <typeparamref name="T" /> value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serviceResponse">The service response.</param> /// <param name="continueWithFunction">The continue with function.</param> /// <returns>If errors exist, returns the instance of IServiceResponse{T} returned by <paramref name="continueWithFunction" />, else returns current instance.</returns> public static IServiceResponse <T> CatchAndContinue <T>(this IServiceResponse <T> serviceResponse, Func <Error, T> continueWithFunction) { if (serviceResponse.Error != null) { T result = continueWithFunction.Invoke(serviceResponse.Error); return(serviceResponse.CreateGenericDataResponse <T>(result)); } return(serviceResponse); }
/// <summary> /// If <seealso cref="IServiceResponse{T}.Error" /> is not null, returns a new <see cref="IServiceResponse{T2}" /> instance with the current /// <seealso cref="IServiceResponse{T}.Error" />. Else, binds the <seealso cref="IServiceResponse{T}.Data" /> into the specified <paramref name="mappingFunction" />. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="T2">The type of the next <see cref="IServiceResponse{T2}" /> to return.</typeparam> /// <param name="serviceResponse">The service response.</param> /// <param name="mappingFunction">The mapping function.</param> /// <returns>Instance of <see cref="IServiceResponse{T2}" />.</returns> public static IServiceResponse <T2> Fmap <T, T2>(this IServiceResponse <T> serviceResponse, Func <T, T2> mappingFunction) { if (serviceResponse.Error != null) { return(serviceResponse.CreateGenericErrorResponse <T, T2>(serviceResponse.Error)); } T2 result = mappingFunction.Invoke(serviceResponse.Data); return(serviceResponse.CreateGenericDataResponse(result)); }
/// <summary> /// Invokes the specified function if <see cref="IServiceResponse{T}.Error"/> is not null. Allows you to re-direct control flow with a new <typeparamref name="T" /> value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serviceResponse">The service response.</param> /// <param name="continueWithFunction">The continue with function.</param> /// <returns>If errors exist, returns the instance of IServiceResponse{T} returned by <paramref name="continueWithFunction" />, else returns current instance.</returns> public static Task <IServiceResponse <T> > CatchAndContinueAsync <T>( this IServiceResponse <T> serviceResponse, Func <Error, T> continueWithFunction) { var tcs = new TaskCompletionSource <IServiceResponse <T> >(); if (serviceResponse.Error != null) { T result = continueWithFunction.Invoke(serviceResponse.Error); tcs.SetResult(serviceResponse.CreateGenericDataResponse <T>(result)); } else { tcs.SetResult(serviceResponse); } return(tcs.Task); }
/// <summary> /// Invokes the specified action if <see cref="IServiceResponse{T}.Error" /> is null. /// Returns the current <see cref="IServiceResponse{T}" /> instance unless the task /// from <paramref name="letFunc"/> <see cref="Task.IsFaulted"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serviceResponse">The service response.</param> /// <param name="letFunc">The let function.</param> /// <returns>Task<IServiceResponse<T>>.</returns> public static Task <IServiceResponse <T> > LetAsync <T>( this IServiceResponse <T> serviceResponse, Func <T, Task> letFunc) { if (serviceResponse.Error != null) { return(Task.FromResult(serviceResponse.CreateGenericErrorResponse(serviceResponse.Error))); } return(letFunc(serviceResponse.Data) .ContinueWith(task => { if (task.IsFaulted) { return serviceResponse.CreateGenericErrorResponse(task.Exception.ToError()); } return serviceResponse.CreateGenericDataResponse <T>(serviceResponse.Data); })); }