Skip to content

Commit

Permalink
Fixed #847: ScopeRestrictedRegistry now also applies ComponentRegistr…
Browse files Browse the repository at this point in the history
…ationLifetimeDecorator to registrations from registration sources.
  • Loading branch information
alexmg committed Jul 2, 2017
1 parent a12b3ab commit a88128e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Autofac/Core/Registration/ComponentRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void Register(IComponentRegistration registration)
/// <param name="registration">The component registration.</param>
/// <param name="preserveDefaults">If true, existing defaults for the services provided by the
/// component will not be changed.</param>
public virtual void Register(IComponentRegistration registration, bool preserveDefaults)
public void Register(IComponentRegistration registration, bool preserveDefaults)
{
if (registration == null) throw new ArgumentNullException(nameof(registration));

Expand Down Expand Up @@ -187,7 +187,7 @@ private void UpdateInitialisedAdapters(IComponentRegistration registration)
AddRegistration(adapter, true, true);
}

private void AddRegistration(IComponentRegistration registration, bool preserveDefaults, bool originatedFromSource = false)
protected virtual void AddRegistration(IComponentRegistration registration, bool preserveDefaults, bool originatedFromSource = false)
{
foreach (var service in registration.Services)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Autofac/Core/Registration/ScopeRestrictedRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal ScopeRestrictedRegistry(object scopeTag, IDictionary<string, object> pr
_restrictedRootScopeLifetime = new MatchingScopeLifetime(scopeTag);
}

public override void Register(IComponentRegistration registration, bool preserveDefaults)
protected override void AddRegistration(IComponentRegistration registration, bool preserveDefaults, bool originatedFromSource = false)
{
if (registration == null) throw new ArgumentNullException(nameof(registration));

Expand All @@ -52,7 +52,7 @@ public override void Register(IComponentRegistration registration, bool preserve
if (registration.Lifetime is RootScopeLifetime)
toRegister = new ComponentRegistrationLifetimeDecorator(registration, _restrictedRootScopeLifetime);

base.Register(toRegister, preserveDefaults);
base.AddRegistration(toRegister, preserveDefaults, originatedFromSource);
}
}
}
12 changes: 12 additions & 0 deletions test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using Autofac.Core;
using Autofac.Core.Lifetime;
using Autofac.Core.Registration;
using Autofac.Test.Scenarios.RegistrationSources;
using Xunit;

Expand Down Expand Up @@ -241,6 +242,17 @@ public void SingletonsRegisteredInNestedScopeAreTiedToThatScope()
Assert.True(dt.IsDisposed);
}

[Fact]
public void LifetimeScopeCreatedWithAdditionalRegistrationsUsesScopeRestrictedRegistry()
{
var rootScope = new ContainerBuilder().Build();

var nestedScope = rootScope.BeginLifetimeScope(cb =>
cb.RegisterType<object>().SingleInstance());

Assert.IsType<ScopeRestrictedRegistry>(nestedScope.ComponentRegistry);
}

[Fact]
public void AdaptersInNestedScopeOverrideAdaptersInParent()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Core.Registration;
using Xunit;

namespace Autofac.Test.Core.Registration
{
public sealed class ScopeRestrictedRegistryTests
{
private static readonly IComponentRegistration ObjectRegistration =
RegistrationBuilder.ForType<object>().SingleInstance().CreateRegistration();

private class ObjectRegistrationSource : IRegistrationSource
{
public IEnumerable<IComponentRegistration> RegistrationsFor(
Service service,
Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
yield return ObjectRegistration;
}

public bool IsAdapterForIndividualComponents => false;
}

[Fact]
public void SingletonsFromRegistrationSourceAreWrappedWithLifetimeDecorator()
{
var registry = new ScopeRestrictedRegistry(new object(), new Dictionary<string, object>());

registry.AddRegistrationSource(new ObjectRegistrationSource());

var typedService = new TypedService(typeof(object));
registry.TryGetRegistration(typedService, out IComponentRegistration registration);

Assert.IsType<ComponentRegistrationLifetimeDecorator>(registration);
}

[Fact]
public void SingletonsRegisteredDirectlyAreWrappedWithLifetimeDecorator()
{
var registry = new ScopeRestrictedRegistry(new object(), new Dictionary<string, object>());

registry.Register(ObjectRegistration);

var typedService = new TypedService(typeof(object));
registry.TryGetRegistration(typedService, out IComponentRegistration registration);

Assert.IsType<ComponentRegistrationLifetimeDecorator>(registration);
}
}
}

0 comments on commit a88128e

Please # to comment.