Add example for detailing how registration on resolve works

This commit is contained in:
Mike Bierlee 2016-02-09 21:29:23 +01:00
parent fdbd3c06a1
commit f1e7b2809c
3 changed files with 58 additions and 0 deletions

View file

@ -17,4 +17,5 @@ script:
- dub build --build=release --config=arrayCompletionExample
- dub build --build=release --config=annotationsExample
- dub build --build=release --config=applicationContextExample
- dub build --build=release --config=registerOnResolveExample
# - dub build --build=ddox

View file

@ -90,6 +90,18 @@
"importPaths": [
"source"
]
},
{
"name" : "registerOnResolveExample",
"description" : "Example where dependencies are registered at the moment they are resolved.",
"targetType": "executable",
"targetName": "registerOnResolveExample",
"sourcePaths": [
"example/registeronresolve"
],
"importPaths": [
"source"
]
}
]
}

View file

@ -0,0 +1,45 @@
/**
* Poodinis Dependency Injection Framework
* Copyright 2014-2016 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;
class Violin {
}
interface InstrumentPlayer {
}
class ViolinPlayer : InstrumentPlayer {
// Autowired concrete types can be registered on resolve
@Autowire
private Violin violin;
}
class Orchestra {
// Autowired non-concrete types can be registered on resolved, given they have a qualifier.
@Autowire!ViolinPlayer
private InstrumentPlayer violinPlayer;
}
void main() {
auto dependencies = DependencyContainer.getInstance();
/*
* By using the resolve option "registerBeforeResolving" you can register the resolved class
* immediately. Note that any autowired member will not automatically be registered as well.
*/
auto violinPlayer = dependencies.resolve!Violin([ResolveOption.registerBeforeResolving]);
/*
* You can make the resolve option persistent by setting it on the container with setPersistentResolveOptions().
* This will register all resolved types and their autowired members (recursively).
* Note that you will still get ResolveExceptions when a non-concrete type is autowired (without qualifier).
* In those cases you will still have to register those particular dependencies beforehand.
*/
dependencies.setPersistentResolveOptions(ResolveOption.registerBeforeResolving);
auto orchestra = dependencies.resolve!Orchestra;
}