Skip to content

Commit

Permalink
Consolidated handling of isUnique parameter for TryGet (fixes ninject…
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-executor committed Dec 17, 2020
1 parent 3ff6bda commit 66c1cb4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
64 changes: 64 additions & 0 deletions src/Ninject.Test/Integration/ReadOnlyKernelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ public void TryGetOfT_Parameters_ReturnsNullIfMultipleBindingsExistForADependenc
bindings.Should().HaveCount(1);
}

[Fact]
public void TryGetOfT_NameAndParameters_ReturnsNullIfMultipleBindingsExistForResolvedService()
{
Configuration.Bind<IWarrior>().To<Samurai>().Named("a");
Configuration.Bind<IWarrior>().To<Ninja>().Named("a");
Configuration.Bind<IWeapon>().To<Sword>();

Kernel = Configuration.BuildReadOnlyKernel();

var warrior = Kernel.TryGet<IWarrior>("a", Array.Empty<IParameter>());
warrior.Should().BeNull();

var bindings = Kernel.GetBindings(typeof(IWarrior));
bindings.Should().HaveCount(2);
}

[Fact]
public void TryGetOfT_ConstraintAndParameters_ReturnsNullIfMultipleBindingsExistForResolvedService()
{
Configuration.Bind<IWarrior>().To<Samurai>();
Configuration.Bind<IWarrior>().To<Ninja>();
Configuration.Bind<IWeapon>().To<Sword>();

Kernel = Configuration.BuildReadOnlyKernel();

var warrior = Kernel.TryGet<IWarrior>((metadata) => true, Array.Empty<IParameter>());
warrior.Should().BeNull();

var bindings = Kernel.GetBindings(typeof(IWarrior));
bindings.Should().HaveCount(2);
}

[Fact]
public void TryGetOfT_Parameters_ReturnsNullIfOnlyUnmetConditionalBindingsExistForADependency()
{
Expand Down Expand Up @@ -194,6 +226,22 @@ public void TryGet_ServiceAndNameAndParameters_ReturnsNullIfMultipleBindingsExis
bindings.Should().HaveCount(1);
}

[Fact]
public void TryGet_ServiceAndNameAndParameters_ReturnsNullIfMultipleBindingsExistForResolvedService()
{
Configuration.Bind<IWarrior>().To<Samurai>().Named("a");
Configuration.Bind<IWarrior>().To<Ninja>().Named("a");
Configuration.Bind<IWeapon>().To<Sword>();

Kernel = Configuration.BuildReadOnlyKernel();

var warrior = Kernel.TryGet(typeof(IWarrior), "a", Array.Empty<IParameter>());
warrior.Should().BeNull();

var bindings = Kernel.GetBindings(typeof(IWarrior));
bindings.Should().HaveCount(2);
}

[Fact]
public void TryGet_ServiceAndNameAndParameters_ReturnsNullIfOnlyUnmetConditionalBindingsExistForADependency()
{
Expand Down Expand Up @@ -292,6 +340,22 @@ public void TryGet_ServiceAndConstraintAndParameters_ReturnsNullIfOnlyUnmetCondi
var bindings = Kernel.GetBindings(typeof(IWarrior));
bindings.Should().HaveCount(1);
}

[Fact]
public void TryGet_ServiceAndConstraintAndParameters_ReturnsNullIfMultipleBindingsExistForResolvedService()
{
Configuration.Bind<IWarrior>().To<Samurai>();
Configuration.Bind<IWarrior>().To<Samurai>();
Configuration.Bind<IWeapon>().To<Sword>();

Kernel = Configuration.BuildReadOnlyKernel();

var warrior = Kernel.TryGet(typeof(IWarrior), (metadata) => true, Array.Empty<IParameter>());
warrior.Should().BeNull();

var bindings = Kernel.GetBindings(typeof(IWarrior));
bindings.Should().HaveCount(2);
}
}

public class WhenTryGetIsCalledForServiceWithMultipleBindingsOfSameWeight : ReadOnlyKernelContext
Expand Down
4 changes: 2 additions & 2 deletions src/Ninject/Syntax/ResolutionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public static object TryGet(this IResolutionRoot root, Type service, params IPar
/// </returns>
public static object TryGet(this IResolutionRoot root, Type service, string name, params IParameter[] parameters)
{
return TryGet(() => ResolveSingle(root, service, b => b.Name == name, parameters, true, false));
return TryGet(() => ResolveSingle(root, service, b => b.Name == name, parameters, true, true));
}

/// <summary>
Expand All @@ -296,7 +296,7 @@ public static object TryGet(this IResolutionRoot root, Type service, string name
/// </returns>
public static object TryGet(this IResolutionRoot root, Type service, Func<IBindingMetadata, bool> constraint, params IParameter[] parameters)
{
return TryGet(() => ResolveSingle(root, service, constraint, parameters, true, false));
return TryGet(() => ResolveSingle(root, service, constraint, parameters, true, true));
}

/// <summary>
Expand Down

0 comments on commit 66c1cb4

Please # to comment.