2
votes

Hi when I am looking at the implementation of PrismApplicationBase I found following piece of code

    /// <summary>
    /// Registers all types that are required by Prism to function with the container.
    /// </summary>
    /// <param name="containerRegistry"></param>
    protected virtual void RegisterRequiredTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterInstance<IContainerExtension>(_containerExtension);
        containerRegistry.RegisterSingleton<ILoggerFacade, EmptyLogger>();
        containerRegistry.RegisterSingleton<IApplicationProvider, ApplicationProvider>();
        containerRegistry.RegisterSingleton<IApplicationStore, ApplicationStore>();
        containerRegistry.RegisterSingleton<IEventAggregator, EventAggregator>();
        containerRegistry.RegisterSingleton<IDependencyService, DependencyService>();
        containerRegistry.RegisterSingleton<IPageDialogService, PageDialogService>();
        containerRegistry.RegisterSingleton<IDeviceService, DeviceService>();
        containerRegistry.RegisterSingleton<IPageBehaviorFactory, PageBehaviorFactory>();
        containerRegistry.RegisterSingleton<IModuleCatalog, ModuleCatalog>();
        containerRegistry.RegisterSingleton<IModuleManager, ModuleManager>();
        containerRegistry.RegisterSingleton<IModuleInitializer, ModuleInitializer>();
        containerRegistry.Register<INavigationService, PageNavigationService>(NavigationServiceName);
    }

we can see that the last registration for the navigation service is NOT singleton.

So my two questions are:

  1. Why the registration for the INavigationService is not singleton like other services ?
  2. Why do we assign a name for it (i.e. NavigationServiceName)
2

2 Answers

0
votes

First of all, Navigating in a Prism application is conceptually different than standard navigation in Xamarin.Forms. While Xamarin.Forms navigation relies on a Page class instance to navigate, Prism removes all dependencies on Page types to achieve loosely coupled navigation from within a ViewModel. In Prism, the concept of navigating to a View or navigating to a ViewModel does not exist.So INavigationService is not singleton.

Navigate to an experience, or a unique identifier, which represents the target view you wish to navigate to in your application. so you need assgin a name for it.

0
votes

If you take a look at the source code, you'll notice that the PageNavigationService has no state of its own, so there's no need to make it a singleton.

One tries to have as few singletons as possible, normally. They introduce overhead, cannot die (sortof an intentional memory leak) and complicate things.