< Summary

Information
Class: MyNet.UI.Dialogs.ServiceCollectionExtensions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Dialogs/Extensions/ServiceCollectionExtensions.cs
Tag: 323_28699572109
Line coverage
46%
Covered lines: 14
Uncovered lines: 16
Coverable lines: 30
Total lines: 161
Line coverage: 46.6%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddDialogs(...)100%22100%
AddDialogStrategy(...)100%210%
AddDialogStrategy(...)100%210%
AddDialogPresenter(...)100%11100%
AddDialogPresenter(...)100%210%
AddFileDialogService(...)100%210%
AddMessageBoxFactory(...)100%210%
AddMessageBoxCommandFactory(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Dialogs/Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ServiceCollectionExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using Microsoft.Extensions.DependencyInjection;
 9using Microsoft.Extensions.DependencyInjection.Extensions;
 10using MyNet.UI.Commands;
 11using MyNet.UI.Dialogs.ContentDialogs;
 12using MyNet.UI.Dialogs.FileDialogs;
 13using MyNet.UI.Dialogs.MessageBox;
 14
 15#pragma warning disable IDE0130
 16namespace 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>
 22public 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        {
 2733            var builder = new DialogServiceBuilder(services);
 2734            configure?.Invoke(builder);
 35
 2736            services.TryAddSingleton<IContentDialogService, ContentDialogService>();
 2737            services.TryAddSingleton<IMessageBoxFactory, DefaultMessageBoxFactory>();
 2738            services.TryAddSingleton<IMessageBoxService, MessageBoxService>();
 2739            services.TryAddSingleton<IFileDialogService, CancelledFileDialogService>();
 2740            services.TryAddSingleton<IDialogService, DialogService>();
 41
 2742            services.TryAddEnumerable(
 2743                ServiceDescriptor.Singleton<IDialogStrategy, HeadlessDialogStrategy>());
 2744            services.TryAddEnumerable(
 2745                ServiceDescriptor.Singleton<IDialogStrategy, PresenterDialogStrategy>());
 46
 2747            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        {
 058            services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogStrategy, TStrategy>());
 059            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        {
 071            ArgumentNullException.ThrowIfNull(strategy);
 072            services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogStrategy>(strategy));
 073            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        {
 384            services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogPresenter, TPresenter>());
 385            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        {
 097            ArgumentNullException.ThrowIfNull(presenter);
 098            services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogPresenter>(presenter));
 099            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        {
 0110            services.Replace(ServiceDescriptor.Singleton<IFileDialogService, TFileDialogService>());
 0111            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        {
 0122            services.Replace(ServiceDescriptor.Singleton<IMessageBoxFactory, TFactory>());
 0123            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        {
 0133            ArgumentNullException.ThrowIfNull(commandFactory);
 0134            services.Replace(ServiceDescriptor.Singleton<IMessageBoxFactory>(
 0135                new DefaultMessageBoxFactory(commandFactory)));
 0136            return services;
 137        }
 138    }
 139}
 140
 141/// <summary>
 142/// Configures optional dialog registrations inside <see cref="ServiceCollectionExtensions.AddDialogs"/>.
 143/// </summary>
 144public sealed class DialogServiceBuilder(IServiceCollection services)
 145{
 146    /// <summary>
 147    /// Gets the underlying service collection.
 148    /// </summary>
 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    {
 157        Services.AddDialogPresenter<TPresenter>();
 158        return this;
 159    }
 160}
 161