| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ServiceCollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 |
| | | 12 | | namespace MyNet.UI.Navigation; |
| | | 13 | | #pragma warning restore IDE0130 |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Extension methods for registering navigation services. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class ServiceCollectionExtensions |
| | | 19 | | { |
| | | 20 | | extension(IServiceCollection services) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Registers the modern navigation stack with its default implementations. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <returns>The same service collection for chaining.</returns> |
| | | 26 | | public IServiceCollection AddNavigation() |
| | | 27 | | { |
| | 24 | 28 | | services.TryAddSingleton<INavigationJournal, NavigationJournal>(); |
| | 24 | 29 | | services.TryAddSingleton<INavigationLifecycle, NavigationLifecycle>(); |
| | 24 | 30 | | services.TryAddSingleton<INavigationService, NavigationService>(); |
| | 24 | 31 | | services.TryAddSingleton<INavigationClient, NavigationClient>(); |
| | | 32 | | |
| | 24 | 33 | | return services; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Registers a navigation guard implementation. |
| | | 38 | | /// Guards run in registration order; the first guard returning <see langword="false"/> cancels navigation. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <typeparam name="TGuard">Guard implementation type.</typeparam> |
| | | 41 | | /// <returns>The same service collection for chaining.</returns> |
| | | 42 | | public IServiceCollection AddNavigationGuard<TGuard>() |
| | | 43 | | where TGuard : class, INavigationGuard |
| | | 44 | | { |
| | 0 | 45 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<INavigationGuard, TGuard>()); |
| | 0 | 46 | | return services; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Registers a navigation guard instance. |
| | | 51 | | /// Guards run in registration order; the first guard returning <see langword="false"/> cancels navigation. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <typeparam name="TGuard">Guard contract type.</typeparam> |
| | | 54 | | /// <param name="guard">Guard instance.</param> |
| | | 55 | | /// <returns>The same service collection for chaining.</returns> |
| | | 56 | | public IServiceCollection AddNavigationGuard<TGuard>(TGuard guard) |
| | | 57 | | where TGuard : class, INavigationGuard |
| | | 58 | | { |
| | 6 | 59 | | ArgumentNullException.ThrowIfNull(guard); |
| | 6 | 60 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<INavigationGuard>(guard)); |
| | 6 | 61 | | return services; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Registers a navigation middleware implementation. |
| | | 66 | | /// Middleware runs in registration order as the outermost layer first (similar to ASP.NET Core). |
| | | 67 | | /// </summary> |
| | | 68 | | /// <typeparam name="TMiddleware">Middleware implementation type.</typeparam> |
| | | 69 | | /// <returns>The same service collection for chaining.</returns> |
| | | 70 | | public IServiceCollection AddNavigationMiddleware<TMiddleware>() |
| | | 71 | | where TMiddleware : class, INavigationMiddleware |
| | | 72 | | { |
| | 0 | 73 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<INavigationMiddleware, TMiddleware>()); |
| | 0 | 74 | | return services; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Registers a navigation middleware instance. |
| | | 79 | | /// Middleware runs in registration order as the outermost layer first (similar to ASP.NET Core). |
| | | 80 | | /// </summary> |
| | | 81 | | /// <typeparam name="TMiddleware">Middleware contract type.</typeparam> |
| | | 82 | | /// <param name="middleware">Middleware instance.</param> |
| | | 83 | | /// <returns>The same service collection for chaining.</returns> |
| | | 84 | | public IServiceCollection AddNavigationMiddleware<TMiddleware>(TMiddleware middleware) |
| | | 85 | | where TMiddleware : class, INavigationMiddleware |
| | | 86 | | { |
| | 0 | 87 | | ArgumentNullException.ThrowIfNull(middleware); |
| | 0 | 88 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<INavigationMiddleware>(middleware)); |
| | 0 | 89 | | return services; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |