@ -61,21 +61,30 @@ namespace Test.LightweightIocContainer
{
{
ITest Create ( ) ;
ITest Create ( ) ;
ITest Create ( string name ) ;
ITest Create ( string name ) ;
ITest Create ( MultitonScope scope ) ;
ITest CreateTest ( string name = null ) ;
ITest CreateTest ( string name = null ) ;
ITest Create ( byte id ) ;
ITest Create ( byte id ) ;
void ClearMultitonInstance < T > ( ) ;
}
}
private class TestFactory : ITestFactory
private class TestFactory : ITestFactory
{
{
public ITest Create ( ) = > new Test ( ) ;
public ITest Create ( ) = > new Test ( ) ;
public ITest Create ( string name ) = > throw new System . NotImplementedException ( ) ;
public ITest Create ( string name ) = > throw new System . NotImplementedException ( ) ;
public ITest Create ( MultitonScope scope ) = > throw new System . NotImplementedException ( ) ;
public ITest CreateTest ( string name = null ) = > throw new System . NotImplementedException ( ) ;
public ITest CreateTest ( string name = null ) = > throw new System . NotImplementedException ( ) ;
public ITest Create ( byte id ) = > throw new System . NotImplementedException ( ) ;
public ITest Create ( byte id ) = > throw new System . NotImplementedException ( ) ;
public void ClearMultitonInstance < T > ( ) = > throw new System . NotImplementedException ( ) ;
}
[UsedImplicitly]
public interface IMultitonTestFactory
{
ITest Create ( MultitonScope scope ) ;
void ClearMultitonInstance < T > ( ) ;
}
[UsedImplicitly]
public interface IInvalidMultitonTestFactory
{
ITest Create ( MultitonScope scope ) ;
ITest Create ( int number ) ;
}
}
[UsedImplicitly]
[UsedImplicitly]
@ -188,12 +197,12 @@ namespace Test.LightweightIocContainer
[Test]
[Test]
public void TestResolveMultitonFromFactory ( )
public void TestResolveMultitonFromFactory ( )
{
{
_ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < ITestFactory > ( ) ) ;
_ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < IMultiton TestFactory > ( ) ) ;
MultitonScope scope1 = new ( ) ;
MultitonScope scope1 = new ( ) ;
MultitonScope scope2 = new ( ) ;
MultitonScope scope2 = new ( ) ;
ITestFactory testFactory = _ iocContainer . Resolve < ITestFactory > ( ) ;
IMultiton TestFactory testFactory = _ iocContainer . Resolve < IMultiton TestFactory > ( ) ;
ITest resolvedTest1 = testFactory . Create ( scope1 ) ;
ITest resolvedTest1 = testFactory . Create ( scope1 ) ;
ITest resolvedTest2 = testFactory . Create ( scope1 ) ;
ITest resolvedTest2 = testFactory . Create ( scope1 ) ;
@ -207,12 +216,12 @@ namespace Test.LightweightIocContainer
[Test]
[Test]
public void TestResolveMultitonFromFactoryClearInstances ( )
public void TestResolveMultitonFromFactoryClearInstances ( )
{
{
_ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < ITestFactory > ( ) ) ;
_ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < IMultiton TestFactory > ( ) ) ;
MultitonScope scope1 = new ( ) ;
MultitonScope scope1 = new ( ) ;
MultitonScope scope2 = new ( ) ;
MultitonScope scope2 = new ( ) ;
ITestFactory testFactory = _ iocContainer . Resolve < ITestFactory > ( ) ;
IMultiton TestFactory testFactory = _ iocContainer . Resolve < IMultiton TestFactory > ( ) ;
ITest resolvedTest1 = testFactory . Create ( scope1 ) ;
ITest resolvedTest1 = testFactory . Create ( scope1 ) ;
ITest resolvedTest2 = testFactory . Create ( scope1 ) ;
ITest resolvedTest2 = testFactory . Create ( scope1 ) ;
@ -232,6 +241,14 @@ namespace Test.LightweightIocContainer
Assert . AreNotSame ( resolvedTest3 , resolvedTest5 ) ;
Assert . AreNotSame ( resolvedTest3 , resolvedTest5 ) ;
}
}
[Test]
public void InvalidMultitonFactoryRegistrationFactoryWithoutParameter ( ) = >
Assert . Throws < InvalidFactoryRegistrationException > ( ( ) = > _ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < ITestFactory > ( ) ) ) ;
[Test]
public void InvalidMultitonFactoryRegistrationFactoryWithoutScopeAsFirstParameter ( ) = >
Assert . Throws < InvalidFactoryRegistrationException > ( ( ) = > _ iocContainer . Register ( r = > r . AddMultiton < ITest , Test , MultitonScope > ( ) . WithFactory < IInvalidMultitonTestFactory > ( ) ) ) ;
[Test]
[Test]
public void TestInvalidCreateMethodReturnType ( ) = >
public void TestInvalidCreateMethodReturnType ( ) = >
Assert . Throws < InvalidFactoryRegistrationException > ( ( ) = > _ iocContainer . Register ( r = > r . Add < ITest , Test > ( ) . WithFactory < ITestFactoryWrongReturn > ( ) ) ) ;
Assert . Throws < InvalidFactoryRegistrationException > ( ( ) = > _ iocContainer . Register ( r = > r . Add < ITest , Test > ( ) . WithFactory < ITestFactoryWrongReturn > ( ) ) ) ;