-
-
Notifications
You must be signed in to change notification settings - Fork 838
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #730: Added MatchingLifetimeScopeTags() extension to IComponent…
…Registration to get scope tags.
- Loading branch information
Showing
3 changed files
with
131 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Autofac.Core.Lifetime; | ||
|
||
namespace Autofac.Core | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="IComponentRegistration"/>. | ||
/// </summary> | ||
public static class ComponentRegistrationExtensions | ||
{ | ||
/// <summary> | ||
/// For components registered instance-per-matching-lifetime-scope, retrieves the set | ||
/// of lifetime scope tags to match. | ||
/// </summary> | ||
/// <param name="registration"> | ||
/// The <see cref="IComponentRegistration"/> to query for matching lifetime scope tags. | ||
/// </param> | ||
/// <returns> | ||
/// If the component is registered instance-per-matching-lifetime-scope, this method returns | ||
/// the set of matching lifetime scope tags. If the component is singleton, instance-per-scope, | ||
/// instance-per-dependency, or otherwise not an instance-per-matching-lifetime-scope | ||
/// component, this method returns an empty enumeration. | ||
/// </returns> | ||
public static IEnumerable<object> MatchingLifetimeScopeTags(this IComponentRegistration registration) | ||
{ | ||
if (registration == null) | ||
{ | ||
throw new ArgumentNullException(nameof(registration)); | ||
} | ||
|
||
var lifetime = registration.Lifetime as MatchingScopeLifetime; | ||
if (lifetime != null) | ||
{ | ||
return lifetime.TagsToMatch; | ||
} | ||
|
||
return Enumerable.Empty<object>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
test/Autofac.Test/Core/ComponentRegistrationExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using Autofac.Core; | ||
using Xunit; | ||
|
||
namespace Autofac.Test.Core | ||
{ | ||
public class ComponentRegistrationExtensionsTests | ||
{ | ||
[Fact] | ||
public void MatchingLifetimeScopeTags_InstancePerDependency() | ||
{ | ||
var b = new ContainerBuilder(); | ||
b.RegisterType<DivideByZeroException>().InstancePerDependency(); | ||
var c = b.Build(); | ||
Assert.Empty(c.RegistrationFor<DivideByZeroException>().MatchingLifetimeScopeTags()); | ||
} | ||
|
||
[Fact] | ||
public void MatchingLifetimeScopeTags_InstancePerLifetimeScope() | ||
{ | ||
var b = new ContainerBuilder(); | ||
b.RegisterType<DivideByZeroException>().InstancePerLifetimeScope(); | ||
var c = b.Build(); | ||
Assert.Empty(c.RegistrationFor<DivideByZeroException>().MatchingLifetimeScopeTags()); | ||
} | ||
|
||
[Fact] | ||
public void MatchingLifetimeScopeTags_InstancePerMatchingLifetimeScope_Multiple() | ||
{ | ||
var b = new ContainerBuilder(); | ||
b.RegisterType<DivideByZeroException>().InstancePerMatchingLifetimeScope("tag1", "tag2"); | ||
var c = b.Build(); | ||
Assert.Equal(new object[] { "tag1", "tag2" }, c.RegistrationFor<DivideByZeroException>().MatchingLifetimeScopeTags()); | ||
} | ||
|
||
[Fact] | ||
public void MatchingLifetimeScopeTags_InstancePerMatchingLifetimeScope_Single() | ||
{ | ||
var b = new ContainerBuilder(); | ||
b.RegisterType<DivideByZeroException>().InstancePerMatchingLifetimeScope("tag1"); | ||
var c = b.Build(); | ||
Assert.Equal(new object[] { "tag1" }, c.RegistrationFor<DivideByZeroException>().MatchingLifetimeScopeTags()); | ||
} | ||
|
||
[Fact] | ||
public void MatchingLifetimeScopeTags_NullRegistration() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => ComponentRegistrationExtensions.MatchingLifetimeScopeTags(null)); | ||
} | ||
|
||
[Fact] | ||
public void MatchingLifetimeScopeTags_Singleton() | ||
{ | ||
var b = new ContainerBuilder(); | ||
b.RegisterType<DivideByZeroException>().SingleInstance(); | ||
var c = b.Build(); | ||
Assert.Empty(c.RegistrationFor<DivideByZeroException>().MatchingLifetimeScopeTags()); | ||
} | ||
} | ||
} |