< Summary

Information
Class: MyNet.UI.Navigation.Models.NavigationParameters
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Navigation/Models/NavigationParameters.cs
Tag: 323_28699572109
Line coverage
55%
Covered lines: 20
Uncovered lines: 16
Coverable lines: 36
Total lines: 150
Line coverage: 55.5%
Branch coverage
50%
Covered branches: 9
Total branches: 18
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
.cctor()100%210%
get_Keys()100%210%
get_Values()100%210%
get_Count()100%210%
get_Item(...)100%210%
From(...)35.71%791430.76%
Set(...)100%11100%
Get(...)100%22100%
TryGetValue(...)100%22100%
ContainsKey(...)100%210%
TryGetValue(...)100%210%
GetEnumerator()100%11100%
System.Collections.IEnumerable.GetEnumerator()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Navigation/Models/NavigationParameters.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NavigationParameters.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections;
 9using System.Collections.Generic;
 10using System.Diagnostics.CodeAnalysis;
 11using System.Linq;
 12using System.Reflection;
 13
 14namespace MyNet.UI.Navigation.Models;
 15
 16/// <summary>
 17/// Represents a lightweight, mutable bag of navigation parameters.
 18/// </summary>
 19[SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix", Justification = "NavigationParameters is the
 20public sealed class NavigationParameters : INavigationParameters, IReadOnlyDictionary<string, object?>
 21{
 22    private readonly Dictionary<string, object?> _values;
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="NavigationParameters"/> class.
 26    /// </summary>
 27    public NavigationParameters()
 3028        : this([])
 29    {
 3030    }
 31
 32    /// <summary>
 33    /// Initializes a new instance of the <see cref="NavigationParameters"/> class.
 34    /// </summary>
 35    /// <param name="values">Initial values.</param>
 36    public NavigationParameters(IEnumerable<KeyValuePair<string, object?>> values)
 37    {
 4538        ArgumentNullException.ThrowIfNull(values);
 4539        _values = new(values, StringComparer.Ordinal);
 4540    }
 41
 42    /// <summary>
 43    /// Gets an empty parameter bag.
 44    /// </summary>
 045    public static NavigationParameters Empty { get; } = new();
 46
 47    /// <inheritdoc />
 048    public IEnumerable<string> Keys => _values.Keys;
 49
 50    /// <inheritdoc />
 051    public IEnumerable<object?> Values => _values.Values;
 52
 53    /// <inheritdoc />
 054    public int Count => _values.Count;
 55
 56    /// <inheritdoc />
 057    public object? this[string key] => _values[key];
 58
 59    /// <summary>
 60    /// Creates a parameter bag from an existing object.
 61    /// </summary>
 62    /// <param name="values">Anonymous object, dictionary, or parameter bag.</param>
 63    /// <returns>A navigation parameter bag.</returns>
 64    public static NavigationParameters From(object? values)
 65    {
 66        switch (values)
 67        {
 68            case null:
 069                return new();
 70            case NavigationParameters parameters:
 071                return new(parameters);
 72            case IReadOnlyDictionary<string, object?> readOnlyDictionary:
 073                return new(readOnlyDictionary);
 74            case IEnumerable<KeyValuePair<string, object?>> pairs:
 075                return new(pairs);
 76            case IDictionary dictionary:
 77                {
 078                    var entries = new List<KeyValuePair<string, object?>>();
 079                    foreach (DictionaryEntry entry in dictionary)
 80                    {
 081                        if (entry.Key is string key)
 082                            entries.Add(new(key, entry.Value));
 83                    }
 84
 085                    return new(entries);
 86                }
 87
 88            default:
 989                return new(values.GetType()
 990                    .GetProperties(BindingFlags.Instance | BindingFlags.Public)
 991                    .Where(property => property.CanRead && property.GetIndexParameters().Length == 0)
 992                    .Select(property => new KeyValuePair<string, object?>(property.Name, property.GetValue(values))));
 93        }
 94    }
 95
 96    /// <summary>
 97    /// Adds or replaces a parameter value.
 98    /// </summary>
 99    /// <param name="key">Parameter key.</param>
 100    /// <param name="value">Parameter value.</param>
 101    /// <returns>The same parameter bag for chaining.</returns>
 102    public NavigationParameters Set(string key, object? value)
 103    {
 21104        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 21105        _values[key] = value;
 21106        return this;
 107    }
 108
 109    /// <summary>
 110    /// Gets a parameter value with type conversion support when possible.
 111    /// </summary>
 112    /// <typeparam name="T">Expected value type.</typeparam>
 113    /// <param name="key">Parameter key.</param>
 114    /// <param name="defaultValue">Default value when the parameter is absent or incompatible.</param>
 115    /// <returns>The converted value or <paramref name="defaultValue"/>.</returns>
 116    public T? Get<T>(string key, T? defaultValue = default)
 24117        => TryGetValue(key, out T? value) ? value : defaultValue;
 118
 119    /// <summary>
 120    /// Tries to retrieve a typed parameter value.
 121    /// </summary>
 122    /// <typeparam name="T">Expected value type.</typeparam>
 123    /// <param name="key">Parameter key.</param>
 124    /// <param name="value">Resolved value when found.</param>
 125    /// <returns><see langword="true"/> when the value exists and can be read.</returns>
 126    public bool TryGetValue<T>(string key, out T? value)
 127    {
 39128        ArgumentException.ThrowIfNullOrWhiteSpace(key);
 129
 39130        if (!_values.TryGetValue(key, out var rawValue))
 131        {
 3132            value = default;
 3133            return false;
 134        }
 135
 36136        return NavigationParameterConversions.TryConvert(rawValue, out value);
 137    }
 138
 139    /// <inheritdoc />
 0140    public bool ContainsKey(string key) => _values.ContainsKey(key);
 141
 142    /// <inheritdoc />
 0143    public bool TryGetValue(string key, out object? value) => _values.TryGetValue(key, out value);
 144
 145    /// <inheritdoc />
 9146    public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _values.GetEnumerator();
 147
 3148    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 149}
 150