< Summary

Information
Class: MyNet.UI.Commands.CommandFactoryExtensions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Commands/Extensions/CommandFactoryExtensions.cs
Tag: 323_28699572109
Line coverage
87%
Covered lines: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 113
Line coverage: 87%
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
GetOrDefault(...)100%22100%
CreateRequired(...)100%11100%
CreateRequired(...)100%11100%
CreateRequired(...)100%210%
CreateRequired(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Commands/Extensions/CommandFactoryExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="CommandFactoryExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Diagnostics.CodeAnalysis;
 9using System.Threading.Tasks;
 10using System.Windows.Input;
 11
 12#pragma warning disable IDE0130
 13namespace MyNet.UI.Commands;
 14#pragma warning restore IDE0130
 15
 16/// <summary>
 17/// Provides convenience helpers around <see cref="ICommandFactory"/>.
 18/// </summary>
 19public static class CommandFactoryExtensions
 20{
 21    extension(ICommandFactory? factory)
 22    {
 23        /// <summary>
 24        /// Returns the factory instance or <see cref="RelayCommandFactory.Default"/> when null.
 25        /// </summary>
 26        [SuppressMessage("Design", "CA1024:Use properties where appropriate", Justification = "Method is more discoverab
 51627        public ICommandFactory GetOrDefault() => factory ?? RelayCommandFactory.Default;
 28    }
 29
 30    extension(ICommandFactory factory)
 31    {
 32        /// <summary>
 33        /// Creates a command that requires a non-null parameter of type T. If the parameter is null, the command will n
 34        /// </summary>
 35        /// <param name="execute">The action to execute.</param>
 36        /// <typeparam name="T">The type of the parameter.</typeparam>
 37        /// <returns>A command that executes the action if the parameter is not null.</returns>
 38        /// <exception cref="ArgumentNullException">Thrown if the factory or execute action is null.</exception>
 39        public ICommand CreateRequired<T>(Action<T> execute)
 40        {
 9041            ArgumentNullException.ThrowIfNull(factory);
 9042            ArgumentNullException.ThrowIfNull(execute);
 43
 9044            return factory.Create<T>(x =>
 9045            {
 9046                if (x is null)
 9047                    return;
 9048
 9049                execute.Invoke(x);
 9050            });
 51        }
 52
 53        /// <summary>
 54        /// Creates a command that requires a non-null parameter of type T and a canExecute function. If the parameter i
 55        /// </summary>
 56        /// <param name="execute">The action to execute.</param>
 57        /// <param name="canExecute">The function that determines whether the command can execute.</param>
 58        /// <typeparam name="T">The type of the parameter.</typeparam>
 59        /// <returns>A command that executes the action if the parameter is not null and canExecute returns true.</retur
 60        /// <exception cref="ArgumentNullException">Thrown if the factory, execute action, or canExecute function is nul
 61        public ICommand CreateRequired<T>(Action<T> execute, Func<T, bool> canExecute)
 62        {
 4563            ArgumentNullException.ThrowIfNull(factory);
 4564            ArgumentNullException.ThrowIfNull(execute);
 4565            ArgumentNullException.ThrowIfNull(canExecute);
 66
 4567            return factory.Create<T>(x =>
 4568                {
 4569                    if (x is null)
 4570                        return;
 4571
 4572                    execute.Invoke(x);
 4573                },
 4574                x => x is not null && canExecute.Invoke(x));
 75        }
 76
 77        /// <summary>
 78        /// Creates an asynchronous command that requires a non-null parameter of type T. If the parameter is null, the 
 79        /// </summary>
 80        /// <param name="execute">The asynchronous action to execute.</param>
 81        /// <typeparam name="T">The type of the parameter.</typeparam>
 82        /// <returns>A command that executes the asynchronous action if the parameter is not null.</returns>
 83        /// <exception cref="ArgumentNullException">Thrown if the factory or execute action is null.</exception>
 84        public ICommand CreateRequired<T>(Func<T, Task> execute)
 85        {
 086            ArgumentNullException.ThrowIfNull(factory);
 087            ArgumentNullException.ThrowIfNull(execute);
 88
 089            return factory.Create<T>(
 090                x => x is null ? Task.CompletedTask : execute(x));
 91        }
 92
 93        /// <summary>
 94        /// Creates an asynchronous command that requires a non-null parameter of type T and a canExecute function. If t
 95        /// </summary>
 96        /// <param name="execute">The asynchronous action to execute.</param>
 97        /// <param name="canExecute">The function that determines whether the command can execute.</param>
 98        /// <typeparam name="T">The type of the parameter.</typeparam>
 99        /// <returns>A command that executes the asynchronous action if the parameter is not null and canExecute returns
 100        /// <exception cref="ArgumentNullException">Thrown if the factory, execute action, or canExecute function is nul
 101        public ICommand CreateRequired<T>(Func<T, Task> execute, Func<T, bool> canExecute)
 102        {
 42103            ArgumentNullException.ThrowIfNull(factory);
 42104            ArgumentNullException.ThrowIfNull(execute);
 42105            ArgumentNullException.ThrowIfNull(canExecute);
 106
 42107            return factory.Create<T>(
 42108                x => x is null ? Task.CompletedTask : execute(x),
 42109                x => x is not null && canExecute.Invoke(x));
 110        }
 111    }
 112}
 113