From ab30e633e0b05a70c757b0f2d8d6de3513761373 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 25 Aug 2016 21:17:47 +0200 Subject: [PATCH] Add example on constructor injection --- .travis.yml | 1 + dub.json | 12 ++++++ example/constructorinjection/app.d | 64 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 example/constructorinjection/app.d diff --git a/.travis.yml b/.travis.yml index f782aa8..c2bcb48 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,4 +22,5 @@ script: - dub build --build=release --config=annotationsExample - dub build --build=release --config=applicationContextExample - dub build --build=release --config=registerOnResolveExample + - dub build --build=release --config=constructorInjectionExample # - dub build --build=ddox diff --git a/dub.json b/dub.json index d663b56..12c9000 100644 --- a/dub.json +++ b/dub.json @@ -102,6 +102,18 @@ "importPaths": [ "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" + ] } ] } diff --git a/example/constructorinjection/app.d b/example/constructorinjection/app.d new file mode 100644 index 0000000..d97f839 --- /dev/null +++ b/example/constructorinjection/app.d @@ -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(); +}