Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Pipelines #1121

Merged
merged 11 commits into from
May 27, 2020
Prev Previous commit
Next Next commit
Switch events in the services tracker to regular event objects.
alistairjevans committed May 22, 2020
commit 3cb998f37c4dc098540ba7f5d4f7aef00176cf59
39 changes: 5 additions & 34 deletions src/Autofac/Core/Registration/DefaultRegisteredServicesTracker.cs
Original file line number Diff line number Diff line change
@@ -38,14 +38,6 @@ internal class DefaultRegisteredServicesTracker : Disposable, IRegisteredService
private readonly ConcurrentDictionary<IServiceWithType, IReadOnlyList<IComponentRegistration>> _decorators
= new ConcurrentDictionary<IServiceWithType, IReadOnlyList<IComponentRegistration>>();

/// <summary>
/// Gets the set of properties used during component registration.
/// </summary>
/// <value>
/// An <see cref="IDictionary{TKey, TValue}"/> that can be used to share context across registrations.
/// </value>
private readonly IDictionary<string, object?> _properties = new Dictionary<string, object?>();

/// <summary>
/// Protects instance variables from concurrent access.
/// </summary>
@@ -61,22 +53,12 @@ public DefaultRegisteredServicesTracker()
/// Fired whenever a component is registered - either explicitly or via a
/// <see cref="IRegistrationSource"/>.
/// </summary>
public event EventHandler<IComponentRegistration> Registered
{
add => _properties[MetadataKeys.InternalRegisteredPropertyKey] = GetRegistered() + value;

remove => _properties[MetadataKeys.InternalRegisteredPropertyKey] = GetRegistered() - value;
}
public event EventHandler<IComponentRegistration>? Registered;

/// <summary>
/// Fired when an <see cref="IRegistrationSource"/> is added to the registry.
/// </summary>
public event EventHandler<IRegistrationSource> RegistrationSourceAdded
{
add => _properties[MetadataKeys.InternalRegistrationSourceAddedPropertyKey] = GetRegistrationSourceAdded() + value;

remove => _properties[MetadataKeys.InternalRegistrationSourceAddedPropertyKey] = GetRegistrationSourceAdded() - value;
}
public event EventHandler<IRegistrationSource>? RegistrationSourceAdded;

/// <inheritdoc />
public IEnumerable<IComponentRegistration> Registrations
@@ -110,7 +92,8 @@ public virtual void AddRegistration(IComponentRegistration registration, bool pr
}

_registrations.Add(registration);
GetRegistered()?.Invoke(this, registration);
var handler = Registered;
handler?.Invoke(this, registration);

if (originatedFromSource)
{
@@ -129,7 +112,7 @@ public void AddRegistrationSource(IRegistrationSource source)
foreach (var serviceRegistrationInfo in _serviceInfo)
serviceRegistrationInfo.Value.Include(source);

var handler = GetRegistrationSourceAdded();
var handler = RegistrationSourceAdded;
handler?.Invoke(this, source);
}
}
@@ -254,17 +237,5 @@ private ServiceRegistrationInfo GetServiceInfo(Service service)
_serviceInfo.Add(service, info);
return info;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private EventHandler<IComponentRegistration>? GetRegistered() =>
_properties.TryGetValue(MetadataKeys.InternalRegisteredPropertyKey, out var registered)
? (EventHandler<IComponentRegistration>?)registered
: null;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private EventHandler<IRegistrationSource>? GetRegistrationSourceAdded() =>
_properties.TryGetValue(MetadataKeys.InternalRegistrationSourceAddedPropertyKey, out var registrationSourceAdded)
? (EventHandler<IRegistrationSource>?)registrationSourceAdded
: null;
}
}