|
| 1 | +// Copyright (c) Autofac Project. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using Autofac.Core.Resolving.Pipeline; |
| 7 | + |
| 8 | +namespace Autofac.Core.Resolving; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Tracks registrations encountered in a resolve operation to accelerate circular dependency checks. |
| 12 | +/// </summary> |
| 13 | +internal sealed class DependencyDetectionState |
| 14 | +{ |
| 15 | + private static readonly IEqualityComparer<IComponentRegistration> ReferenceComparer = new ComponentRegistrationReferenceComparer(); |
| 16 | + private readonly Dictionary<IComponentRegistration, int> _registrations = new(ReferenceComparer); |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Gets a value indicating whether the state has been initialized from the active request stack. |
| 20 | + /// </summary> |
| 21 | + public bool IsInitialized { get; private set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes the state from the existing request stack, if not already initialized. |
| 25 | + /// </summary> |
| 26 | + /// <param name="existingStack">The current request stack segment.</param> |
| 27 | + public void Initialize(IEnumerable<ResolveRequestContext> existingStack) |
| 28 | + { |
| 29 | + if (IsInitialized) |
| 30 | + { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + foreach (var context in existingStack) |
| 35 | + { |
| 36 | + AddRegistration(context.Registration); |
| 37 | + } |
| 38 | + |
| 39 | + IsInitialized = true; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Returns true if the registration is currently tracked. |
| 44 | + /// </summary> |
| 45 | + /// <param name="registration">The registration to check.</param> |
| 46 | + /// <returns>True if the registration is tracked.</returns> |
| 47 | + public bool Contains(IComponentRegistration registration) |
| 48 | + { |
| 49 | + return IsInitialized && _registrations.ContainsKey(registration); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Adds a registration to the tracked set. |
| 54 | + /// </summary> |
| 55 | + /// <param name="registration">The registration to add.</param> |
| 56 | + public void Push(IComponentRegistration registration) |
| 57 | + { |
| 58 | + if (!IsInitialized) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + AddRegistration(registration); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Removes a registration from the tracked set. |
| 68 | + /// </summary> |
| 69 | + /// <param name="registration">The registration to remove.</param> |
| 70 | + public void Pop(IComponentRegistration registration) |
| 71 | + { |
| 72 | + if (!IsInitialized || !_registrations.TryGetValue(registration, out var count)) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (count <= 1) |
| 78 | + { |
| 79 | + _registrations.Remove(registration); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + _registrations[registration] = count - 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 88 | + private void AddRegistration(IComponentRegistration registration) |
| 89 | + { |
| 90 | + if (_registrations.TryGetValue(registration, out var count)) |
| 91 | + { |
| 92 | + _registrations[registration] = count + 1; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + _registrations.Add(registration, 1); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private sealed class ComponentRegistrationReferenceComparer : IEqualityComparer<IComponentRegistration> |
| 101 | + { |
| 102 | + public bool Equals(IComponentRegistration? x, IComponentRegistration? y) => ReferenceEquals(x, y); |
| 103 | + |
| 104 | + public int GetHashCode(IComponentRegistration obj) => RuntimeHelpers.GetHashCode(obj); |
| 105 | + } |
| 106 | +} |
0 commit comments