mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add example where qualifiers are used
This commit is contained in:
parent
a41bfe8ac4
commit
2a69cb4757
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@
|
||||||
/poodinis.lib
|
/poodinis.lib
|
||||||
/poodinis.obj
|
/poodinis.obj
|
||||||
/quickstartExample.exe
|
/quickstartExample.exe
|
||||||
|
/qualifiersExample.exe
|
||||||
/.settings
|
/.settings
|
||||||
/.dub
|
/.dub
|
||||||
/dub.selections.json
|
/dub.selections.json
|
||||||
|
|
|
@ -3,3 +3,4 @@ language: d
|
||||||
script:
|
script:
|
||||||
- dub test --build=unittest --config=unittest
|
- dub test --build=unittest --config=unittest
|
||||||
- dub build --build=release --config=quickstartExample
|
- dub build --build=release --config=quickstartExample
|
||||||
|
- dub build --build=release --config=qualifiersExample
|
||||||
|
|
13
dub.json
13
dub.json
|
@ -34,7 +34,6 @@
|
||||||
{
|
{
|
||||||
"name" : "quickstartExample",
|
"name" : "quickstartExample",
|
||||||
"description" : "Quickstart example from the Poodinis readme.",
|
"description" : "Quickstart example from the Poodinis readme.",
|
||||||
"license": "MIT",
|
|
||||||
"targetType": "executable",
|
"targetType": "executable",
|
||||||
"targetName": "quickstartExample",
|
"targetName": "quickstartExample",
|
||||||
"sourcePaths": [
|
"sourcePaths": [
|
||||||
|
@ -43,6 +42,18 @@
|
||||||
"importPaths": [
|
"importPaths": [
|
||||||
"source"
|
"source"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" : "qualifiersExample",
|
||||||
|
"description" : "Quickstart example where qualifiers are used.",
|
||||||
|
"targetType": "executable",
|
||||||
|
"targetName": "qualifiersExample",
|
||||||
|
"sourcePaths": [
|
||||||
|
"example/qualifiers"
|
||||||
|
],
|
||||||
|
"importPaths": [
|
||||||
|
"source"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
50
example/qualifiers/app.d
Normal file
50
example/qualifiers/app.d
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
import poodinis.dependency;
|
||||||
|
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
interface Engine {
|
||||||
|
public void engage();
|
||||||
|
}
|
||||||
|
|
||||||
|
class FuelEngine : Engine {
|
||||||
|
public void engage() {
|
||||||
|
writeln("VROOOOOOM!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ElectricEngine : Engine {
|
||||||
|
public void engage() {
|
||||||
|
writeln("hummmmmmmm....");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class HybridCar {
|
||||||
|
alias KilometersPerHour = int;
|
||||||
|
|
||||||
|
@Autowire!FuelEngine
|
||||||
|
public Engine fuelEngine;
|
||||||
|
|
||||||
|
@Autowire!ElectricEngine
|
||||||
|
public Engine electricEngine;
|
||||||
|
|
||||||
|
public void moveAtSpeed(KilometersPerHour speed) {
|
||||||
|
if (speed <= 45) {
|
||||||
|
electricEngine.engage();
|
||||||
|
} else {
|
||||||
|
fuelEngine.engage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
auto container = DependencyContainer.getInstance();
|
||||||
|
|
||||||
|
container.register!HybridCar;
|
||||||
|
container.register!(Engine, FuelEngine);
|
||||||
|
container.register!(Engine, ElectricEngine);
|
||||||
|
|
||||||
|
auto car = container.resolve!HybridCar;
|
||||||
|
|
||||||
|
car.moveAtSpeed(10); // Should print "hummmmmmmm...."
|
||||||
|
car.moveAtSpeed(50); // Should print "VROOOOOOM!"
|
||||||
|
}
|
Loading…
Reference in a new issue