Add example where qualifiers are used

This commit is contained in:
Mike Bierlee 2015-02-21 17:48:55 +01:00
parent a41bfe8ac4
commit 2a69cb4757
4 changed files with 64 additions and 1 deletions

1
.gitignore vendored
View file

@ -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

View file

@ -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

View file

@ -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
View 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!"
}