Prism icon indicating copy to clipboard operation
Prism copied to clipboard

Bringing Async to RequestNavigate

Open dvorn opened this issue 10 years ago • 10 comments

I suggest to discuss various approaches to bring async to RequestNavigate and compare their relative merits and drawbacks so that we can decide which way to go.

The goal is to let the user write code like this:

NavigationResult result = await regionManager.RequestNavigateAsync(region, view, parameters);
NavigationResult result = await region.RequestNavigateAsync(view, parameters);

To be symmetrical to the user, we also should provide

public interface IConfirmNavigationRequestAsync : INavigationAware
{
    Task<bool> ConfirmNavigationRequestAsync(NavigationContext navigationContext);
}

The approaches discussed so far are:

  1. Provide async variants of RequestNavigate as extension methods (@wldevries @briannoyes). This was started in #124 and I prepared almost complete variant (sans tests for added functionality) in #656.
  2. Change internal workings of Prism and native interfaces to use async. Provide callback-based methods as extensions. (@dvorn) This is almost complete in #646.
  3. Extend native interfaces of Prism to include async versions of RequestNavigate ( @brianlagunas, https://github.com/PrismLibrary/Prism/pull/124#issuecomment-138973523). The work was also started in #124 (towards the end) but postponed. It is technically possible to complete this work.

dvorn avatar Jun 23 '16 12:06 dvorn

We need to compare approachs against:

Backward compatibility for

  • Ordinary programs
  • Programs that implement custom RegionNavigationService, RegionNavigationJournal, RegionManager
  • Mock objects created by deriving from interfaces IRegionManager, etc.
  • Mocks created by Moq and other mocking libraries

Performance

Amount of code to maintain

Ease of future development - adding features like #119, #30.

Ease of creation of custom implementations of IRegionNavigationService, etc.

dvorn avatar Jun 23 '16 12:06 dvorn

About approach 1:

Advanced users may start using this approach immediately, no need to wait for this to become part of Prism. Just grab the newly added files from the PR, add them to your project, register new RegionNavigationService in the container and voila, it works.

For novice users, however, it'd better be part of Prism with corresponding changes in the documentation.

dvorn avatar Jun 23 '16 13:06 dvorn

@brianlagunas

My only concern is the testability of the extension methods in Prism applications. Those are a pain to test, and if the methods were added to the interface, it would be easier to mock/test.

I hear you. You and @briannoyes finally convinced me. In fact, interfaces in Prism.Regions have different nature and should be treated differently.

there are those interfaces in Prism that users implement all the time and that changing in any way would be breaking - i.e. INavigationAware and IConfirmNavigationRequest. Those should remain as is and new async counterparts introduced.

We do not change those and add IConfirmNavigationRequestAsync. Period.

there are interfaces like IRegionManager that no one in their right mind is going to try to implement their own on

Not quite true. In fact, users kind of implement IRegionManager all the time when they create mock classes for IRegionManager.

However, IRegionManager seems to be the interface which is most frequently mocked by the users: kind of "when command is executed, RequestNavigate method is called". We simplify this as much as possible if include all possible overloads right into interface. If the users use Moq, it become straightforward. If they derive a class from IRegionManager, they may throw NotImplementedException on added methods which is done in one click in VS. Note also that the library itself never calls IReqionManager.RequestNavigate.

there are those that most users would rarely implement (i.e. INavigationService)

INavigateAsync and derivatives - IRegion, IRegionNavigationService are different. They call each other. E.g., Region calls NavigationService. If there is a multitude of overloads for RequestNavigate on RegionNavigationService, which one should the Region call? There is no any reasonable criteria to decide. E.g., the Region.RequestNavigate(Uri target) may substitute null for parameters, or it may call RegionNavigationService which will substitute null for parameters.

Thus, I think those interfaces should provide only one method - full-fledged asynchronous Task<NavigationResult> RequestNavigateAsync(Uri target, NavigationParameters parameters) and everything else provided as extension methods.

Internal implementation should be using this method without fall through to callback-based method. This way we will be in a better position in the future when we will be thinking of unifying approaches across platforms.

There will be minor issues when upgrading older projects which may contain custom implementations of RegionNavigationService or some mocking for IRegion. Those will need to be modified. However, upgrading RegionNavigationService from callback-interface to async-interface is almost automatic (I provided the example). Upgrading the mocks is also trivial. Moreover, mocking Task-based API is much more simple than mocking callback-based API. That is, we are actually simplifying the job of developers.

I am going to prepare a new PR.

Do we need to also include an implementation for #119?

dvorn avatar Jun 28 '16 19:06 dvorn

Sure, might as well add #119 if we are trying to tackle this async/await feature.

brianlagunas avatar Jun 28 '16 20:06 brianlagunas

@dvorn I know that adding async/await to prism wpf is challenging, but I was wondering if you could post/link the latest version you have for navigation and interactivity. I'd love to do some testing and give feedback.

pdinnissen avatar Jul 15 '17 04:07 pdinnissen

@pdinnissen Actually, there is nothing special in adding async/await. The RequestNavigate method has always been asynchronous. "Asynchronous" means that the call returns before the actual job initiated by that call has been done.

There are many patterns for asynchronous APIs: https://docs.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/

In most cases, an asynchronous API following one pattern can be easily, mechanically transformed to another API. In particular, the form

void RequestNavigate(... , Action<NavigationResult> callback);

can be expressed as a Task-based API

Task<NavigationResult> RequestNavigate(...);

In the first form, the method returns immediately, but actual navigation is completed when the callback is called. In the second form, the method also returns immediately, but the navigation is completed when the returned Task has run to completion.

If you have an API in one of the forms, you can easily switch to another, as is shown in https://github.com/PrismLibrary/Prism/pull/656.

In fact, you do not need to modify Prism to use Task-based asynchronous API. It is sufficient to add extensions methods to your project.

dvorn avatar Oct 04 '17 14:10 dvorn

@brianlagunas

As far as adding this feature goes, instead of trying to modify what's there and breaking existing apps, what about if we introduce an new "opt-in" region service called IRegionNavigationService which is async friendly?

Not sure I quite understand your suggestion. Users do not call IRegionNavigationService methods directly but rather use RegionManager and Region methods which, in turn, call to IRegionNavigationService. Thus it is a problem mainly with RegionManager.

dvorn avatar Oct 26 '17 05:10 dvorn

Now I think that probably @brianlagunas is right and real practical benefits of introducing Task-based async RequestNavigate methods not worth the backward compatibility troubles.

RequestNavigate methods runs synchronously (that is, navigation completed when the RequestNavigate method returns) unless the current view implements IConfirmNavigationRequest and ConfirmNavigationRequest runs asynchronously, which is a rear case. Thus, in most cases the users just run RequestNavigate overload without the callback parameter.

Even when IConfirmNavigationRequest is implemented and ConfirmNavigationRequest is running asynchronously, the real power of async/await might be used if the user have some logic after the await RequestNavigate(...). This also does not seem to be the case: seemingly, in most cases, RequestNavigate is the last method call. It is symptomatic that in sample applications such as StockTrader RI and View-Switching Navigation QuickStart they do not even bother to provide a callback in RequestNavigate methods.

The low activity in this thread also is an indication that most users do not really want this feature.

Thus, the only practical benefit is error handling after RequestNavigate: await RequestNavigate(...) joins the asynchronous thread of execution and provides convenient access to NavigationResult. The NavigationResult may contain two pieces of information: a Boolean value for the case when navigation was rejected by IConfirmNavigationRequest (and in most cases, nobody cares - see above), or an exception when the target view was not properly registered in the container. The second case might be useful for novice users for troubleshooting during very initial stage of development but not really important for mature applications.

dvorn avatar Oct 26 '17 05:10 dvorn

I am in agreement with you @dvorn. While this would be cool, It's probably not worth the effort and breaking changes it would introduce.

Thanks for the thoughtful response.

brianlagunas avatar Oct 26 '17 15:10 brianlagunas

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 29 '20 11:01 lock[bot]