Skip to content

Commit

Permalink
Added multi interface syntax for rebind.
Browse files Browse the repository at this point in the history
  • Loading branch information
remogloor committed Dec 21, 2011
1 parent 3a1a16d commit afeb277
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-------------------------------------------------------------------------------
// <copyright file="InterfaceSegregationWithFourServicesTests.cs" company="Ninject Project Contributors">
// <copyright file="InterfaceSegregationWithThreeServicesTests.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Remo Gloor (remo.gloor@gmail.com)
//
Expand All @@ -23,17 +23,19 @@
namespace Ninject.Tests.Integration
{
using System;
using System.Linq;

using FluentAssertions;

using Ninject.Activation;
using Ninject.Tests.Fakes;
using Xunit;

public class InterfaceSegregationWithThreerServicesTests : IDisposable
public class InterfaceSegregationWithFourServicesTests : IDisposable
{
private readonly StandardKernel kernel;

public InterfaceSegregationWithThreerServicesTests()
public InterfaceSegregationWithFourServicesTests()
{
this.kernel = new StandardKernel();
}
Expand All @@ -46,59 +48,75 @@ public void Dispose()
[Fact]
public void MultipleServicesBoundWithGenericToReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().To(typeof(Monk)).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To(typeof(Monk)).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToConstantReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToConstant(new Monk()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToConstant(new Monk()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToMethodReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToMethod(ctx => new Monk()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToMethod(ctx => new Monk()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToProviderReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider(new MonkProvider()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider(new MonkProvider()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithGenericToProviderReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider<MonkProvider>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider<MonkProvider>().InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void Rebind()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope();
this.kernel.Rebind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope();

this.kernel.GetBindings(typeof(IWarrior)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(ICleric)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(IHuman)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(ILifeform)).Count().Should().Be(1);
this.VerifyAllInterfacesAreSameInstance();
}

private void VerifyAllInterfacesAreSameInstance()
{
var warrior = this.kernel.Get<IWarrior>();
var cleric = this.kernel.Get<ICleric>();
var human = this.kernel.Get<IHuman>();
var lifeform = this.kernel.Get<ILifeform>();

warrior.Should().BeSameAs(cleric);
human.Should().BeSameAs(cleric);
lifeform.Should().BeSameAs(cleric);
}

public class MonkProvider : Provider<Monk>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-------------------------------------------------------------------------------
// <copyright file="InterfaceSegregationWithThreeServicesTests.cs" company="Ninject Project Contributors">
// <copyright file="InterfaceSegregationWithFourServicesTests.cs" company="Ninject Project Contributors">
// Copyright (c) 2009-2011 Ninject Project Contributors
// Authors: Remo Gloor (remo.gloor@gmail.com)
//
Expand All @@ -23,17 +23,19 @@
namespace Ninject.Tests.Integration
{
using System;
using System.Linq;

using FluentAssertions;

using Ninject.Activation;
using Ninject.Tests.Fakes;
using Xunit;

public class InterfaceSegregationWithFourServicesTests : IDisposable
public class InterfaceSegregationWithThreerServicesTests : IDisposable
{
private readonly StandardKernel kernel;

public InterfaceSegregationWithFourServicesTests()
public InterfaceSegregationWithThreerServicesTests()
{
this.kernel = new StandardKernel();
}
Expand All @@ -46,48 +48,61 @@ public void Dispose()
[Fact]
public void MultipleServicesBoundWithGenericToReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To(typeof(Monk)).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().To(typeof(Monk)).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToConstantReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToConstant(new Monk()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToConstant(new Monk()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToMethodReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToMethod(ctx => new Monk()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToMethod(ctx => new Monk()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithToProviderReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider(new MonkProvider()).InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider(new MonkProvider()).InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void MultipleServicesBoundWithGenericToProviderReturnSameInstance()
{
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider<MonkProvider>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider<MonkProvider>().InSingletonScope();

this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void Rebind()
{
this.kernel.Bind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope();
this.kernel.Rebind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope();

this.kernel.GetBindings(typeof(IWarrior)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(ICleric)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(IHuman)).Count().Should().Be(1);
this.VerifyAllInterfacesAreSameInstance();
}

Expand All @@ -96,11 +111,9 @@ private void VerifyAllInterfacesAreSameInstance()
var warrior = this.kernel.Get<IWarrior>();
var cleric = this.kernel.Get<ICleric>();
var human = this.kernel.Get<IHuman>();
var lifeform = this.kernel.Get<ILifeform>();

warrior.Should().BeSameAs(cleric);
human.Should().BeSameAs(cleric);
lifeform.Should().BeSameAs(cleric);
}

public class MonkProvider : Provider<Monk>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
namespace Ninject.Tests.Integration
{
using System;
using System.Linq;

using FluentAssertions;

using Ninject.Activation;
Expand Down Expand Up @@ -91,6 +93,18 @@ public void MultipleServicesBoundWithGenericToProviderReturnSameInstance()
this.VerifyAllInterfacesAreSameInstance();
}

[Fact]
public void Rebind()
{
this.kernel.Bind<IWarrior, ICleric>().To<Monk>().InSingletonScope();
this.kernel.Bind<IWarrior, ICleric>().To<Monk>().InSingletonScope();
this.kernel.Rebind<IWarrior, ICleric>().To<Monk>().InSingletonScope();

this.kernel.GetBindings(typeof(IWarrior)).Count().Should().Be(1);
this.kernel.GetBindings(typeof(ICleric)).Count().Should().Be(1);
this.VerifyAllInterfacesAreSameInstance();
}

private void VerifyAllInterfacesAreSameInstance()
{
var warrior = this.kernel.Get<IWarrior>();
Expand Down
2 changes: 1 addition & 1 deletion src/Ninject.Test/Ninject.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
<Compile Include="Integration\ConstantTests.cs" />
<Compile Include="Fakes\ResolveCountingProvider.cs" />
<Compile Include="Integration\InterfaceSegregationWithTwoServicesTests.cs" />
<Compile Include="Integration\InterfaceSegregationWithThreeServicesTests.cs" />
<Compile Include="Integration\InterfaceSegregationWithFourServicesTests.cs" />
<Compile Include="Integration\InterfaceSegregationWithThreeServicesTests.cs" />
<Compile Include="Integration\DefaultParameterTests.cs" />
<Compile Include="Integration\EnumerableDependenciesTests\ConstrainedDependenciesContext.cs" />
<Compile Include="Integration\EnumerableDependenciesTests\Fakes\ChildB.cs" />
Expand Down
65 changes: 57 additions & 8 deletions src/Ninject/Syntax/BindingRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,72 @@ public void Unbind<T>()
/// <summary>
/// Removes any existing bindings for the specified service, and declares a new one.
/// </summary>
/// <typeparam name="T">The service to re-bind.</typeparam>
/// <typeparam name="T1">The first service to re-bind.</typeparam>
/// <returns>The fluent syntax</returns>
public IBindingToSyntax<T> Rebind<T>()
public IBindingToSyntax<T1> Rebind<T1>()
{
Unbind<T>();
return Bind<T>();
Unbind<T1>();
return Bind<T1>();
}

/// <summary>
/// Removes any existing bindings for the specified services, and declares a new one.
/// </summary>
/// <typeparam name="T1">The first service to re-bind.</typeparam>
/// <typeparam name="T2">The second service to re-bind.</typeparam>
/// <returns>The fluent syntax.</returns>
public IBindingToSyntax<T1, T2> Rebind<T1, T2>()
{
Unbind<T1>();
Unbind<T2>();
return Bind<T1, T2>();
}

/// <summary>
/// Removes any existing bindings for the specified services, and declares a new one.
/// </summary>
/// <typeparam name="T1">The first service to re-bind.</typeparam>
/// <typeparam name="T2">The second service to re-bind.</typeparam>
/// <typeparam name="T3">The third service to re-bind.</typeparam>
/// <returns>The fluent syntax.</returns>
public IBindingToSyntax<T1, T2, T3> Rebind<T1, T2, T3>()
{
Unbind<T1>();
Unbind<T2>();
Unbind<T3>();
return Bind<T1, T2, T3>();
}

/// <summary>
/// Removes any existing bindings for the specified services, and declares a new one.
/// </summary>
/// <typeparam name="T1">The first service to re-bind.</typeparam>
/// <typeparam name="T2">The second service to re-bind.</typeparam>
/// <typeparam name="T3">The third service to re-bind.</typeparam>
/// <typeparam name="T4">The fourth service to re-bind.</typeparam>
/// <returns>The fluent syntax.</returns>
public IBindingToSyntax<T1, T2, T3, T4> Rebind<T1, T2, T3, T4>()
{
Unbind<T1>();
Unbind<T2>();
Unbind<T3>();
Unbind<T4>();
return Bind<T1, T2, T3, T4>();
}

/// <summary>
/// Removes any existing bindings for the specified service, and declares a new one.
/// </summary>
/// <param name="service">The service to re-bind.</param>
/// <param name="services">The services to re-bind.</param>
/// <returns>The fluent syntax</returns>
public IBindingToSyntax<object> Rebind(Type service)
public IBindingToSyntax<object> Rebind(params Type[] services)
{
Unbind(service);
return Bind(service);
foreach (var service in services)
{
Unbind(service);
}

return Bind(services);
}

/// <summary>
Expand Down
Loading

0 comments on commit afeb277

Please sign in to comment.