< Summary

Information
Class: MyNet.UI.Locators.Conventions.TypeResolver
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/Conventions/TypeResolver.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 54
Line coverage: 100%
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
.ctor(...)100%11100%
Register(...)100%11100%
Resolve(...)100%11100%
ClearCache()100%11100%
ResolveInternal(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/Conventions/TypeResolver.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TypeResolver.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Concurrent;
 9using System.Collections.Generic;
 10using System.Linq;
 11
 12namespace MyNet.UI.Locators.Conventions;
 13
 14/// <summary>
 15/// Implements a type resolver that uses a collection of naming conventions to resolve types, with support for manual re
 16/// </summary>
 17/// <param name="conventions">The collection of naming conventions to use for type resolution.</param>
 18public sealed class TypeResolver(IEnumerable<ITypeNamingConvention> conventions) : ITypeResolver
 19{
 4520    private readonly IReadOnlyList<ITypeNamingConvention> _conventions = [.. conventions];
 4521    private readonly ConcurrentDictionary<Type, Type?> _cache = new();
 4522    private readonly ConcurrentDictionary<Type, Type> _manual = new();
 23
 24    /// <summary>
 25    /// Registers a mapping between a source type and a target type. This allows you to override the default resolution 
 26    /// </summary>
 27    /// <param name="source">The source type to map from.</param>
 28    /// <param name="target">The target type to map to.</param>
 29    public void Register(Type source, Type target)
 30    {
 2131        _manual[source] = target;
 2132        _cache.TryRemove(source, out _);
 2133    }
 34
 35    /// <summary>
 36    /// Resolves the target type for the given source type. The resolver first checks for any manually registered mappin
 37    /// </summary>
 38    /// <param name="source">The source type to resolve.</param>
 39    /// <returns>The resolved target type, or null if no mapping is found.</returns>
 5740    public Type? Resolve(Type source) => _cache.GetOrAdd(source, ResolveInternal);
 41
 42    /// <summary>
 43    /// Clears the convention-based resolution cache. Manually registered mappings are preserved and will still override
 44    /// </summary>
 645    public void ClearCache() => _cache.Clear();
 46
 47    /// <summary>
 48    /// Performs the actual resolution logic for a given source type. This method first checks if there is a manually re
 49    /// </summary>
 50    /// <param name="source">The source type to resolve.</param>
 51    /// <returns>The resolved target type, or null if no mapping is found.</returns>
 5152    private Type? ResolveInternal(Type source) => _manual.TryGetValue(source, out var manual) ? manual : _conventions.Se
 53}
 54