< Summary

Information
Class: MyNet.UI.Dialogs.DialogServiceExtensions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Dialogs/Extensions/DialogServiceExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 8
Uncovered lines: 0
Coverable lines: 8
Total lines: 80
Line coverage: 100%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShowAsync(...)100%11100%
ShowAsync(...)100%11100%
Create(...)100%11100%
Create(...)100%11100%
ResolveFromLocator(...)50%22100%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DialogServiceExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using MyNet.UI.Dialogs.ContentDialogs;
 11using MyNet.UI.Locators;
 12
 13#pragma warning disable IDE0130
 14namespace MyNet.UI.Dialogs;
 15#pragma warning restore IDE0130
 16
 17/// <summary>
 18/// Extension methods that provide DI-friendly helpers around <see cref="IDialogService"/>.
 19/// They replace the need for a global static dialog manager.
 20/// </summary>
 21public static class DialogServiceExtensions
 22{
 23    extension(IDialogService dialogService)
 24    {
 25        /// <summary>
 26        /// Resolves a dialog instance from the view-model locator and shows it.
 27        /// </summary>
 28        /// <typeparam name="TDialog">The dialog type to resolve and show.</typeparam>
 29        /// <param name="viewModelLocator">The locator used to resolve the dialog.</param>
 30        /// <param name="options">Optional dialog options.</param>
 31        /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
 32        /// <returns>A non-typed dialog result.</returns>
 33        public Task<DialogResult<bool>> ShowAsync<TDialog>(IViewModelLocator viewModelLocator,
 34            DialogOptions? options = null,
 35            CancellationToken cancellationToken = default)
 36            where TDialog : class, IDialog
 637            => dialogService.ShowAsync(ResolveFromLocator<TDialog>(dialogService, viewModelLocator), options, cancellati
 38
 39        /// <summary>
 40        /// Resolves a typed dialog instance from the view-model locator and shows it.
 41        /// </summary>
 42        /// <typeparam name="TDialog">The typed dialog type to resolve and show.</typeparam>
 43        /// <typeparam name="TResult">The typed result value returned by the dialog.</typeparam>
 44        /// <param name="viewModelLocator">The locator used to resolve the dialog.</param>
 45        /// <param name="options">Optional dialog options.</param>
 46        /// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
 47        /// <returns>A strongly-typed dialog result.</returns>
 48        public Task<DialogResult<TResult>> ShowAsync<TDialog, TResult>(IViewModelLocator viewModelLocator,
 49            DialogOptions? options = null,
 50            CancellationToken cancellationToken = default)
 51            where TDialog : class, IDialog<TResult>
 352            => dialogService.ShowAsync(ResolveFromLocator<TDialog>(dialogService, viewModelLocator), options, cancellati
 53
 54        /// <summary>
 55        /// Creates a non-typed dialog builder after resolving the dialog from the view-model locator.
 56        /// </summary>
 57        public IDialogBuilder Create<TDialog>(IViewModelLocator viewModelLocator)
 58            where TDialog : class, IDialog
 659            => dialogService.Create(ResolveFromLocator<TDialog>(dialogService, viewModelLocator));
 60
 61        /// <summary>
 62        /// Creates a typed dialog builder after resolving the dialog from the view-model locator.
 63        /// </summary>
 64        public IDialogBuilder<TResult> Create<TDialog, TResult>(IViewModelLocator viewModelLocator)
 65            where TDialog : class, IDialog<TResult>
 366            => dialogService.Create(ResolveFromLocator<TDialog>(dialogService, viewModelLocator));
 67    }
 68
 69    private static TDialog ResolveFromLocator<TDialog>(IDialogService dialogService, IViewModelLocator viewModelLocator)
 70        where TDialog : class
 71    {
 1872        ArgumentNullException.ThrowIfNull(dialogService);
 1873        ArgumentNullException.ThrowIfNull(viewModelLocator);
 74
 1575        var dialog = viewModelLocator.Get<TDialog>();
 76
 1277        return dialog ?? throw new InvalidOperationException($"Dialog type '{typeof(TDialog).FullName}' could not be res
 78    }
 79}
 80