Move initializer to its own example

This commit is contained in:
Mike Bierlee 2021-04-29 00:30:12 +03:00
parent bb4eaf8676
commit 89512e0cb0
4 changed files with 41 additions and 14 deletions

View file

@ -135,6 +135,18 @@
"importPaths": [ "importPaths": [
"source" "source"
] ]
},
{
"name" : "injectionInitializerExample",
"description" : "Example where the usage of an injection initializer is demonstrated.",
"targetType": "executable",
"targetName": "injectionInitializerExample",
"sourcePaths": [
"example/injectioninitializer"
],
"importPaths": [
"source"
]
} }
] ]
} }

View file

@ -5,6 +5,8 @@
* The full terms of the license can be found in the LICENSE file. * The full terms of the license can be found in the LICENSE file.
*/ */
import std.stdio;
class Scheduler { class Scheduler {
private Calendar calendar; private Calendar calendar;
@ -36,17 +38,9 @@ class Calendar {
} }
} }
import std.stdio;
class HardwareClock { class HardwareClock {
// Parameterless constructors will halt any further selection of constructors. // Parameterless constructors will halt any further selection of constructors.
this() { this() {}
writeln("default constructor");
}
this(string name) {
writeln(name);
}
// As a result, this constructor will not be used when HardwareClock is created. // As a result, this constructor will not be used when HardwareClock is created.
this(Calendar calendar) { this(Calendar calendar) {
@ -64,10 +58,7 @@ void main() {
auto dependencies = new shared DependencyContainer(); auto dependencies = new shared DependencyContainer();
dependencies.register!Scheduler; dependencies.register!Scheduler;
dependencies.register!Calendar; dependencies.register!Calendar;
dependencies.register!HardwareClock( { dependencies.register!HardwareClock;
writeln("Running the creator");
return new HardwareClock("clock name");
});
auto scheduler = dependencies.resolve!Scheduler; auto scheduler = dependencies.resolve!Scheduler;
scheduler.scheduleJob(); scheduler.scheduleJob();

View file

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

View file

@ -202,7 +202,7 @@ synchronized class DependencyContainer {
} }
/** /**
* * TODO: Deduplicate code
*/ */
Registration register(SuperType, ConcreteType : SuperType)(InjectionInitializer!SuperType initializer, Registration register(SuperType, ConcreteType : SuperType)(InjectionInitializer!SuperType initializer,
RegistrationOption options = RegistrationOption.none) RegistrationOption options = RegistrationOption.none)