diff --git a/source/poodinis/container.d b/source/poodinis/container.d index d5f67a4..177c8d2 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -23,6 +23,7 @@ debug { import poodinis.registration; import poodinis.autowire; +import poodinis.context; /** * Exception thrown when errors occur while resolving a type in a dependency container. @@ -361,6 +362,18 @@ synchronized class DependencyContainer { registrations.remove(typeid(RegistrationType)); } + /** + * Register dependencies through an application context. + * + * An application context allows you to fine-tune dependency set-up and instantiation. + * It is mostly used for dependencies which come from an external library or when you don't + * want to use annotations to set-up dependencies in your classes. + */ + public void registerContext(Context : ApplicationContext)() { + auto context = new Context(); + context.registerDependencies(this); + } + /** * Returns a global singleton instance of a dependency container. */ diff --git a/source/poodinis/context.d b/source/poodinis/context.d new file mode 100644 index 0000000..bd4fb65 --- /dev/null +++ b/source/poodinis/context.d @@ -0,0 +1,20 @@ +/** + * Contains the implementation of application context setup. + * + * Part of the Poodinis Dependency Injection framework. + * + * Authors: + * Mike Bierlee, m.bierlee@lostmoment.com + * Copyright: 2014-2015 Mike Bierlee + * License: + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +module poodinis.context; + +import poodinis.container; + +class ApplicationContext { + public void registerDependencies(shared(DependencyContainer) container) {} +} diff --git a/source/poodinis/package.d b/source/poodinis/package.d index 88f80c8..94954e0 100644 --- a/source/poodinis/package.d +++ b/source/poodinis/package.d @@ -14,3 +14,4 @@ module poodinis; public import poodinis.autowire; public import poodinis.container; public import poodinis.registration; +public import poodinis.context; diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index cfe6597..da32df9 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -11,6 +11,12 @@ import std.exception; import core.thread; version(unittest) { + class TestContext : ApplicationContext { + public override void registerDependencies(shared(DependencyContainer) container) { + container.register!TestClass; + } + } + interface TestInterface { } @@ -485,4 +491,13 @@ version(unittest) { assert(instance.unrelated !is null); } + + // Test setting up simple dependencies through application context + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.registerContext!TestContext; + auto instance = container.resolve!TestClass; + + assert(instance !is null); + } } diff --git a/test/poodinis/contexttest.d b/test/poodinis/contexttest.d new file mode 100644 index 0000000..fd142d1 --- /dev/null +++ b/test/poodinis/contexttest.d @@ -0,0 +1,12 @@ +/** + * Poodinis Dependency Injection Framework + * Copyright 2014-2015 Mike Bierlee + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +import poodinis.context; + +version(unittest) { + +}