| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr; |
| | | 6 | | |
| | | 7 | | public readonly record struct ServiceRegistrationInfo( |
| | 5939 | 8 | | ServiceDescriptor ServiceDescriptor) |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the service (abstraction/contract) <see cref="Type"/> that was registered. |
| | | 12 | | /// This is the key used by the DI container for resolution requests. |
| | | 13 | | /// </summary> |
| | 3554 | 14 | | public Type ServiceType => ServiceDescriptor.ServiceType; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the concrete implementation <see cref="Type"/>, if the registration |
| | | 18 | | /// was made with an implementation type. Returns <see langword="null"/> when: |
| | | 19 | | /// <list type="bullet"> |
| | | 20 | | /// <item>The registration uses an <see cref="ServiceDescriptor.ImplementationFactory"/>.</item> |
| | | 21 | | /// <item>The registration supplies a pre-built <see cref="ServiceDescriptor.ImplementationInstance"/>.</item> |
| | | 22 | | /// <item>The registration represents an open generic without a concrete close (rare in reflection scenarios).</it |
| | | 23 | | /// </list> |
| | | 24 | | /// </summary> |
| | 38 | 25 | | public Type? ImplementationType => ServiceDescriptor.ImplementationType; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the lifetime (<see cref="ServiceLifetime.Singleton"/>, |
| | | 29 | | /// <see cref="ServiceLifetime.Scoped"/>, or <see cref="ServiceLifetime.Transient"/>) |
| | | 30 | | /// associated with the registration. |
| | | 31 | | /// </summary> |
| | 1066 | 32 | | public ServiceLifetime Lifetime => ServiceDescriptor.Lifetime; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Indicates whether the registration was configured with a factory delegate |
| | | 36 | | /// (<see cref="ServiceDescriptor.ImplementationFactory"/>). When <see langword="true"/>, |
| | | 37 | | /// <see cref="ImplementationType"/> will be <see langword="null"/>. |
| | | 38 | | /// </summary> |
| | 21 | 39 | | public bool HasFactory => ServiceDescriptor.ImplementationFactory is not null; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Indicates whether the registration was configured with a pre-constructed |
| | | 43 | | /// instance (<see cref="ServiceDescriptor.ImplementationInstance"/>). When |
| | | 44 | | /// <see langword="true"/>, <see cref="ImplementationType"/> will typically be |
| | | 45 | | /// <see langword="null"/> (unless metadata reflects the instance's type indirectly). |
| | | 46 | | /// </summary> |
| | 20 | 47 | | public bool HasInstance => ServiceDescriptor.ImplementationInstance is not null; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Returns a detailed, formatted string representation of this registration |
| | | 51 | | /// suitable for debugging and diagnostics. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <returns>A multi-line formatted string with registration details.</returns> |
| | | 54 | | public string ToDetailedString() |
| | | 55 | | { |
| | 19 | 56 | | var sb = new StringBuilder(); |
| | 19 | 57 | | var serviceTypeName = FormatTypeName(ServiceType); |
| | | 58 | | |
| | 19 | 59 | | sb.AppendLine($"┌─ {serviceTypeName}"); |
| | 19 | 60 | | sb.AppendLine($"│ Lifetime: {Lifetime}"); |
| | | 61 | | |
| | 19 | 62 | | if (HasInstance) |
| | | 63 | | { |
| | 2 | 64 | | var instanceType = ServiceDescriptor.ImplementationInstance?.GetType(); |
| | 2 | 65 | | sb.AppendLine($"│ Implementation: Instance ({FormatTypeName(instanceType)})"); |
| | | 66 | | } |
| | 17 | 67 | | else if (HasFactory) |
| | | 68 | | { |
| | 1 | 69 | | sb.AppendLine($"│ Implementation: Factory"); |
| | | 70 | | } |
| | 16 | 71 | | else if (ImplementationType is not null) |
| | | 72 | | { |
| | 16 | 73 | | sb.AppendLine($"│ Implementation: {FormatTypeName(ImplementationType)}"); |
| | | 74 | | } |
| | | 75 | | |
| | 19 | 76 | | sb.Append($"└─"); |
| | | 77 | | |
| | 19 | 78 | | return sb.ToString(); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private static string FormatTypeName(Type? type) |
| | | 82 | | { |
| | 39 | 83 | | if (type is null) |
| | | 84 | | { |
| | 0 | 85 | | return "<unknown>"; |
| | | 86 | | } |
| | | 87 | | |
| | 39 | 88 | | if (!type.IsGenericType) |
| | | 89 | | { |
| | 35 | 90 | | return type.Name; |
| | | 91 | | } |
| | | 92 | | |
| | 4 | 93 | | if (type.IsGenericTypeDefinition) |
| | | 94 | | { |
| | | 95 | | // Open generic: IGenericService<> |
| | 2 | 96 | | var baseName = type.Name.Split('`')[0]; |
| | 2 | 97 | | return $"{baseName}<>"; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | // Closed generic: IGenericService<String> |
| | 2 | 101 | | var genericBaseName = type.Name.Split('`')[0]; |
| | 2 | 102 | | var genericArgs = string.Join(", ", type.GetGenericArguments().Select(FormatTypeName)); |
| | 2 | 103 | | return $"{genericBaseName}<{genericArgs}>"; |
| | | 104 | | } |
| | | 105 | | } |