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!!!!!"); + } +}