Skip to content

Commit

Permalink
Merge pull request #16 from MRCollective/keyed-registrations
Browse files Browse the repository at this point in the history
Keyed registration support
  • Loading branch information
MattDavies committed May 28, 2014
2 parents dcf7209 + f8eea73 commit 639a3b8
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AutoSubstituteFixture.cs" />
<Compile Include="KeyedRegistrationFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ExampleFixture.cs" />
</ItemGroup>
Expand Down
63 changes: 63 additions & 0 deletions AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Autofac.Features.Indexed;
using NSubstitute;
using NUnit.Framework;

namespace AutofacContrib.NSubstitute.Tests
{
public enum Switch
{
Off,
On
}

public class ClassWithKeyedDependencies
{
public ClassWithKeyedDependencies(IIndex<Switch, IDependency2> dependencies)
{
OnDependency = dependencies[Switch.On];
OffDependency = dependencies[Switch.Off];
}

public IDependency2 OnDependency { get; private set; }
public IDependency2 OffDependency { get; private set; }
}

public static class KeyedRegistrationFixture
{
[Test]
public static void ShouldResolveIndexedDependencies()
{
var autoSubstitute = new AutoSubstitute();

var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();

Assert.NotNull(target.OnDependency);
Assert.NotNull(target.OffDependency);
}

[Test]
public static void ShouldResolveASubstituteForIndexedDependency()
{
var autoSubstitute = new AutoSubstitute();
var index = autoSubstitute.Resolve<IIndex<Switch, IDependency2>>();
index[Switch.On].SomeOtherMethod().Returns(5);

var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();

Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
}

[Test]
public static void ShouldAcceptProvidedIndexedDependency()
{
var autoSubstitute = new AutoSubstitute();
var substitute = Substitute.For<IDependency2>();
substitute.SomeOtherMethod().Returns(5);
autoSubstitute.Provide(substitute, Switch.On);

var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>();

Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
}
}
}
17 changes: 17 additions & 0 deletions AutofacContrib.NSubstitute/AutoSubstitute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ public TService Provide<TService>(TService instance)
return Container.Resolve<TService>();
}

/// <summary>
/// Register the specified object to the container as the specified keyed service type and resolve it.
/// </summary>
/// <typeparam name="TService">The type to register the object as</typeparam>
/// <param name="instance">The object to register into the container</param>
/// <param name="serviceKey">The key to register the service with</param>
/// <returns>The instance resolved from container</returns>
public TService Provide<TService>(TService instance, object serviceKey)
where TService : class
{
Container.ComponentRegistry.Register(RegistrationBuilder.ForDelegate((c, p) => instance).As(new KeyedService(serviceKey, typeof(TService)))
.InstancePerLifetimeScope().CreateRegistration()
);

return Container.Resolve<TService>();
}

/// <summary>
/// Registers to the container and returns a substitute for a given concrete class given the explicit constructor parameters.
/// This is used for concrete classes where NSubstitutes won't be created by default by the container when using Resolve.
Expand Down
6 changes: 4 additions & 2 deletions AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Autofac;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Features.Indexed;
using NSubstitute;

namespace AutofacContrib.NSubstitute
Expand All @@ -25,8 +27,8 @@ public IEnumerable<IComponentRegistration> RegistrationsFor
{
if (service == null)
throw new ArgumentNullException("service");

var typedService = service as TypedService;
var typedService = service as IServiceWithType;
if (typedService == null ||
!typedService.ServiceType.IsInterface ||
typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||
Expand Down

0 comments on commit 639a3b8

Please sign in to comment.