mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add example on constructor injection
This commit is contained in:
parent
ed20c8fd16
commit
ab30e633e0
|
@ -22,4 +22,5 @@ script:
|
||||||
- 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=release --config=registerOnResolveExample
|
||||||
|
- dub build --build=release --config=constructorInjectionExample
|
||||||
# - dub build --build=ddox
|
# - dub build --build=ddox
|
||||||
|
|
12
dub.json
12
dub.json
|
@ -102,6 +102,18 @@
|
||||||
"importPaths": [
|
"importPaths": [
|
||||||
"source"
|
"source"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" : "constructorInjectionExample",
|
||||||
|
"description" : "Example where dependencies are injected into constructors when their classes are created.",
|
||||||
|
"targetType": "executable",
|
||||||
|
"targetName": "constructorInjectionExample",
|
||||||
|
"sourcePaths": [
|
||||||
|
"example/constructorinjection"
|
||||||
|
],
|
||||||
|
"importPaths": [
|
||||||
|
"source"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
64
example/constructorinjection/app.d
Normal file
64
example/constructorinjection/app.d
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Scheduler {
|
||||||
|
private Calendar calendar;
|
||||||
|
|
||||||
|
// All parameters will autmatically be assigned when Scheduler is created.
|
||||||
|
this(Calendar calendar) {
|
||||||
|
this.calendar = calendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void scheduleJob() {
|
||||||
|
calendar.findOpenDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Calendar {
|
||||||
|
private HardwareClock hardwareClock;
|
||||||
|
|
||||||
|
// This constructor contains built-in type "int" and thus will not be used.
|
||||||
|
this(int initialDateTimeStamp, HardwareClock hardwareClock) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// This constructor is chosen instead as candidate for injection when Calendar is created.
|
||||||
|
this(HardwareClock hardwareClock) {
|
||||||
|
this.hardwareClock = hardwareClock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findOpenDate() {
|
||||||
|
hardwareClock.doThings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HardwareClock {
|
||||||
|
// Parameterless constructors will halt any further selection of constructors.
|
||||||
|
this() {}
|
||||||
|
|
||||||
|
// As a result, this constructor will not be used when HardwareClock is created.
|
||||||
|
this(Calendar calendar) {
|
||||||
|
throw new Exception("This constructor should not be used by Poodinis");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doThings() {
|
||||||
|
import std.stdio;
|
||||||
|
writeln("Things are being done!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
import poodinis; // Locally imported to emphasize that classes do not depend on Poodinis.
|
||||||
|
|
||||||
|
auto dependencies = new shared DependencyContainer();
|
||||||
|
dependencies.register!Scheduler;
|
||||||
|
dependencies.register!Calendar;
|
||||||
|
dependencies.register!HardwareClock;
|
||||||
|
|
||||||
|
auto scheduler = dependencies.resolve!Scheduler;
|
||||||
|
scheduler.scheduleJob();
|
||||||
|
}
|
Loading…
Reference in a new issue