diff --git a/source/poodinis/container.d b/source/poodinis/container.d index d9fc746..61037a3 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -373,6 +373,7 @@ synchronized class DependencyContainer { auto context = new Context(); context.registerDependencies(this); context.registerContextComponents(this); + autowire(this, context); } /** diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 4c47098..61490fb 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -22,6 +22,41 @@ version(unittest) { } } + class AutowiredTestContext : ApplicationContext { + + @Autowire + public UnrelatedClass unrelatedClass; + + @Component + public ClassWrapper wrapper() { + return new ClassWrapper(unrelatedClass); + } + } + + class ComplexAutowiredTestContext : ApplicationContext { + + @Autowire + public UnrelatedClass unrelatedClass; + + @Autowire + public ClassWrapper classWrapper; + + public override void registerDependencies(shared(DependencyContainer) container) { + container.register!UnrelatedClass; + } + + @Component + public ClassWrapper wrapper() { + return new ClassWrapper(unrelatedClass); + } + + @Component + public ClassWrapperWrapper wrapperWrapper() { + return new ClassWrapperWrapper(classWrapper); + } + + } + interface TestInterface { } @@ -129,6 +164,22 @@ version(unittest) { public Wants wants; } + class ClassWrapper { + public Object someClass; + + this(Object someClass) { + this.someClass = someClass; + } + } + + class ClassWrapperWrapper { + public ClassWrapper wrapper; + + this(ClassWrapper wrapper) { + this.wrapper = wrapper; + } + } + // Test register concrete type unittest { shared(DependencyContainer) container = new DependencyContainer(); @@ -514,4 +565,28 @@ version(unittest) { assert(instance !is null); } + + // Test autowiring application context + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.register!UnrelatedClass; + container.registerContext!AutowiredTestContext; + auto instance = container.resolve!ClassWrapper; + + assert(instance !is null); + assert(instance.someClass !is null); + } + + // Test autowiring application context with dependencies registered in same context + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.registerContext!ComplexAutowiredTestContext; + auto instance = container.resolve!ClassWrapperWrapper; + auto wrapper = container.resolve!ClassWrapper; + auto someClass = container.resolve!UnrelatedClass; + + assert(instance !is null); + assert(instance.wrapper is wrapper); + assert(instance.wrapper.someClass is someClass); + } }