Skip to content

Commit

Permalink
add more control on execute api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Dec 29, 2023
1 parent cb8de24 commit d8eb058
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,83 @@ public class ApiBaseViewModel : BaseViewModel
///
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <typeparam name="TMappedResult"></typeparam>
/// <param name="getServerResult"></param>
/// <param name="mapResult"></param>
/// <param name="onSuccess"></param>
/// <param name="onError"></param>
/// <returns></returns>
public async virtual Task ExecuteApi<TResult>(Func<Task<TResult>> getServerResult, Func<MessageContract<TResult>, Task> onSuccess, Func<ErrorContract, Task> onError = default)
public async virtual Task ExecuteMapApi<TResult, TMappedResult>(Func<Task<TResult>> getServerResult, Func<Task<TMappedResult>> mapResult, Func<MessageContract<TMappedResult>, Task> onSuccess, Func<ErrorContract, Task> onError = default)
{
try
{
Busy();
var result = await getServerResult();

var response = result.ToContract<TMappedResult>();

if (response.IsSuccess)
await onSuccess(response);
else
await OnServerErrorHandling(response.Error);
}
catch (Exception ex)
{
if (onError != null)
await onError(ex);
else
await OnError(ex);
}
finally
{
UnBusy();
}
}

/// <summary>
///
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="getServerResult"></param>
/// <param name="onSuccess"></param>
/// <param name="onError"></param>
/// <returns></returns>
public async virtual Task ExecuteMapApi<TResult>(Func<Task<TResult>> getServerResult, Func<MessageContract<TResult>, Task> onSuccess, Func<ErrorContract, Task> onError = default)
{
try
{
Busy();
var result = await getServerResult();

var response = result.ToContract<TResult>();

if (response.IsSuccess)
await onSuccess(response);
else
await OnServerErrorHandling(response.Error);
}
catch (Exception ex)
{
if (onError != null)
await onError(ex);
else
await OnError(ex);
}
finally
{
UnBusy();
}
}

/// <summary>
///
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="getServerResult"></param>
/// <param name="onSuccess"></param>
/// <param name="onError"></param>
/// <returns></returns>
public async virtual Task ExecuteApi<TResult>(Func<Task<object>> getServerResult, Func<MessageContract<TResult>, Task> onSuccess, Func<ErrorContract, Task> onError = default)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<IsPackable>true</IsPackable>
<Version>0.0.0.17</Version>
<Version>0.0.0.18</Version>
<Description>Model View View Model</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>mvvm,mvpvm,modelview,modelviewviewmodel</PackageTags>
Expand Down

0 comments on commit d8eb058

Please sign in to comment.