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

ToNamespace and CommanderContainerExtension bindings not resolving correctly when using multiple containers #73

Closed
FodderMK opened this issue Feb 8, 2017 · 2 comments
Assignees
Labels
Milestone

Comments

@FodderMK
Copy link
Contributor

FodderMK commented Feb 8, 2017

It looks like bindings that are obtained with ToNamespace() aren't working correctly when used in conjunction with RegisterCommands when there are multiple containers. The following example results in a InjectorException: There are no constructors on the type System.Int32. exception. If you comment out line 17 (this.containerTwo = ...) the code works correctly. Likewises, if I change line 46 to inject AdicMultipleContainerTestClassOne directly it works fine.

using Adic;
using Adic.Container;
using AdicMultipleContainerTest.Command;
using AdicMultipleContainerTest.Example;

public class AdicMultipleContainerTestGameRoot : ContextRoot {
	private IInjectionContainer containerOne;
	private IInjectionContainer containerTwo;

	[Inject] private TestCommand Command;

	public override void SetupContainers() {
		this.containerOne = this.AddContainer<InjectionContainer>()
			.RegisterExtension<CommanderContainerExtension>();
		
		this.containerTwo = this.AddContainer<InjectionContainer>();

		this.containerOne.Bind<IAdicMultipleContainerTestClass>().ToNamespace("AdicMultipleContainerTest.Example");
		this.containerOne.RegisterCommands("AdicMultipleContainerTest.Command");
	}

	public override void Init() {
		this.Inject();
		this.Command.Execute();
	}
}

namespace AdicMultipleContainerTest.Example
{
	public class AdicMultipleContainerTestClassOne : IAdicMultipleContainerTestClass {
		public int Integer { get { return 999; }}
	}

	public class AdicMultipleContainerTestClassTwo : IAdicMultipleContainerTestClass {
		public int Integer { get { return 111; } }
	}

	public interface IAdicMultipleContainerTestClass {
		int Integer { get; }
	}
}

namespace AdicMultipleContainerTest.Command
{
	public class TestCommand : Adic.Command {
		[Inject] private IAdicMultipleContainerTestClass[] TestClasses;

		public override void Execute(params object[] parameters) {
			UnityEngine.Debug.Log(this.TestClasses[0].Integer);
		}
	}
}
@intentor intentor self-assigned this Feb 10, 2017
@intentor intentor added this to the v2.24 milestone Feb 10, 2017
@intentor intentor added the bug label Feb 10, 2017
@intentor
Copy link
Owner

Hello, @FodderMK!

I'll take a look and release a fix on the next version.

Additionally, speaking about multiple containers, I'm working towards releasing a subcontainer system, so it's easy to have different containers working together. If you have any ideas on the subject, just drop a comment to the #41 issue!

@intentor
Copy link
Owner

intentor commented Mar 1, 2017

The problem occurs because Adic is trying to re-inject TestCommand on AdicMultipleContainerTestGameRoot from containerTwo, which doesn't contain a binding to IAdicMultipleContainerTestClass. An exception when trying to instantiate interfaces was added.

The solution is to prevent reinjection if the field/property has already been injected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants