forked from ninject/Ninject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added fluent syntax to define multiple interfaces for one implementat…
…ion.
- Loading branch information
Showing
35 changed files
with
2,475 additions
and
764 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
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,15 @@ | ||
namespace Ninject.Tests.Fakes | ||
{ | ||
public interface ICleric | ||
{ | ||
void Pray(); | ||
} | ||
|
||
public interface IHuman | ||
{ | ||
} | ||
|
||
public interface ILifeform | ||
{ | ||
} | ||
} |
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
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
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
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
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
src/Ninject.Test/Integration/InterfaceSegregationWithFourServicesTests.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,90 @@ | ||
namespace Ninject.Tests.Integration | ||
{ | ||
using System; | ||
using FluentAssertions; | ||
|
||
using Ninject.Activation; | ||
using Ninject.Tests.Fakes; | ||
using Xunit; | ||
|
||
public class InterfaceSegregationWithThreerServicesTests : IDisposable | ||
{ | ||
private readonly StandardKernel kernel; | ||
|
||
public InterfaceSegregationWithThreerServicesTests() | ||
{ | ||
this.kernel = new StandardKernel(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.kernel.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().To<Monk>().InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().To(typeof(Monk)).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToConstantReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToConstant(new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToMethodReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToMethod(ctx => new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider(new MonkProvider()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman>().ToProvider<MonkProvider>().InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
private void VerifyAllInterfacesAreSameInstance() | ||
{ | ||
var warrior = this.kernel.Get<IWarrior>(); | ||
var cleric = this.kernel.Get<ICleric>(); | ||
var human = this.kernel.Get<IHuman>(); | ||
|
||
warrior.Should().BeSameAs(cleric); | ||
human.Should().BeSameAs(cleric); | ||
} | ||
|
||
public class MonkProvider : Provider<Monk> | ||
{ | ||
protected override Monk CreateInstance(IContext context) | ||
{ | ||
return new Monk(); | ||
} | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/Ninject.Test/Integration/InterfaceSegregationWithThreeServicesTests.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,92 @@ | ||
namespace Ninject.Tests.Integration | ||
{ | ||
using System; | ||
using FluentAssertions; | ||
|
||
using Ninject.Activation; | ||
using Ninject.Tests.Fakes; | ||
using Xunit; | ||
|
||
public class InterfaceSegregationWithFourServicesTests : IDisposable | ||
{ | ||
private readonly StandardKernel kernel; | ||
|
||
public InterfaceSegregationWithFourServicesTests() | ||
{ | ||
this.kernel = new StandardKernel(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.kernel.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To<Monk>().InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().To(typeof(Monk)).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToConstantReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToConstant(new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToMethodReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToMethod(ctx => new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider(new MonkProvider()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric, IHuman, ILifeform>().ToProvider<MonkProvider>().InSingletonScope(); | ||
|
||
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> | ||
{ | ||
protected override Monk CreateInstance(IContext context) | ||
{ | ||
return new Monk(); | ||
} | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/Ninject.Test/Integration/InterfaceSegregationWithTwoServicesTests.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,88 @@ | ||
namespace Ninject.Tests.Integration | ||
{ | ||
using System; | ||
using FluentAssertions; | ||
|
||
using Ninject.Activation; | ||
using Ninject.Tests.Fakes; | ||
using Xunit; | ||
|
||
public class InterfaceSegregationWithTwoServicesTests : IDisposable | ||
{ | ||
private readonly StandardKernel kernel; | ||
|
||
public InterfaceSegregationWithTwoServicesTests() | ||
{ | ||
this.kernel = new StandardKernel(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.kernel.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().To<Monk>().InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().To(typeof(Monk)).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToConstantReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().ToConstant(new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToMethodReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().ToMethod(ctx => new Monk()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().ToProvider(new MonkProvider()).InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
[Fact] | ||
public void MultipleServicesBoundWithGenericToProviderReturnSameInstance() | ||
{ | ||
this.kernel.Bind<IWarrior, ICleric>().ToProvider<MonkProvider>().InSingletonScope(); | ||
|
||
this.VerifyAllInterfacesAreSameInstance(); | ||
} | ||
|
||
private void VerifyAllInterfacesAreSameInstance() | ||
{ | ||
var warrior = this.kernel.Get<IWarrior>(); | ||
var cleric = this.kernel.Get<ICleric>(); | ||
|
||
warrior.Should().BeSameAs(cleric); | ||
} | ||
|
||
public class MonkProvider : Provider<Monk> | ||
{ | ||
protected override Monk CreateInstance(IContext context) | ||
{ | ||
return new Monk(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.