| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TypeResolver.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Concurrent; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Linq; |
| | | 11 | | |
| | | 12 | | namespace 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> |
| | | 18 | | public sealed class TypeResolver(IEnumerable<ITypeNamingConvention> conventions) : ITypeResolver |
| | | 19 | | { |
| | 45 | 20 | | private readonly IReadOnlyList<ITypeNamingConvention> _conventions = [.. conventions]; |
| | 45 | 21 | | private readonly ConcurrentDictionary<Type, Type?> _cache = new(); |
| | 45 | 22 | | 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 | | { |
| | 21 | 31 | | _manual[source] = target; |
| | 21 | 32 | | _cache.TryRemove(source, out _); |
| | 21 | 33 | | } |
| | | 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> |
| | 57 | 40 | | 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> |
| | 6 | 45 | | 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> |
| | 51 | 52 | | private Type? ResolveInternal(Type source) => _manual.TryGetValue(source, out var manual) ? manual : _conventions.Se |
| | | 53 | | } |
| | | 54 | | |