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": [
"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.
*/
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();

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,
RegistrationOption options = RegistrationOption.none)