Add example project demonstrating @AssignNewInstance

This commit is contained in:
Mike Bierlee 2015-07-04 00:30:14 +02:00
parent 3e11ff1e92
commit a59e7a9731
3 changed files with 57 additions and 0 deletions

2
.gitignore vendored
View file

@ -4,6 +4,7 @@
/quickstartExample.exe
/qualifiersExample.exe
/arrayCompletionExample.exe
/annotationsExample.exe
/.settings
/.dub
/dub.selections.json
@ -16,3 +17,4 @@
/quickstartExample
/qualifiersExample
/arrayCompletionExample
/annotationsExample

View file

@ -66,6 +66,18 @@
"importPaths": [
"source"
]
},
{
"name" : "annotationsExample",
"description" : "Example where several Poodinis autowire annotations are demonstrated.",
"targetType": "executable",
"targetName": "annotationsExample",
"sourcePaths": [
"example/annotations"
],
"importPaths": [
"source"
]
}
]
}

43
example/annotations/app.d Normal file
View file

@ -0,0 +1,43 @@
import poodinis;
import std.random;
import std.digest.md;
import std.stdio;
import std.conv;
class SuperSecurityDevice {
private int seed;
public this() {
auto randomGenerator = Random(unpredictableSeed);
seed = uniform(0, 999, randomGenerator);
}
public string getPassword() {
return to!string(seed) ~ "t1m3sp13!!:";
}
}
class SecurityManager {
@Autowire
public SuperSecurityDevice levelOneSecurity;
@Autowire
@AssignNewInstance
public SuperSecurityDevice levelTwoSecurity;
}
void main() {
auto dependencies = DependencyContainer.getInstance();
dependencies.register!SuperSecurityDevice; // Registered with the default "Single instance" scope
dependencies.register!SecurityManager;
auto manager = dependencies.resolve!SecurityManager;
writeln("Password for user one: " ~ manager.levelOneSecurity.getPassword());
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
if (manager.levelOneSecurity is manager.levelTwoSecurity) {
writeln("SECURITY BREACH!!!!!");
}
}