mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add example for detailing how registration on resolve works
This commit is contained in:
parent
fdbd3c06a1
commit
f1e7b2809c
|
@ -17,4 +17,5 @@ script:
|
||||||
- dub build --build=release --config=arrayCompletionExample
|
- dub build --build=release --config=arrayCompletionExample
|
||||||
- dub build --build=release --config=annotationsExample
|
- dub build --build=release --config=annotationsExample
|
||||||
- dub build --build=release --config=applicationContextExample
|
- dub build --build=release --config=applicationContextExample
|
||||||
|
- dub build --build=release --config=registerOnResolveExample
|
||||||
# - dub build --build=ddox
|
# - dub build --build=ddox
|
||||||
|
|
12
dub.json
12
dub.json
|
@ -90,6 +90,18 @@
|
||||||
"importPaths": [
|
"importPaths": [
|
||||||
"source"
|
"source"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" : "registerOnResolveExample",
|
||||||
|
"description" : "Example where dependencies are registered at the moment they are resolved.",
|
||||||
|
"targetType": "executable",
|
||||||
|
"targetName": "registerOnResolveExample",
|
||||||
|
"sourcePaths": [
|
||||||
|
"example/registeronresolve"
|
||||||
|
],
|
||||||
|
"importPaths": [
|
||||||
|
"source"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
45
example/registeronresolve/app.d
Normal file
45
example/registeronresolve/app.d
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue