From 89512e0cb0e4b6eb8e64b7511176cf24e408ea33 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 29 Apr 2021 00:30:12 +0300 Subject: [PATCH] Move initializer to its own example --- dub.json | 12 ++++++++++++ example/constructorinjection/app.d | 17 ++++------------- example/injectioninitializer/app.d | 24 ++++++++++++++++++++++++ source/poodinis/container.d | 2 +- 4 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 example/injectioninitializer/app.d diff --git a/dub.json b/dub.json index 1536f9f..c5733af 100644 --- a/dub.json +++ b/dub.json @@ -135,6 +135,18 @@ "importPaths": [ "source" ] + }, + { + "name" : "injectionInitializerExample", + "description" : "Example where the usage of an injection initializer is demonstrated.", + "targetType": "executable", + "targetName": "injectionInitializerExample", + "sourcePaths": [ + "example/injectioninitializer" + ], + "importPaths": [ + "source" + ] } ] } diff --git a/example/constructorinjection/app.d b/example/constructorinjection/app.d index 1394312..0e16c3e 100644 --- a/example/constructorinjection/app.d +++ b/example/constructorinjection/app.d @@ -5,6 +5,8 @@ * The full terms of the license can be found in the LICENSE file. */ + import std.stdio; + class Scheduler { private Calendar calendar; @@ -36,18 +38,10 @@ class Calendar { } } -import std.stdio; - class HardwareClock { // Parameterless constructors will halt any further selection of constructors. - this() { - writeln("default constructor"); - } + this() {} - this(string name) { - writeln(name); - } - // 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"); @@ -64,10 +58,7 @@ void main() { auto dependencies = new shared DependencyContainer(); dependencies.register!Scheduler; dependencies.register!Calendar; - dependencies.register!HardwareClock( { - writeln("Running the creator"); - return new HardwareClock("clock name"); - }); + dependencies.register!HardwareClock; auto scheduler = dependencies.resolve!Scheduler; scheduler.scheduleJob(); diff --git a/example/injectioninitializer/app.d b/example/injectioninitializer/app.d new file mode 100644 index 0000000..1e2b3f5 --- /dev/null +++ b/example/injectioninitializer/app.d @@ -0,0 +1,24 @@ +/** + * Poodinis Dependency Injection Framework + * Copyright 2014-2021 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. + */ + +import poodinis; +import std.stdio; + +class Doohickey +{ +} + +void main() +{ + auto dependencies = new shared DependencyContainer(); + dependencies.register!Doohickey({ + writeln("Creating Doohickey via initializer delegate."); + return new Doohickey(); + }); + + dependencies.resolve!Doohickey; +} diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 2df6775..6bafd77 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -202,7 +202,7 @@ synchronized class DependencyContainer { } /** - * + * TODO: Deduplicate code */ Registration register(SuperType, ConcreteType : SuperType)(InjectionInitializer!SuperType initializer, RegistrationOption options = RegistrationOption.none)