diff --git a/.gitignore b/.gitignore index bd634af..6487696 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /poodinis.lib /poodinis.obj /quickstartExample.exe +/qualifiersExample.exe /.settings /.dub /dub.selections.json diff --git a/.travis.yml b/.travis.yml index b300fa1..fdd8525 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ language: d script: - dub test --build=unittest --config=unittest - dub build --build=release --config=quickstartExample + - dub build --build=release --config=qualifiersExample diff --git a/dub.json b/dub.json index 35e5113..9d0f5a3 100644 --- a/dub.json +++ b/dub.json @@ -34,7 +34,6 @@ { "name" : "quickstartExample", "description" : "Quickstart example from the Poodinis readme.", - "license": "MIT", "targetType": "executable", "targetName": "quickstartExample", "sourcePaths": [ @@ -43,6 +42,18 @@ "importPaths": [ "source" ] + }, + { + "name" : "qualifiersExample", + "description" : "Quickstart example where qualifiers are used.", + "targetType": "executable", + "targetName": "qualifiersExample", + "sourcePaths": [ + "example/qualifiers" + ], + "importPaths": [ + "source" + ] } ] } diff --git a/example/qualifiers/app.d b/example/qualifiers/app.d new file mode 100644 index 0000000..0ab24c8 --- /dev/null +++ b/example/qualifiers/app.d @@ -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!" +}