Add registration of basic dependencies through application context

This commit is contained in:
Mike Bierlee 2015-12-24 01:11:39 +01:00
parent be5eb37617
commit 55d3139f5b
5 changed files with 61 additions and 0 deletions

View file

@ -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.
*/

20
source/poodinis/context.d Normal file
View file

@ -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) {}
}

View file

@ -14,3 +14,4 @@ module poodinis;
public import poodinis.autowire;
public import poodinis.container;
public import poodinis.registration;
public import poodinis.context;

View file

@ -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);
}
}

View file

@ -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) {
}