Skip to content

Commit

Permalink
Inherit Registered callback in child scopes (closes #218)
Browse files Browse the repository at this point in the history
  • Loading branch information
eugbaranov committed Oct 30, 2018
1 parent 426c3ee commit 997153d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Autofac/Builder/MetadataKeys.cs
Expand Up @@ -34,5 +34,7 @@ internal static class MetadataKeys
internal const string StartOnActivatePropertyKey = "__StartOnActivate";

internal const string ContainerBuildOptions = "__ContainerBuildOptions";

internal const string RegisteredPropertyKey = "__RegisteredKey";
}
}
32 changes: 28 additions & 4 deletions src/Autofac/Core/Registration/ComponentRegistry.cs
Expand Up @@ -207,8 +207,7 @@ protected virtual void AddRegistration(IComponentRegistration registration, bool

_registrations.Add(registration);

var handler = Registered;
handler?.Invoke(this, new ComponentRegisteredEventArgs(this, registration));
GetRegistered()?.Invoke(this, new ComponentRegisteredEventArgs(this, registration));
}

/// <summary>
Expand Down Expand Up @@ -266,7 +265,24 @@ public IEnumerable<IComponentRegistration> DecoratorsFor(IComponentRegistration
/// Fired whenever a component is registered - either explicitly or via a
/// <see cref="IRegistrationSource"/>.
/// </summary>
public event EventHandler<ComponentRegisteredEventArgs> Registered;
public event EventHandler<ComponentRegisteredEventArgs> Registered
{
add
{
lock (_synchRoot)
{
Properties[MetadataKeys.RegisteredPropertyKey] = GetRegistered() + value;
}
}

remove
{
lock (_synchRoot)
{
Properties[MetadataKeys.RegisteredPropertyKey] = GetRegistered() - value;
}
}
}

/// <summary>
/// Add a registration source that will provide registrations on-the-fly.
Expand Down Expand Up @@ -360,5 +376,13 @@ private ServiceRegistrationInfo GetServiceInfo(Service service)
_serviceInfo.Add(service, info);
return info;
}

private EventHandler<ComponentRegisteredEventArgs> GetRegistered()
{
if (Properties.TryGetValue(MetadataKeys.RegisteredPropertyKey, out var registered))
return (EventHandler<ComponentRegisteredEventArgs>)registered;

return null;
}
}
}
}
36 changes: 36 additions & 0 deletions test/Autofac.Test/ModuleTests.cs
Expand Up @@ -59,6 +59,42 @@ public void AttachesToRegistrations()
Assert.Equal(container.ComponentRegistry.Registrations.Count(), attachingModule.Registrations.Count);
}

[Fact]
public void AttachesToRegistrationsInScope()
{
var attachingModule = new AttachingModule();
Assert.Equal(0, attachingModule.Registrations.Count);

var builder = new ContainerBuilder();
builder.RegisterModule(attachingModule);

using (var container = builder.Build())
using (var scope = container.BeginLifetimeScope(c => c.RegisterType(typeof(int))))
{
var expected = container.ComponentRegistry.Registrations.Count() + scope.ComponentRegistry.Registrations.Count();
Assert.Equal(expected, attachingModule.Registrations.Count);
}
}

[Fact]
public void AttachesToRegistrationsInNestedScope()
{
var attachingModule = new AttachingModule();
Assert.Equal(0, attachingModule.Registrations.Count);

var builder = new ContainerBuilder();
builder.RegisterModule(attachingModule);

using (var container = builder.Build())
using (var outerScope = container.BeginLifetimeScope(c => c.RegisterType(typeof(int))))
using (var innerScope = outerScope.BeginLifetimeScope(c => c.RegisterType(typeof(double))))
{
var expected = container.ComponentRegistry.Registrations.Count()
+ outerScope.ComponentRegistry.Registrations.Count() + innerScope.ComponentRegistry.Registrations.Count();
Assert.Equal(expected, attachingModule.Registrations.Count);
}
}

internal class ModuleExposingThisAssembly : Module
{
public Assembly ModuleThisAssembly
Expand Down

0 comments on commit 997153d

Please sign in to comment.