When using Polly with MvvmCross, the choice between synchronous and asynchronous policies depends on the nature of the operations you are performing and how they interact with your application's UI and data access layers. Here are the key differences and considerations:
Synchronous Policies
- Blocking Behavior: Synchronous policies block the calling thread until the operation completes. This can be problematic in UI applications, as it can freeze the UI and prevent user interaction.
- Implementation: Synchronous policies in Polly are implemented using `Policy` and `Policy`. These policies are straightforward to use but are less suitable for UI applications due to their blocking nature.
- Use Cases: Synchronous policies are more commonly used in backend services or console applications where blocking is not a concern. However, in MvvmCross, they might be used in scenarios where asynchronous operations are not necessary or when dealing with legacy code that doesn't support async/await.
Asynchronous Policies
- Non-Blocking Behavior: Asynchronous policies, on the other hand, do not block the calling thread. They allow the application to continue executing other tasks while waiting for the operation to complete. This is crucial in UI applications like those built with MvvmCross, as it ensures the UI remains responsive.
- Implementation: Asynchronous policies in Polly are implemented using `AsyncPolicy` and `AsyncPolicy`. These policies support async/await and are designed to work seamlessly with asynchronous operations, making them ideal for UI applications.
- Use Cases: Asynchronous policies are highly recommended for MvvmCross applications. They allow for smooth UI interaction and are essential when dealing with network requests, database queries, or any operation that might take significant time to complete. Polly's asynchronous policies can be used to implement retry mechanisms, circuit breakers, or other resilience strategies without blocking the UI thread.
Integration with MvvmCross
In MvvmCross, asynchronous operations are typically handled using `MvxNotifyTask`, which provides a way to monitor the state of tasks and handle exceptions gracefully[4]. When integrating Polly's asynchronous policies with MvvmCross, you can use `MvxNotifyTask` to wrap the asynchronous execution of Polly policies. This ensures that any exceptions or task states are properly handled and notified to the UI.
For example, you might use Polly's `AsyncPolicy` to retry an asynchronous operation, and then wrap this operation in an `MvxNotifyTask` to handle the task's state and any exceptions that might occur during execution.
csharp
public class MyViewModel : MvxViewModel
{
private readonly ISomeService _someService;
private readonly AsyncPolicy _retryPolicy;
public MyViewModel(ISomeService someService)
{
_someService = someService;
_retryPolicy = Policy.Handle()
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
}
public IMvxCommand MyCommand { get; private set; }
private MvxNotifyTask _myTaskNotifier;
public MvxNotifyTask MyTaskNotifier
{
get => _myTaskNotifier;
private set => SetProperty(ref _myTaskNotifier, value);
}
public void Prepare()
{
MyCommand = new MvxCommand(() => MyTaskNotifier = MvxNotifyTask.Create(() => ExecuteWithRetryAsync(), onException: ex => OnException(ex)));
}
private async Task ExecuteWithRetryAsync()
{
await _retryPolicy.ExecuteAsync(async () => await _someService.DoSomethingAsync());
}
private void OnException(Exception exception)
{
// Handle exception
}
}
In summary, when using Polly with MvvmCross, asynchronous policies are generally preferred due to their non-blocking nature, which ensures a responsive UI. Synchronous policies might be used in specific scenarios but are less common in UI applications.
Citations:[1] http://www.thepollyproject.org/2019/02/13/custom-policies-for-all-execution-types-custom-policies-part-iv/
[2] https://stackoverflow.com/questions/59456197/using-polly-for-a-retry-attempt-from-an-async-function
[3] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[4] https://www.mvvmcross.com/documentation/advanced/mvxnotifytask
[5] https://ianvink.wordpress.com/tag/polly/
[6] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/march/async-programming-patterns-for-asynchronous-mvvm-applications-data-binding
[7] https://github.com/App-vNext/Polly/blob/main/docs/general.md
[8] https://www.pollydocs.org/community/libraries-and-contributions.html
[9] https://docs.ros.org/en/humble/How-To-Guides/Sync-Vs-Async.html