Skip to content

Commit 639a3b8

Browse files
committed
Merge pull request #16 from MRCollective/keyed-registrations
Keyed registration support
2 parents dcf7209 + f8eea73 commit 639a3b8

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

AutofacContrib.NSubstitute.Tests/AutofacContrib.NSubstitute.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
</ItemGroup>
5656
<ItemGroup>
5757
<Compile Include="AutoSubstituteFixture.cs" />
58+
<Compile Include="KeyedRegistrationFixture.cs" />
5859
<Compile Include="Properties\AssemblyInfo.cs" />
5960
<Compile Include="ExampleFixture.cs" />
6061
</ItemGroup>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using Autofac.Features.Indexed;
2+
using NSubstitute;
3+
using NUnit.Framework;
4+
5+
namespace AutofacContrib.NSubstitute.Tests
6+
{
7+
public enum Switch
8+
{
9+
Off,
10+
On
11+
}
12+
13+
public class ClassWithKeyedDependencies
14+
{
15+
public ClassWithKeyedDependencies(IIndex<Switch, IDependency2> dependencies)
16+
{
17+
OnDependency = dependencies[Switch.On];
18+
OffDependency = dependencies[Switch.Off];
19+
}
20+
21+
public IDependency2 OnDependency { get; private set; }
22+
public IDependency2 OffDependency { get; private set; }
23+
}
24+
25+
public static class KeyedRegistrationFixture
26+
{
27+
[Test]
28+
public static void ShouldResolveIndexedDependencies()
29+
{
30+
var autoSubstitute = new AutoSubstitute();
31+
32+
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();
33+
34+
Assert.NotNull(target.OnDependency);
35+
Assert.NotNull(target.OffDependency);
36+
}
37+
38+
[Test]
39+
public static void ShouldResolveASubstituteForIndexedDependency()
40+
{
41+
var autoSubstitute = new AutoSubstitute();
42+
var index = autoSubstitute.Resolve<IIndex<Switch, IDependency2>>();
43+
index[Switch.On].SomeOtherMethod().Returns(5);
44+
45+
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();
46+
47+
Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
48+
}
49+
50+
[Test]
51+
public static void ShouldAcceptProvidedIndexedDependency()
52+
{
53+
var autoSubstitute = new AutoSubstitute();
54+
var substitute = Substitute.For<IDependency2>();
55+
substitute.SomeOtherMethod().Returns(5);
56+
autoSubstitute.Provide(substitute, Switch.On);
57+
58+
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();
59+
60+
Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
61+
}
62+
}
63+
}

AutofacContrib.NSubstitute/AutoSubstitute.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ public TService Provide<TService>(TService instance)
100100
return Container.Resolve<TService>();
101101
}
102102

103+
/// <summary>
104+
/// Register the specified object to the container as the specified keyed service type and resolve it.
105+
/// </summary>
106+
/// <typeparam name="TService">The type to register the object as</typeparam>
107+
/// <param name="instance">The object to register into the container</param>
108+
/// <param name="serviceKey">The key to register the service with</param>
109+
/// <returns>The instance resolved from container</returns>
110+
public TService Provide<TService>(TService instance, object serviceKey)
111+
where TService : class
112+
{
113+
Container.ComponentRegistry.Register(RegistrationBuilder.ForDelegate((c, p) => instance).As(new KeyedService(serviceKey, typeof(TService)))
114+
.InstancePerLifetimeScope().CreateRegistration()
115+
);
116+
117+
return Container.Resolve<TService>();
118+
}
119+
103120
/// <summary>
104121
/// Registers to the container and returns a substitute for a given concrete class given the explicit constructor parameters.
105122
/// This is used for concrete classes where NSubstitutes won't be created by default by the container when using Resolve.

AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using Autofac;
56
using Autofac.Builder;
67
using Autofac.Core;
8+
using Autofac.Features.Indexed;
79
using NSubstitute;
810

911
namespace AutofacContrib.NSubstitute
@@ -25,8 +27,8 @@ public IEnumerable<IComponentRegistration> RegistrationsFor
2527
{
2628
if (service == null)
2729
throw new ArgumentNullException("service");
28-
29-
var typedService = service as TypedService;
30+
31+
var typedService = service as IServiceWithType;
3032
if (typedService == null ||
3133
!typedService.ServiceType.IsInterface ||
3234
typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||

0 commit comments

Comments
 (0)