| | | 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 | | using MyNet.UI.Commands; |
| | | 11 | | using MyNet.UI.Dialogs.ContentDialogs; |
| | | 12 | | using MyNet.UI.Dialogs.FileDialogs; |
| | | 13 | | using MyNet.UI.Dialogs.MessageBox; |
| | | 14 | | |
| | | 15 | | #pragma warning disable IDE0130 |
| | | 16 | | namespace MyNet.UI.Dialogs; |
| | | 17 | | #pragma warning restore IDE0130 |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Extension methods for registering dialog services into a <see cref="IServiceCollection"/>. |
| | | 21 | | /// </summary> |
| | | 22 | | public static class ServiceCollectionExtensions |
| | | 23 | | { |
| | | 24 | | extension(IServiceCollection services) |
| | | 25 | | { |
| | | 26 | | /// <summary> |
| | | 27 | | /// Registers the dialog stack with default headless implementations suitable for tests and non-UI hosts. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="configure">Optional callback to register platform strategies or presenters.</param> |
| | | 30 | | /// <returns>The same service collection for chaining.</returns> |
| | | 31 | | public IServiceCollection AddDialogs(Action<DialogServiceBuilder>? configure = null) |
| | | 32 | | { |
| | | 33 | | var builder = new DialogServiceBuilder(services); |
| | | 34 | | configure?.Invoke(builder); |
| | | 35 | | |
| | | 36 | | services.TryAddSingleton<IContentDialogService, ContentDialogService>(); |
| | | 37 | | services.TryAddSingleton<IMessageBoxFactory, DefaultMessageBoxFactory>(); |
| | | 38 | | services.TryAddSingleton<IMessageBoxService, MessageBoxService>(); |
| | | 39 | | services.TryAddSingleton<IFileDialogService, CancelledFileDialogService>(); |
| | | 40 | | services.TryAddSingleton<IDialogService, DialogService>(); |
| | | 41 | | |
| | | 42 | | services.TryAddEnumerable( |
| | | 43 | | ServiceDescriptor.Singleton<IDialogStrategy, HeadlessDialogStrategy>()); |
| | | 44 | | services.TryAddEnumerable( |
| | | 45 | | ServiceDescriptor.Singleton<IDialogStrategy, PresenterDialogStrategy>()); |
| | | 46 | | |
| | | 47 | | return services; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Registers a custom <see cref="IDialogStrategy"/> implementation (e.g. WPF window host). |
| | | 52 | | /// </summary> |
| | | 53 | | /// <typeparam name="TStrategy">Strategy implementation type.</typeparam> |
| | | 54 | | /// <returns>The same service collection for chaining.</returns> |
| | | 55 | | public IServiceCollection AddDialogStrategy<TStrategy>() |
| | | 56 | | where TStrategy : class, IDialogStrategy |
| | | 57 | | { |
| | | 58 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogStrategy, TStrategy>()); |
| | | 59 | | return services; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Registers a custom <see cref="IDialogStrategy"/> instance. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <typeparam name="TStrategy">Strategy contract type.</typeparam> |
| | | 66 | | /// <param name="strategy">Strategy instance.</param> |
| | | 67 | | /// <returns>The same service collection for chaining.</returns> |
| | | 68 | | public IServiceCollection AddDialogStrategy<TStrategy>(TStrategy strategy) |
| | | 69 | | where TStrategy : class, IDialogStrategy |
| | | 70 | | { |
| | | 71 | | ArgumentNullException.ThrowIfNull(strategy); |
| | | 72 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogStrategy>(strategy)); |
| | | 73 | | return services; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Registers a platform <see cref="IDialogPresenter"/> and enables <see cref="PresenterDialogStrategy"/>. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <typeparam name="TPresenter">Presenter implementation type.</typeparam> |
| | | 80 | | /// <returns>The same service collection for chaining.</returns> |
| | | 81 | | public IServiceCollection AddDialogPresenter<TPresenter>() |
| | | 82 | | where TPresenter : class, IDialogPresenter |
| | | 83 | | { |
| | | 84 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogPresenter, TPresenter>()); |
| | | 85 | | return services; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Registers a platform <see cref="IDialogPresenter"/> instance. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <typeparam name="TPresenter">Presenter contract type.</typeparam> |
| | | 92 | | /// <param name="presenter">Presenter instance.</param> |
| | | 93 | | /// <returns>The same service collection for chaining.</returns> |
| | | 94 | | public IServiceCollection AddDialogPresenter<TPresenter>(TPresenter presenter) |
| | | 95 | | where TPresenter : class, IDialogPresenter |
| | | 96 | | { |
| | | 97 | | ArgumentNullException.ThrowIfNull(presenter); |
| | | 98 | | services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogPresenter>(presenter)); |
| | | 99 | | return services; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Replaces the default headless <see cref="IFileDialogService"/> with a platform implementation. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <typeparam name="TFileDialogService">File dialog service type.</typeparam> |
| | | 106 | | /// <returns>The same service collection for chaining.</returns> |
| | | 107 | | public IServiceCollection AddFileDialogService<TFileDialogService>() |
| | | 108 | | where TFileDialogService : class, IFileDialogService |
| | | 109 | | { |
| | | 110 | | services.Replace(ServiceDescriptor.Singleton<IFileDialogService, TFileDialogService>()); |
| | | 111 | | return services; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Replaces the default <see cref="IMessageBoxFactory"/> (e.g. for custom styling). |
| | | 116 | | /// </summary> |
| | | 117 | | /// <typeparam name="TFactory">Factory implementation type.</typeparam> |
| | | 118 | | /// <returns>The same service collection for chaining.</returns> |
| | | 119 | | public IServiceCollection AddMessageBoxFactory<TFactory>() |
| | | 120 | | where TFactory : class, IMessageBoxFactory |
| | | 121 | | { |
| | | 122 | | services.Replace(ServiceDescriptor.Singleton<IMessageBoxFactory, TFactory>()); |
| | | 123 | | return services; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Supplies a shared <see cref="ICommandFactory"/> for message box view models. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="commandFactory">Command factory instance.</param> |
| | | 130 | | /// <returns>The same service collection for chaining.</returns> |
| | | 131 | | public IServiceCollection AddMessageBoxCommandFactory(ICommandFactory commandFactory) |
| | | 132 | | { |
| | | 133 | | ArgumentNullException.ThrowIfNull(commandFactory); |
| | | 134 | | services.Replace(ServiceDescriptor.Singleton<IMessageBoxFactory>( |
| | | 135 | | new DefaultMessageBoxFactory(commandFactory))); |
| | | 136 | | return services; |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Configures optional dialog registrations inside <see cref="ServiceCollectionExtensions.AddDialogs"/>. |
| | | 143 | | /// </summary> |
| | | 144 | | public sealed class DialogServiceBuilder(IServiceCollection services) |
| | | 145 | | { |
| | | 146 | | /// <summary> |
| | | 147 | | /// Gets the underlying service collection. |
| | | 148 | | /// </summary> |
| | 27 | 149 | | public IServiceCollection Services { get; } = services ?? throw new ArgumentNullException(nameof(services)); |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Registers a platform presenter on the underlying service collection. |
| | | 153 | | /// </summary> |
| | | 154 | | public DialogServiceBuilder AddPresenter<TPresenter>() |
| | | 155 | | where TPresenter : class, IDialogPresenter |
| | | 156 | | { |
| | 3 | 157 | | Services.AddDialogPresenter<TPresenter>(); |
| | 3 | 158 | | return this; |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |