| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DisplayTextStrategyResolver.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.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Humanizer.Display.Registration; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Humanizer.Display; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Resolves display name providers using deterministic hierarchical fallback. |
| | | 17 | | /// Resolution order is: exact type, interfaces, base types, then object. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class DisplayTextStrategyResolver(IDisplayTextStrategyRegistry registry) : IDisplayTextStrategyResolver |
| | | 20 | | { |
| | 31 | 21 | | private readonly ConcurrentDictionary<Type, IDisplayTextStrategy?> _cache = new(); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | public bool TryGet<T>([NotNullWhen(true)] out IDisplayTextStrategy<T>? strategy) |
| | | 25 | | where T : notnull |
| | | 26 | | { |
| | 15 | 27 | | if (_cache.GetOrAdd(typeof(T), Resolve) is { } result) |
| | | 28 | | { |
| | 15 | 29 | | strategy = new TypedDisplayTextStrategyAdapter<T>(result); |
| | 15 | 30 | | return true; |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | strategy = null; |
| | 0 | 34 | | return false; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public IDisplayTextStrategy<T> GetRequired<T>() |
| | | 39 | | where T : notnull |
| | 15 | 40 | | => TryGet<T>(out var s) |
| | 15 | 41 | | ? s |
| | 15 | 42 | | : throw new InvalidOperationException($"No display text strategy registered for '{typeof(T).FullName}'."); |
| | | 43 | | |
| | | 44 | | /// <inheritdoc/> |
| | | 45 | | public IDisplayTextStrategy GetRequiredForType(Type type) |
| | | 46 | | { |
| | 104 | 47 | | ArgumentNullException.ThrowIfNull(type); |
| | | 48 | | |
| | 104 | 49 | | return _cache.GetOrAdd(type, Resolve) |
| | 104 | 50 | | ?? throw new InvalidOperationException($"No display text strategy registered for '{type.FullName}'."); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Resolves a display name provider for the requested type using hierarchical fallback: |
| | | 55 | | /// 1. Exact match: provider registered for the requested type. |
| | | 56 | | /// 2. Assignable fallback: provider registered for an interface or base type assignable from the requested type. |
| | | 57 | | /// 3. Enum fallback: provider registered for Enum if the requested type is an enum (important case). |
| | | 58 | | /// 4. Object fallback: provider registered for object. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="requestedType">The type for which to resolve a display name provider.</param> |
| | | 61 | | /// <returns>The resolved display name provider, or null if none is found.</returns> |
| | | 62 | | private IDisplayTextStrategy? Resolve(Type requestedType) |
| | | 63 | | { |
| | | 64 | | // 1. exact match |
| | 35 | 65 | | if (registry.TryGetStrategy(requestedType, out var exact)) |
| | 5 | 66 | | return exact; |
| | | 67 | | |
| | | 68 | | // 2. assignable fallback (interfaces / base types) |
| | 30 | 69 | | var fallback = registry |
| | 30 | 70 | | .Strategies |
| | 30 | 71 | | .FirstOrDefault(x => x.Key.IsAssignableFrom(requestedType)); |
| | | 72 | | |
| | 30 | 73 | | if (fallback.Value is not null) |
| | 30 | 74 | | return fallback.Value; |
| | | 75 | | |
| | | 76 | | // 3. Enum fallback (important case) |
| | 0 | 77 | | if (requestedType.IsEnum && registry.TryGetStrategy(typeof(Enum), out var enumStrategy)) |
| | | 78 | | { |
| | 0 | 79 | | return enumStrategy; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // 4. object fallback (optional global default) |
| | 0 | 83 | | return registry.TryGetStrategy(typeof(object), out var objectStrategy) ? objectStrategy : null; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |