Skip to content

Commit

Permalink
Merge pull request #4 from AgeOfLearning/2.6.0_multicontract_bindings
Browse files Browse the repository at this point in the history
2.6.0 multicontract bindings
  • Loading branch information
leonidumanskiy authored Apr 25, 2019
2 parents 0916925 + eb53fce commit fce5e38
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 63 deletions.
240 changes: 240 additions & 0 deletions KrakenIoc/KrakenIoc.Testing/V1/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,36 @@ public void ThrowsExceptionWhenLateBoundTypeDoesNotImplementBinderType()

#region Test 2 - DoesResolveSingleton

#region Interface & Implemenation
internal interface ISomeInterface { }

internal interface IAnotherInterface { }

internal class SomeTypeImplementsTwoInterfaces : ISomeInterface, IAnotherInterface
{

}

internal interface ISomeTypeNine
{
ISomeInterface Some { get; }
IAnotherInterface Another { get; }
}

internal class SomeTypeNine : ISomeTypeNine
{
public ISomeInterface Some { get; set; }
public IAnotherInterface Another { get; set; }

public SomeTypeNine(ISomeInterface some, IAnotherInterface another)
{
Some = some;
Another = another;
}
}

#endregion

[TestMethod]
public void DoesResolveSingleton()
{
Expand All @@ -349,6 +379,33 @@ public void DoesResolveSingleton()

container = null;
}

[TestMethod]
public void DoesResolveSingletonWhenMultipleInterfacesBound()
{
Container container = new Container();

// Transient & Singleton binding...
container.Bind<ISomeTypeNine>().To<SomeTypeNine>();

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface)).To<SomeTypeImplementsTwoInterfaces>().AsSingleton();

// Resolve SomeTypeNine... should be injected
ISomeTypeNine consumer = container.Resolve<ISomeTypeNine>();

Assert.IsNotNull(consumer.Some, "did not inject ISomeInterface");
Assert.IsNotNull(consumer.Another, "did not inject IAnotherInterface");
Assert.AreEqual(consumer.Some, consumer.Another, "did not inject singleton");

// Resolve singleton
ISomeInterface some = container.Resolve<ISomeInterface>();
IAnotherInterface another = container.Resolve<IAnotherInterface>();

Assert.AreEqual(consumer.Some, some, "did not inject singleton");
Assert.AreEqual(consumer.Some, another, "did not inject singleton");

container = null;
}
#endregion

#region Test 3 - DoesNotAllowConstructorCircularDependency
Expand Down Expand Up @@ -722,6 +779,16 @@ interface ISomeTypeTest9Factory : IFactory<ISomeTypeTest9>
IInjectContext ItemInjectContext { get; }
}

interface ISomeTypeImplementsTwoInterfacesFactory : IFactory<SomeTypeImplementsTwoInterfaces> { }

interface ISomeTypeImplementsTwoInterfacesNonGenericFactory : IFactory { }

interface ISomeTypeTest9FactoryNonGeneric : IFactory
{
int NumCreated { get; }
IInjectContext ItemInjectContext { get; }
}

class SomeTypeTest9Factory : Factory<ISomeTypeTest9>, ISomeTypeTest9Factory
{
public int NumCreated { get; set; }
Expand All @@ -735,6 +802,45 @@ public override ISomeTypeTest9 Create(IInjectContext injectContext)
}
}

class SomeTypeImplementsTwoInterfacesFactory : Factory<SomeTypeImplementsTwoInterfaces>, ISomeTypeImplementsTwoInterfacesFactory
{
public int NumCreated { get; set; }
public IInjectContext ItemInjectContext { get; private set; }

public override SomeTypeImplementsTwoInterfaces Create(IInjectContext injectContext)
{
NumCreated++;
ItemInjectContext = injectContext;
return new SomeTypeImplementsTwoInterfaces();
}
}

class SomeTypeImplementsTwoInterfacesNonGenericFactory : ISomeTypeImplementsTwoInterfacesNonGenericFactory
{
public int NumCreated { get; set; }
public IInjectContext ItemInjectContext { get; private set; }

public object Create(IInjectContext injectContext)
{
NumCreated++;
ItemInjectContext = injectContext;
return new SomeTypeImplementsTwoInterfaces();
}
}

class SomeTypeTest9FactoryNonGeneric : ISomeTypeTest9FactoryNonGeneric
{
public int NumCreated { get; set; }
public IInjectContext ItemInjectContext { get; private set; }

public object Create(IInjectContext injectContext)
{
NumCreated++;
ItemInjectContext = injectContext;
return new SomeTypeTest9();
}
}

interface ISomeTypeTest9_B { }

class SomeTypeTest9_B : ISomeTypeTest9_B
Expand Down Expand Up @@ -776,6 +882,98 @@ public void DoesResolveFromFactoryAsSingleton()
Assert.AreEqual(1, factory.NumCreated, "did not resolve once"); // must not be 2
}

[TestMethod]
public void DoesResolveFromFactoryWhenMultipleInterfacesAreBoundAsTransient()
{
Container container = new Container();
var factory = new SomeTypeImplementsTwoInterfacesFactory();

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface))
.To<SomeTypeImplementsTwoInterfaces>()
.FromFactory<ISomeTypeImplementsTwoInterfacesFactory, SomeTypeImplementsTwoInterfaces>().AsTransient();

container.Bind<ISomeTypeImplementsTwoInterfacesFactory>(factory).To<SomeTypeImplementsTwoInterfacesFactory>().AsSingleton();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(2, factory.NumCreated, "did not resolve exactly twice using factory");
}

[TestMethod]
public void DoesResolveFromFactoryWhenMultipleInterfacesAreBoundAsSingleton()
{
Container container = new Container();
var factory = new SomeTypeImplementsTwoInterfacesFactory();

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface))
.To<SomeTypeImplementsTwoInterfaces>()
.FromFactory<ISomeTypeImplementsTwoInterfacesFactory, SomeTypeImplementsTwoInterfaces>().AsSingleton();

container.Bind<ISomeTypeImplementsTwoInterfacesFactory>(factory).To<SomeTypeImplementsTwoInterfacesFactory>().AsSingleton();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(1, factory.NumCreated, "did not resolve exactly once using factory");

Assert.AreEqual(someInterface, anotherInterface, "Resolved interfaces do not point to the same object reference");
}

[TestMethod]
public void DoesResolveFromNonGenericFactory()
{
Container container = new Container();
ISomeTypeTest9FactoryNonGeneric factory = new SomeTypeTest9FactoryNonGeneric();

container.Bind<ISomeTypeTest9>().To<SomeTypeTest9>().FromFactory<ISomeTypeTest9FactoryNonGeneric>().AsTransient();
container.Bind<ISomeTypeTest9FactoryNonGeneric>(factory).To<SomeTypeTest9FactoryNonGeneric>().AsSingleton();

ISomeTypeTest9 someTypeTest9 = container.Resolve<ISomeTypeTest9>();

Assert.AreEqual(1, factory.NumCreated, "did not resolve using factory");
}

[TestMethod]
public void DoesResolveFromNonGenericFactoryWhenMultipleInterfacesAreBoundAsTransient()
{
Container container = new Container();
var factory = new SomeTypeImplementsTwoInterfacesNonGenericFactory();

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface))
.To<SomeTypeImplementsTwoInterfaces>()
.FromFactory<ISomeTypeImplementsTwoInterfacesNonGenericFactory>()
.AsTransient();

container.Bind<ISomeTypeImplementsTwoInterfacesNonGenericFactory>(factory).To<SomeTypeImplementsTwoInterfacesNonGenericFactory>().AsSingleton();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(2, factory.NumCreated, "did not resolve exactly twice using factory");
}

[TestMethod]
public void DoesResolveFromNonGenericFactoryWhenMultipleInterfacesAreBoundAsSingleton()
{
Container container = new Container();
var factory = new SomeTypeImplementsTwoInterfacesNonGenericFactory();

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface))
.To<SomeTypeImplementsTwoInterfaces>()
.FromFactory<ISomeTypeImplementsTwoInterfacesNonGenericFactory>()
.AsSingleton();

container.Bind<ISomeTypeImplementsTwoInterfacesNonGenericFactory>(factory).To<SomeTypeImplementsTwoInterfacesNonGenericFactory>().AsSingleton();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(1, factory.NumCreated, "did not resolve exactly once using factory");

Assert.AreEqual(someInterface, anotherInterface, "Resolved interfaces do not point to the same object reference");
}

[TestMethod]
public void DoesResolveFromFactoryWithInheritedContainer()
{
Expand Down Expand Up @@ -819,6 +1017,48 @@ public void DoesResolveFromFactoryMethod()

Assert.AreEqual(2, numResolved, "did not resolve twice");
}

[TestMethod]
public void DoesResolveFromFactoryMethodWhenMultipleInterfacesAreBoundAsTransient()
{
Container container = new Container();

int numResolved = 0;

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface)).To<SomeTypeImplementsTwoInterfaces>().FromFactoryMethod(delegate (IInjectContext injectContext)
{
numResolved++;
return new SomeTypeImplementsTwoInterfaces();

}).AsTransient();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(2, numResolved, "did not resolve exactly twice");
Assert.AreNotEqual(someInterface, anotherInterface, "two objects should not be equal");
}

[TestMethod]
public void DoesResolveFromFactoryMethodWhenMultipleInterfacesAreBoundAsSingleton()
{
Container container = new Container();

int numResolved = 0;

container.Bind(typeof(ISomeInterface), typeof(IAnotherInterface)).To<SomeTypeImplementsTwoInterfaces>().FromFactoryMethod(delegate (IInjectContext injectContext)
{
numResolved++;
return new SomeTypeImplementsTwoInterfaces();

}).AsSingleton();

ISomeInterface someInterface = container.Resolve<ISomeInterface>();
IAnotherInterface anotherInterface = container.Resolve<IAnotherInterface>();

Assert.AreEqual(1, numResolved, "did not resolve exactly once");
Assert.AreEqual(someInterface, anotherInterface, "two objects should be equal");
}

[TestMethod]
public void DoesResolveFromFactoryMethodWithInheritedContainer()
Expand Down
Loading

0 comments on commit fce5e38

Please sign in to comment.