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

add more control on execute api #29

Merged
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
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
Loading