Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for delegate factories with singleton, multi-instance and custom lifetimes #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/TinyIoC.Tests/TestData/BasicClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,27 @@ namespace TinyIoC.Tests.TestData
{
namespace BasicClasses
{

internal class CyclicA
{
readonly CyclicB dependency;

public CyclicA(CyclicB dependency)
{
this.dependency = dependency;
}
}

internal class CyclicB
{
readonly CyclicA dependency;

public CyclicB(CyclicA dependency)
{
this.dependency = dependency;
}
}

internal interface ITestInterface
{
}
Expand Down
25 changes: 25 additions & 0 deletions src/TinyIoC.Tests/TinyIoCFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ namespace TinyIoC.Tests
[TestClass]
public class TinyIoCFunctionalTests
{

[TestMethod]
public void Container_Throws_Whith_Simple_Cyclic_Dependency()
{
var container = UtilityMethods.GetContainer();
container.Register<CyclicA>();
container.Register<CyclicB>();

AssertHelper.ThrowsException<TinyIoCResolutionException>(() => container.Resolve<CyclicA>());

}


[TestMethod]
public void Container_Throws_Whith_Simple_Factory_Cyclic_Dependency()
{
var container = UtilityMethods.GetContainer();
container.Register<CyclicA>((c, o) => new CyclicA(c.Resolve<CyclicB>()));
container.Register<CyclicB>((c, o) => new CyclicB(c.Resolve<CyclicA>()));

AssertHelper.ThrowsException<TinyIoCResolutionException>(() => container.Resolve<CyclicA>());

}


[TestMethod]
public void NestedInterfaceDependencies_CorrectlyRegistered_ResolvesRoot()
{
Expand Down
25 changes: 14 additions & 11 deletions src/TinyIoC.Tests/TinyIoCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,25 +1031,28 @@ public void Register_SingletonWithWeakReference_Throws()
}

[TestMethod]
//[ExpectedException(typeof(TinyIoCRegistrationException))]
public void Register_FactoryToSingletonFluent_ThrowsException()
public void Register_FactoryToSingletonFluent_Registers()
{
var container = UtilityMethods.GetContainer();
AssertHelper.ThrowsException<TinyIoCRegistrationException>(() => container.Register<TestClassDefaultCtor>((c, p) => new TestClassDefaultCtor()).AsSingleton());

// Should have thrown by now
//Assert.IsTrue(false);
container.Register<TestClassDefaultCtor>((c, p) => new TestClassDefaultCtor()).AsSingleton();
//works
}

[TestMethod]
//[ExpectedException(typeof(TinyIoCRegistrationException))]
public void Register_FactoryToMultiInstanceFluent_ThrowsException()
public void Register_FactoryToMultiInstanceFluent_Registers()
{
var container = UtilityMethods.GetContainer();
AssertHelper.ThrowsException<TinyIoCRegistrationException>(() => container.Register<TestClassDefaultCtor>((c, p) => new TestClassDefaultCtor()).AsMultiInstance());
container.Register<TestClassDefaultCtor>((c, p) => new TestClassDefaultCtor()).AsMultiInstance();

// Should have thrown by now
//Assert.IsTrue(false);
//works
}

[TestMethod]
public void Register_FactoryToCustomLifetime_Registers()
{
var container = UtilityMethods.GetContainer();
container.Register<TestClassDefaultCtor>((c, p) => new TestClassDefaultCtor()).AsPerRequestSingleton();
//works
}

[TestMethod]
Expand Down
Loading