From a59e7a9731e24a6e9f39b621bb80671e72d3bcf6 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 4 Jul 2015 00:30:14 +0200 Subject: [PATCH] Add example project demonstrating @AssignNewInstance --- .gitignore | 2 ++ dub.json | 12 +++++++++++ example/annotations/app.d | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 example/annotations/app.d diff --git a/.gitignore b/.gitignore index ff8263d..d06ef68 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/dub.json b/dub.json index aedfa69..e8d7e30 100644 --- a/dub.json +++ b/dub.json @@ -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" + ] } ] } diff --git a/example/annotations/app.d b/example/annotations/app.d new file mode 100644 index 0000000..e6e06b7 --- /dev/null +++ b/example/annotations/app.d @@ -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!!!!!"); + } +}